Files
rosetta/docs/guide/subtitles.md
T

5.5 KiB

Subtitles

SRT, WebVTT and ASS / SSA files load as one Subtitles value: a list of timed cues, plus named styles where the format has them. Cue text uses one small markup for all three (<i>, <b>, <u>, <s>, and line breaks as \n), so converting between the formats keeps italics and line breaks.

local subs = assert(F.subtitle.load(srtText))
local cue = subs.cues[1]
print(cue.start, cue.stop, cue.text)                    -- 0.5  2  Hello, <i>world</i>.
local vtt = assert(F.saveString(subs, { format = "vtt" }))
assert(string.sub(vtt, 1, 6) == "WEBVTT")

The type

F.Types.Subtitles.new() makes an empty one.

Field Type Meaning
kind "subtitles"
cues { Cue } in file order
styles { [name]: { [field]: string } } ASS styles by name; empty for SRT and WebVTT
meta table source, and each format's extras (below)
Cue field Type Meaning
start, stop number seconds
text string the shared markup: <i>, <b>, <u>, <s> (SRT also <font ...>, WebVTT <c.class> / <v Name> as written); lines separated by "\n"
id string? SRT index, WebVTT cue identifier
style string? ASS: the style name
settings string? WebVTT cue settings ("align:start"), SRT coordinates, as written

ASS cues also keep layer, name, marginL, marginR, marginV, effect, and raw: the original text with its override blocks ({\pos(...)}, karaoke, ...), which the writer uses when text is unchanged.

Format meta extras
ASS scriptInfo (Title, PlayResX, ...), scriptInfoOrder, styleFormat, styleOrder, eventFormat, comments, sections (other sections, verbatim)
WebVTT header, notes, css (STYLE blocks), regions
local s = assert(F.subtitle.load(assText))
assert(#s.cues == 2 and s.cues[2].style == "Sign")
assert(s.cues[2].text == "A sign\nwith two lines")      -- \N became a line break
assert(s.styles.Sign.Fontsize == "16" and s.styles.Default.PrimaryColour == "&H00FFFFFF")
assert(s.meta.scriptInfo.PlayResX == "640")
local v = assert(F.subtitle.load(vttText))
assert(v.cues[1].id == "intro" and v.cues[1].settings == "align:start")

Loading

F.subtitle.load(src, opts?) or F.load(src); text with a BOM, CRLF or CR line ends is fine. The one option is maxBytes (default 64 MB). SRT has no magic number, but its timing lines are recognised.

assert(F.detect(srtText) == "srt" and F.detect(vttText) == "vtt" and F.detect(assText) == "ass")
local s = assert(F.load(srtText))
assert(s.kind == "subtitles" and s.cues[2].text == "Second line\nspans two rows.")

Finding the cue at a time

local s = assert(F.subtitle.load(srtText))
local function cueAt(t: number): string?
	for _, c in s.cues do
		if t >= c.start and t < c.stop then
			return c.text
		end
	end
	return nil
end
assert(cueAt(1) == "Hello, <i>world</i>." and cueAt(2.2) == nil)
local plain = string.gsub(cueAt(1) :: string, "<[^>]+>", "")   -- strip the markup for a plain label
assert(plain == "Hello, world.")

Editing, saving, converting

Cues are plain tables: shift, edit or add them, then save. The format comes from opts.format, the destination's extension, or the source format.

local s = assert(F.subtitle.load(srtText))
for _, c in s.cues do
	c.start += 1.5                                      -- delay everything by 1.5 s
	c.stop += 1.5
end
table.insert(s.cues, { start = 6, stop = 7.5, text = "<b>The end</b>" })
local srt = assert(F.saveString(s))                     -- SRT again; cues are renumbered from 1
assert(string.find(srt, "00:00:02,000 --> 00:00:03,500", 1, true))
local ass = assert(F.saveString(s, { format = "ass" }))  -- markup becomes {\b1}...{\b0}
assert(string.find(ass, "{\\b1}The end{\\b0}", 1, true))

local fresh = F.Types.Subtitles.new()
table.insert(fresh.cues, { start = 1, stop = 2.5, text = "Hi" })
assert(F.saveString(fresh, { format = "srt" }) == "1\n00:00:01,000 --> 00:00:02,500\nHi\n\n")
Format Writes Options
srt cues renumbered, times to the millisecond crlf
vtt header, STYLE / REGION blocks, cues (times to the millisecond)
ass an ASS v4.00+ script with the source's styles (a Default style when there are none); times to the centisecond crlf

Converting ASS to SRT or WebVTT drops the styles and anything the markup cannot hold (positions, colours, karaoke); converting to ASS gives every cue without a style the Default style.

local srt = assert(F.convert(assText, "srt"))
local back = assert(F.subtitle.load(srt))
assert(back.cues[1].text == "Hello, <i>world</i>." and back.cues[2].style == nil)

Formats

Format Read Write Styles
srt yes yes no
vtt (WebVTT) yes yes CSS kept in meta.css
ass / ssa yes yes (ASS v4.00+) yes

See FORMATS.md. Text subtitle tracks inside Matroska files are read by F.format("mkv").subtitles (video.md).

Gotchas

  • Times are seconds as floating point numbers; writers round them (SRT / WebVTT to the millisecond, ASS to the centisecond).
  • Style fields are strings as written in the file (Fontsize = "16", colours as &HAABBGGRR). Nothing is rendered.
  • WebVTT cue text cannot hold blank lines or -->: the writer drops the blank lines and escapes the arrow.
  • Tags in SRT text are not validated; unknown ones pass through as text.
  • The cue list is not sorted for you. Sort by start after inserting cues out of order if the player needs it.