107 lines
4.5 KiB
Luau
107 lines
4.5 KiB
Luau
-- subtitle/ass tests: ffmpeg's ASS output and hand-written ASS / SSA files against ffmpeg's SRT conversion.
|
|
-- Fixtures: tools/fixtures/subtitles.py.
|
|
local T = require("@tests/T")
|
|
local Ass = require("@rosetta/subtitle/ass")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/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")
|
|
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()
|