vec: subtitle/srt, subtitle/vtt, subtitle/ass (SSA too) read + write into Subtitles; tests against ffmpeg conversions, round trips, fuzz

This commit is contained in:
4DBug committed 2026-10-31 08:27:04 -05:00
1 parent ccb06775c5
commit 2521f3dfa6
9 files changed
+1337

No files matched your search

+109
View File
@@ -0,0 +1,109 @@
-- subtitle/ass tests: ffmpeg's ASS output of a random script (times to the centisecond, text through the override
-- markup), hand-written ASS (styles, comments, commas in text, overrides, [Fonts]) and SSA v4 files against ffmpeg's
-- SRT conversion of them, styles as data, round trips through the encoder, the contract and the fuzz pass.
-- Fixtures: tools/fixtures/subtitles.py.
local T = require("@tests/T")
local Ass = require("@formats/subtitle/ass")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/subtitles")
local t = T.suite("ass")
local function decode(s: string): (any, any)
return Ass.decode(Bytes.reader(buffer.fromstring(s)))
end
local function plain(s: string): string
s = string.gsub(s, "<[^>]*>", "")
s = string.gsub(s, "{[^}]*}", "")
return s
end
local function same(subs: any, want: any, label: string, tol: number)
t:eq(#subs.cues, #want, label .. ": cue count")
for i, w in want do
local c = subs.cues[i]
t:check(math.abs(c.start * 1000 - w.start) <= tol, label .. ": cue " .. i .. " start " .. c.start)
t:check(math.abs(c.stop * 1000 - w.stop) <= tol, label .. ": cue " .. i .. " stop " .. c.stop)
t:eq(plain(c.text), w.plain, label .. ": cue " .. i .. " text")
end
end
t:eq(Ass.name, "ass", "name")
t:check(Ass.caps.decode and Ass.caps.encode, "caps")
t:eq(Ass.sniff(Bytes.reader(buffer.fromstring(Fx.ffmpeg_ass))), 1, "sniff")
-- ffmpeg converts SRT to ASS with its own rounding (start and duration to centiseconds): within 10 ms
local subs, err = decode(Fx.ffmpeg_ass)
t:check(subs, "ffmpeg ass: " .. tostring(err))
same(subs, Fx.cues, "ffmpeg ass", 10)
t:eq(subs.styles.Default.Fontname, "Arial", "ffmpeg's Default style")
t:eq(subs.meta.scriptInfo.PlayResX, "384", "script info")
for _, h in Fx.hand do
if h.name ~= "vtt" then
local s, e = decode(h.data)
t:check(s, h.name .. ": " .. tostring(e))
same(s, h.cues, h.name .. " vs ffmpeg", 0.5)
end
end
local a = decode(Fx.hand[2].data)
t:eq(a.meta.scriptInfo.Title, "test", "Title")
t:eq(a.meta.scriptInfoOrder[1], "; Script generated by hand", "comment kept in order")
t:eq(a.styles.Top.Fontname, "Verdana", "style field")
t:eq(a.styles.Top.Alignment, "8", "style alignment")
t:eq(a.meta.styleOrder[2], "Top", "style order")
t:eq(a.cues[1].name, "Bob", "actor")
t:eq(a.cues[1].text, "Hello, <i>world</i>!", "italic markup")
t:eq(a.cues[1].raw, "Hello, {\\i1}world{\\i0}!", "raw text")
t:eq(a.cues[2].layer, 1, "layer")
t:eq(a.cues[2].style, "Top", "style")
t:eq(a.cues[2].text, "<b>Bold top</b>\nsecond line", "bold, \\N, an8 dropped")
t:eq(a.cues[3].text, "commas, inside, the text", "commas in the text field")
t:eq(#a.meta.comments, 1, "comment event")
t:eq(a.meta.sections[1].name, "Fonts", "[Fonts] kept")
t:eq(a.meta.sections[1].lines[2], "ABCDEF", "[Fonts] lines")
local ssa = decode(Fx.hand[3].data)
t:eq(ssa.meta.source.scriptType, "v4.00", "SSA script type")
t:eq(ssa.styles.Default.TertiaryColour, "65535", "SSA style field")
t:eq(ssa.cues[1].marked, "Marked=0", "SSA Marked")
-- round trips: the hand files exactly (same fields, same order), and the ffmpeg file through the cues
for _, s in { a, ssa, subs } do
local out = Ass.encode(s) :: buffer
local back = Ass.decode(Bytes.reader(out))
t:check(back, "round trip")
t:eq(#back.cues, #s.cues, "rt count")
for i, c in s.cues do
t:eq(math.floor(back.cues[i].start * 100 + 0.5), math.floor(c.start * 100 + 0.5), "rt start")
t:eq(math.floor(back.cues[i].stop * 100 + 0.5), math.floor(c.stop * 100 + 0.5), "rt stop")
t:eq(back.cues[i].text, c.text, "rt text")
t:eq(back.cues[i].raw, c.raw, "rt raw")
t:eq(back.cues[i].style, c.style, "rt style")
end
for nm, st in s.styles do
for k, v in st do
t:eq(back.styles[nm][k], v, "rt style " .. nm .. "." .. k)
end
end
end
t:eq(buffer.tostring(Ass.encode(a) :: buffer), Fx.hand[2].data, "hand ASS written back byte for byte")
-- cues from another format: markup converted, a Default style made
do
local out = Ass.encode({ kind = "subtitles", cues = { { start = 1, stop = 2.5, text = "<i>it</i> and <b>bold</b>\nline <font color=\"red\">x</font>" } }, styles = {}, meta = {} } :: any) :: buffer
local back = Ass.decode(Bytes.reader(out))
t:eq(back.cues[1].raw, "{\\i1}it{\\i0} and {\\b1}bold{\\b0}\\Nline x", "markup to overrides")
t:check(back.styles.Default ~= nil, "a Default style")
t:eq(back.cues[1].style, "Default", "cue style")
end
do
local _, e = decode("Dialogue: 0,0:00:01.00,0:00:02.00,Default,,0,0,0,,x")
t:check(e and e.code == Err.CORRUPT, "no [Script Info]")
local _, e2 = Ass.encode({ cues = { { start = 0, stop = "x", text = "" } } } :: any)
t:check(e2 and e2.code == Err.ARG, "encode: bad stop")
end
t:fuzz("decode", function(b: buffer)
return Ass.decode(Bytes.reader(b))
end, { Fx.ffmpeg_ass, Fx.hand[2].data, Fx.hand[3].data }, { keepHeader = 13 })
t:done()
+102
View File
@@ -0,0 +1,102 @@
-- subtitle/srt tests: a random SRT script against ffmpeg (its own parse via its srt muxer: times to the millisecond,
-- text), ffmpeg's conversions of WebVTT / ASS / SSA files to SRT, odd layouts (no index, blank lines in text, CRLF,
-- BOM, dot decimals, coordinates), round trips through the encoder, the contract and the fuzz pass.
-- Fixtures: tools/fixtures/subtitles.py.
local T = require("@tests/T")
local Srt = require("@formats/subtitle/srt")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/subtitles")
local t = T.suite("srt")
local function decode(s: string): (any, any)
return Srt.decode(Bytes.reader(buffer.fromstring(s)))
end
local function plain(s: string): string
s = string.gsub(s, "<[^>]*>", "")
s = string.gsub(s, "{[^}]*}", "")
return s
end
local function ms(x: number): number
return math.floor(x * 1000 + 0.5)
end
local function same(subs: any, want: any, label: string)
t:eq(#subs.cues, #want, label .. ": cue count")
for i, w in want do
local c = subs.cues[i]
t:eq(ms(c.start), w.start, label .. ": cue " .. i .. " start")
t:eq(ms(c.stop), w.stop, label .. ": cue " .. i .. " stop")
t:eq(plain(c.text), w.plain, label .. ": cue " .. i .. " text")
end
end
t:eq(Srt.name, "srt", "name")
t:eq(Srt.category, "subtitle", "category")
t:check(Srt.caps.decode and Srt.caps.encode, "caps")
-- the random script and ffmpeg's re-mux of it
local subs, err = decode(Fx.srt)
t:check(subs, "decode: " .. tostring(err))
same(subs, Fx.cues, "random")
t:eq(subs.cues[3].text, "<u>dog</u>\nalpha dog dog na\195\175ve alpha", "markup and lines kept")
t:eq(subs.cues[1].id, "1", "id")
same(decode(Fx.ffmpeg_srt), Fx.cues, "ffmpeg srt")
t:eq(Srt.sniff(Bytes.reader(buffer.fromstring(Fx.srt))), 0.9, "sniff")
t:eq(Srt.sniff(Bytes.reader(buffer.fromstring(Fx.ffmpeg_vtt))), 0, "sniff a vtt")
for _, h in Fx.hand do
same(decode(h.ffmpeg_srt), h.cues, "ffmpeg " .. h.name .. " -> srt")
end
-- odd layouts
do
local s = "\239\187\1911\r\n00:00:01.5 --> 00:00:02,25 X1:10 X2:20 Y1:5 Y2:6\r\nfirst\r\n\r\nsecond paragraph\r\n\r\n\r\n00:00:03,000 --> 00:00:04,000\r\nno index\r\n2\r\n00:00:05,000-->00:00:06,000\r\n"
local r = decode(s)
t:check(r, "odd layouts")
t:eq(#r.cues, 3, "odd: count")
t:eq(r.cues[1].start, 1.5, "odd: 1-digit fraction")
t:eq(r.cues[1].stop, 2.25, "odd: 2-digit fraction")
t:eq(r.cues[1].text, "first\n\nsecond paragraph", "odd: blank line inside a cue")
t:eq(r.cues[1].settings, "X1:10 X2:20 Y1:5 Y2:6", "odd: coordinates")
t:eq(r.cues[2].text, "no index", "odd: cue without an index, the next index stripped")
t:eq(r.cues[2].id, nil, "odd: no id")
t:eq(r.cues[3].text, "", "odd: empty cue")
local again = decode(buffer.tostring(Srt.encode(r) :: buffer))
t:eq(again.cues[1].text, r.cues[1].text, "odd: round trip keeps the blank line")
t:eq(again.cues[1].settings, r.cues[1].settings, "odd: round trip keeps the coordinates")
end
-- round trips (LF and CRLF)
for _, crlf in { false, true } do
local out = Srt.encode(subs, { crlf = crlf }) :: buffer
local back = Srt.decode(Bytes.reader(out))
t:check(back, "round trip decode")
t:eq(#back.cues, #subs.cues, "round trip count")
for i, c in subs.cues do
t:eq(ms(back.cues[i].start), ms(c.start), "round trip start " .. i)
t:eq(ms(back.cues[i].stop), ms(c.stop), "round trip stop " .. i)
t:eq(back.cues[i].text, c.text, "round trip text " .. i)
end
if not crlf then
t:eq(buffer.tostring(out), Fx.srt, "the encoder writes the canonical file byte for byte")
end
end
-- errors
do
local _, e = decode("just some text\nno timings")
t:check(e and e.code == Err.CORRUPT, "no cues: " .. tostring(e))
local _, e2 = decode("1\n00:00:01,000 --> 00:00:02,000\n\0bin")
t:check(e2 and e2.code == Err.CORRUPT, "NUL bytes")
local empty = decode("")
t:check(empty and #empty.cues == 0, "empty file: no cues")
local _, e3 = Srt.encode({ cues = { { start = -1, stop = 1, text = "x" } } } :: any)
t:check(e3 and e3.code == Err.ARG, "encode: negative time")
local _, e4 = Srt.encode(5 :: any)
t:check(e4 and e4.code == Err.ARG, "encode: not Subtitles")
end
t:fuzz("decode", function(b: buffer)
return Srt.decode(Bytes.reader(b))
end, { Fx.srt, Fx.ffmpeg_srt, Fx.hand[1].ffmpeg_srt })
t:done()
+95
View File
@@ -0,0 +1,95 @@
-- subtitle/vtt tests: ffmpeg's WebVTT output of a random script (times, text), a hand-written file with a header,
-- STYLE / REGION / NOTE blocks, ids, settings and hour timestamps against ffmpeg's SRT conversion of it, round trips
-- through the encoder, the contract and the fuzz pass. Fixtures: tools/fixtures/subtitles.py.
local T = require("@tests/T")
local Vtt = require("@formats/subtitle/vtt")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/subtitles")
local t = T.suite("vtt")
local function decode(s: string): (any, any)
return Vtt.decode(Bytes.reader(buffer.fromstring(s)))
end
local function plain(s: string): string
s = string.gsub(s, "<[^>]*>", "")
s = string.gsub(s, "{[^}]*}", "")
s = string.gsub(s, "&amp;", "&")
return s
end
local function ms(x: number): number
return math.floor(x * 1000 + 0.5)
end
local function same(subs: any, want: any, label: string)
t:eq(#subs.cues, #want, label .. ": cue count")
for i, w in want do
local c = subs.cues[i]
t:eq(ms(c.start), w.start, label .. ": cue " .. i .. " start")
t:eq(ms(c.stop), w.stop, label .. ": cue " .. i .. " stop")
t:eq(plain(c.text), w.plain, label .. ": cue " .. i .. " text")
end
end
t:eq(Vtt.name, "vtt", "name")
t:check(Vtt.caps.decode and Vtt.caps.encode, "caps")
t:eq(Vtt.sniff(Bytes.reader(buffer.fromstring(Fx.ffmpeg_vtt))), 1, "sniff")
t:eq(Vtt.sniff(Bytes.reader(buffer.fromstring("WEBVTTX"))), 0, "sniff WEBVTTX")
local subs, err = decode(Fx.ffmpeg_vtt)
t:check(subs, "ffmpeg vtt: " .. tostring(err))
same(subs, Fx.cues, "ffmpeg vtt")
local hand
for _, h in Fx.hand do
if h.name == "vtt" then
hand = h
end
end
local v, verr = decode(hand.data)
t:check(v, "hand vtt: " .. tostring(verr))
same(v, hand.cues, "hand vtt vs ffmpeg")
t:eq(v.meta.header, "- a test file\nKind: captions", "header")
t:eq(v.meta.css[1], "::cue { color: yellow }", "STYLE")
t:eq(v.meta.regions[1], "id:r1 width:40%", "REGION")
t:eq(v.meta.notes[1], "this is a comment\nover two lines", "NOTE")
t:eq(v.cues[1].id, "intro", "id")
t:eq(v.cues[1].settings, "align:start position:10%", "settings")
t:eq(v.cues[1].text, "<v Roger>Hello <i>there</i></v>", "text kept")
t:eq(v.cues[3].settings, "line:0", "settings 3")
for _, s in { subs, v } do
local out = Vtt.encode(s) :: buffer
local back = Vtt.decode(Bytes.reader(out))
t:check(back, "round trip")
t:eq(#back.cues, #s.cues, "round trip count")
for i, c in s.cues do
t:eq(ms(back.cues[i].start), ms(c.start), "rt start")
t:eq(ms(back.cues[i].stop), ms(c.stop), "rt stop")
t:eq(back.cues[i].text, c.text, "rt text")
t:eq(back.cues[i].id, c.id, "rt id")
t:eq(back.cues[i].settings, c.settings, "rt settings")
end
t:eq(back.meta.header, s.meta.header, "rt header")
end
do
local back = decode(buffer.tostring(Vtt.encode(v) :: buffer))
t:eq(back.meta.css[1], v.meta.css[1], "rt css")
t:eq(back.meta.regions[1], v.meta.regions[1], "rt region")
-- text that WebVTT cannot hold
local w = Vtt.encode({ kind = "subtitles", cues = { { start = 0, stop = 1, text = "a\n\nb --> c" } }, styles = {}, meta = {} } :: any) :: buffer
local wb = Vtt.decode(Bytes.reader(w))
t:eq(wb.cues[1].text, "a\nb --&gt; c", "blank lines dropped, arrow escaped")
end
do
local _, e = decode("1\n00:00:01,000 --> 00:00:02,000\nx")
t:check(e and e.code == Err.CORRUPT, "no signature")
local _, e2 = Vtt.encode({ cues = { { start = 0, stop = 1 } } } :: any)
t:check(e2 and e2.code == Err.ARG, "encode: cue without text")
end
t:fuzz("decode", function(b: buffer)
return Vtt.decode(Bytes.reader(b))
end, { Fx.ffmpeg_vtt, hand.data }, { keepHeader = 6 })
t:done()