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:
1 parent
ccb06775c5
commit
2521f3dfa6
9 files changed
+1337
No files matched your search
@@ -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()
|
||||
Reference in new issue
Block a user