Files
rosetta/src/Rosetta/subtitle/vtt/init.luau
T

202 lines
7.2 KiB
Luau

--!strict
-- subtitle/vtt: WebVTT (W3C WebVTT, 2019 CR). Supports: the header (meta.header), cue ids, `[hh:]mm:ss.ttt` timings,
-- cue settings, NOTE (meta.notes), STYLE (meta.css) and REGION (meta.regions) blocks, BOM, CRLF / CR; cue text as
-- written. Encode drops blank lines in cue text and escapes `-->` (cue text cannot hold them).
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
local Sniff = require("@rosetta/core/Sniff")
local Bytes = require("@rosetta/core/Bytes")
local Types = require("@rosetta/Types")
-- ------------------------------------------------------------------------------------------------ contract
local Vtt = {
name = "vtt",
category = "subtitle",
extensions = { "vtt", "webvtt" },
mime = { "text/vtt" },
deps = { "core/Err", "core/Bytes", "Types", "core/Sniff" },
caps = { decode = true, encode = true, styles = true },
}
export type DecodeOptions = {
maxBytes: number?, -- default 64 MB
}
export type EncodeOptions = {}
-- ------------------------------------------------------------------------------------------------ constants
local function frac(digits: string): number
return (tonumber(digits) :: number) / 10 ^ #digits
end
local function timestamp(s: string): (number?, string)
local h, m, sec, f, rest = string.match(s, "^(%d+):(%d%d):(%d%d)%.(%d+)(.*)$")
if h then
return tonumber(h) :: number * 3600 + tonumber(m) :: number * 60 + tonumber(sec) :: number + frac(f :: string), rest :: string
end
local m2, s2, f2, rest2 = string.match(s, "^(%d+):(%d%d)%.(%d+)(.*)$")
if m2 then
return tonumber(m2) :: number * 60 + tonumber(s2) :: number + frac(f2 :: string), rest2 :: string
end
return nil, s
end
local function timing(line: string): (number?, number?, string?)
local a, rest = timestamp((string.gsub(line, "^%s+", "")))
if not a then
return nil, nil, nil
end
local after = string.match(rest, "^%s*%-%->%s*(.*)$")
if not after then
return nil, nil, nil
end
local b, rest2 = timestamp(after)
if not b then
return nil, nil, nil
end
local settings = string.match(rest2, "^%s*(.-)%s*$") or ""
return a, b, settings
end
-- ------------------------------------------------------------------------------------------------ sniff
function Vtt.sniff(r: Bytes.Reader): number
local head = Sniff.text(r, 10)
if string.sub(head, 1, 6) == "WEBVTT" then
local c = string.sub(head, 7, 7)
if c == "" or c == " " or c == "\t" or c == "\n" or c == "\r" then
return 1
end
end
return 0
end
-- ------------------------------------------------------------------------------------------------ decode
function Vtt.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Subtitles?, Err.Err?)
local maxBytes = opts and opts.maxBytes or 2 ^ 26
if r.limit - r.base > maxBytes then
return nil, Err.new(Err.LIMIT, "file is over the " .. maxBytes .. " byte limit")
end
if Vtt.sniff(r) == 0 then
return nil, Err.new(Err.CORRUPT, "missing the WEBVTT signature", r.base)
end
local s = buffer.readstring(r.buf, r.base, r.limit - r.base)
if string.find(s, "\0", 1, true) then
return nil, Err.new(Err.CORRUPT, "binary data (NUL bytes) in a text format")
end
s = Sniff.dropBom(s)
s = string.gsub(s, "\r\n", "\n")
s = string.gsub(s, "\r", "\n")
local lines = string.split(s, "\n")
local n = #lines
local subs = Types.Subtitles.new()
-- the header block: WEBVTT[ text] + lines up to the first blank
local header = { (string.match(lines[1], "^WEBVTT[ \t]*(.*)$")) or "" }
local i = 2
while i <= n and lines[i] ~= "" do
table.insert(header, lines[i])
i += 1
end
if #header > 1 or header[1] ~= "" then
subs.meta.header = table.concat(header, "\n")
end
local notes, css, regions = {}, {}, {}
while i <= n do
if lines[i] == "" then
i += 1
continue
end
local first = i
while i <= n and lines[i] ~= "" do
i += 1
end
local block = table.move(lines, first, i - 1, 1, {})
local head = block[1]
if string.match(head, "^NOTE[ \t]") or head == "NOTE" then
table.insert(notes, (string.gsub(table.concat(block, "\n"), "^NOTE[ \t]?", "")))
elseif (head == "STYLE" or string.match(head, "^STYLE[ \t]")) and not string.find(table.concat(block, "\n"), "-->", 1, true) then
table.insert(css, table.concat(block, "\n", 2))
elseif (head == "REGION" or string.match(head, "^REGION[ \t]")) and not string.find(table.concat(block, "\n"), "-->", 1, true) then
table.insert(regions, table.concat(block, "\n", 2))
else
local k, id = 1, nil
local a, b, settings = timing(block[1])
if not a and block[2] then
id = block[1]
k = 2
a, b, settings = timing(block[2])
end
if a then
local cue: Types.Cue = { start = a, stop = b :: number, text = table.concat(block, "\n", k + 1), id = id }
if settings and settings ~= "" then
cue.settings = settings
end
table.insert(subs.cues, cue)
end
-- a block with no timing is skipped (the spec's parser ignores it)
end
end
if #notes > 0 then
subs.meta.notes = notes
end
if #css > 0 then
subs.meta.css = css
end
if #regions > 0 then
subs.meta.regions = regions
end
subs.meta.source = { format = "vtt" }
return subs, nil
end
-- ------------------------------------------------------------------------------------------------ encode
local function stamp(t: number): string
local ms = math.floor(t * 1000 + 0.5)
return string.format("%02d:%02d:%02d.%03d", ms // 3600000, ms // 60000 % 60, ms // 1000 % 60, ms % 1000)
end
local function clean(s: string): string
s = string.gsub(s, "\r\n?", "\n")
s = string.gsub(s, "\n[ \t]*\n", "\n")
s = string.gsub(s, "\n[ \t]*\n", "\n")
s = string.gsub(s, "^\n+", "")
s = string.gsub(s, "%-%->", "--&gt;")
return s
end
function Vtt.encode(subs: Types.Subtitles, opts: EncodeOptions?): (buffer?, Err.Err?)
if type(subs) ~= "table" or type(subs.cues) ~= "table" then
return nil, Err.new(Err.ARG, "needs a Subtitles value")
end
local meta: any = if type(subs.meta) == "table" then subs.meta else {}
local out = {}
local header = if type(meta.header) == "string" and meta.header ~= "" then meta.header else nil
if header then
local h = string.gsub(clean(header), "%-%-&gt;", "")
table.insert(out, "WEBVTT " .. h .. "\n")
else
table.insert(out, "WEBVTT\n")
end
for _, key in { "regions", "css" } do
local list = meta[key]
if type(list) == "table" then
for _, b in list do
if type(b) == "string" then
table.insert(out, (if key == "css" then "STYLE\n" else "REGION\n") .. clean(b) .. "\n")
end
end
end
end
for i, c in subs.cues do
if type(c) ~= "table" or type(c.start) ~= "number" or type(c.stop) ~= "number" or type(c.text) ~= "string" or c.start < 0 or c.stop < 0 then
return nil, Err.new(Err.ARG, "cue " .. i .. ": needs start / stop >= 0 (seconds) and a text string")
end
local id = if type(c.id) == "string" and c.id ~= "" and not string.find(c.id, "-->", 1, true) then (string.gsub(c.id, "[\r\n]", " ")) .. "\n" else ""
local settings = if type(c.settings) == "string" and c.settings ~= "" then " " .. (string.gsub(c.settings, "[\r\n]", " ")) else ""
local body = clean(c.text)
table.insert(out, id .. stamp(c.start) .. " --> " .. stamp(c.stop) .. settings .. "\n" .. body .. (if body ~= "" then "\n" else ""))
end
return buffer.fromstring(table.concat(out, "\n")), nil
end
return Vtt