96 lines
3.6 KiB
Luau
96 lines
3.6 KiB
Luau
-- 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("@rosetta/subtitle/vtt")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/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, "&", "&")
|
|
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 --> 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()
|