subtitle

package
v0.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package subtitle reads SubRip (.srt) and WebVTT (.vtt) text, decoded to UTF-8, into cues and the other blocks of the file, with their offsets in that text. Nothing is fetched or run.

Index

Constants

View Source
const MaxHeader = blocks.MaxMeta

MaxHeader bounds the text of the WebVTT header: past it the rest is left out, the text kept longer than the bound so that its user, which cuts it there, sees that it was. It is the bound of a metadata value.

View Source
const MaxText = 8 << 20

MaxText bounds the text of one cue or block: past it the text is cut, and the item says so (Cut).

View Source
const MaxVoices = 4 << 10

MaxVoices bounds the voices of a cue, joined by ", ": once they are longer, the cue's later voices are not collected (its user cuts the joined list at this bound).

Variables

This section is empty.

Functions

func SRT

func SRT(ctx context.Context, src []byte, yield func(*Item) bool) error

SRT reads SubRip text, giving yield each cue (also one with no text) and the text before the first cue. It stops when yield returns false, and returns the context's error when it is cancelled.

func Timestamp

func Timestamp(b []byte) (string, bool)

Timestamp reads a timestamp: [hours:]minutes:seconds, then "." or "," and one to three digits of a fraction, or none. Minutes and seconds are one or two digits, under 60. It returns it as HH:MM:SS.mmm.

Types

type Field

type Field struct{ Key, Value string }

Field is a WebVTT header line of the form "Key: value".

type Header struct {
	// Signature says that the file begins with the WebVTT signature.
	Signature bool
	// Text is what follows WEBVTT on the first line, trimmed, and the
	// header lines that are not "Key: value", one per line; once longer
	// than MaxHeader, the later lines are left out.
	Text string
	// Fields are the header lines "Key: value", in order.
	Fields []Field
	// Regions are the REGION definitions, as written.
	Regions []string
	// Controls counts the control characters dropped from the header.
	Controls int
}

Header is what a WebVTT file says before its first cue.

func VTT

func VTT(ctx context.Context, src []byte, maxDepth int, yield func(*Item) bool) (Header, error)

VTT reads WebVTT text, giving yield each cue (also one with no text), NOTE and STYLE block, and block that is none of these, and returning the header and the REGION definitions. It stops when yield returns false, and returns the context's error when it is cancelled.

type Item

type Item struct {
	Kind Kind
	// Start and End are the offsets in the text of the first byte of the
	// item's first line (a cue's identifier or number line, else its
	// timing line) and of the end of its last line with text, its line
	// end not included.
	Start, End int
	// ID is a WebVTT cue's identifier, a SubRip cue's number, as written.
	ID string
	// Begin and Finish are the cue's timings, HH:MM:SS.mmm when TimingOK,
	// else as written.
	Begin, Finish string
	TimingOK      bool
	// Settings are WebVTT cue settings, or SubRip coordinates, as written.
	Settings string
	// Text is the text as a viewer sees it, markup removed, trimmed; empty
	// for a cue with no text.
	Text  string
	Spans []gofastcore.Span
	// Voices are the names of the WebVTT voices of the cue, in order, each
	// once.
	Voices []string
	// Controls counts the control characters dropped from the text.
	Controls int
	// Replaced counts the character references to no character (&#0;, a
	// surrogate, a number past U+10FFFF) read as U+FFFD, in the text and
	// the voices.
	Replaced int
	// Cut says that the text was longer than MaxText and was cut there.
	Cut bool
	// Deep says that WebVTT tags nested deeper than the bound given VTT;
	// the deeper tags were ignored.
	Deep bool
}

Item is one block of the file.

type Kind

type Kind uint8

Kind says what an item is.

const (
	// Cue is a cue: its timings, settings, identifier and text.
	Cue Kind = iota
	// Note is a WebVTT NOTE block, a comment no player shows.
	Note
	// Style is a WebVTT STYLE block: its text is a style sheet.
	Style
	// Stray is text that is in no cue: before the first cue of a SubRip
	// file, or a WebVTT block that is not a cue, NOTE, STYLE or REGION.
	Stray
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL