subtitle formats: comments are specs only

This commit is contained in:
4DBug committed 2026-11-01 01:52:39 -05:00
1 parent 5d4addfc01
commit 6f8456246f
3 files changed
+19 -47

No files matched your search

+7 -21
View File
@@ -1,14 +1,7 @@
--!strict
-- subtitle/ass: Advanced SubStation Alpha (v4.00+) and SubStation Alpha (v4.00) scripts (the de-facto spec of
-- Aegisub / libass / ffmpeg). Supports: [Script Info] (meta.scriptInfo, in order), the [V4+ Styles] / [V4 Styles]
-- Format + Style lines (subs.styles[name] = { field = value } as strings, meta.styleFormat / meta.styleOrder), the
-- [Events] Format + Dialogue lines (cues with layer, name, margins, effect; Comment lines in meta.comments), any
-- other section kept verbatim (meta.sections: [Fonts], [Graphics], [Aegisub Project Garbage], ...), H:MM:SS.cc times.
-- Cue text: `\N` / `\n` become line breaks, `\h` a no-break space, the {\i1} / {\b1} / {\u1} / {\s1} overrides become
-- the shared <i> / <b> / <u> / <s> markup and other override blocks are dropped; the original text stays in cue.raw.
-- Encode: an ASS v4.00+ script (or the source's style / event formats), cue.raw when cue.text still matches it,
-- otherwise the markup converted back; times rounded to the centisecond. Styles are data: nothing is rendered. Not supported:
-- rendering, karaoke timing (\k overrides are dropped from text, kept in cue.raw), embedded font / graphic decoding.
-- subtitle/ass: ASS (v4.00+) / SSA (v4.00) scripts (de-facto spec of Aegisub / libass / ffmpeg). Supports: Script
-- Info, Styles, Events (Dialogue / Comment), other sections verbatim, H:MM:SS.cc; {\i1} {\b1} {\u1} {\s1} as the shared
-- markup (cue.raw keeps the original). Not supported: rendering, karaoke timing (\k), embedded font / graphic decoding.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -27,10 +20,10 @@ local Ass = {
}
export type DecodeOptions = {
maxBytes: number?, -- refuse larger files (default 64 MB)
maxBytes: number?, -- default 64 MB
}
export type EncodeOptions = {
crlf: boolean?, -- CRLF line ends (default false)
crlf: boolean?, -- default false
}
-- ------------------------------------------------------------------------------------------------ constants
@@ -63,7 +56,6 @@ local STYLE_DEFAULT: { [string]: string } = {
Encoding = "0",
}
local EVENT_FORMAT = { "Layer", "Start", "End", "Style", "Name", "MarginL", "MarginR", "MarginV", "Effect", "Text" }
-- event fields -> cue keys (the rest keep their field name)
local EVENT_KEY = { Layer = "layer", Name = "name", Actor = "name", MarginL = "marginL", MarginR = "marginR", MarginV = "marginV", Effect = "effect", Marked = "marked" }
local function trim(s: string): string
@@ -78,7 +70,6 @@ local function fields(s: string): { string }
return out
end
-- H:MM:SS.cc (also more / fewer fraction digits) -> seconds | nil
local function time(s: string): number?
local h, m, sec, f = string.match(s, "^%s*(%d+):(%d+):(%d+)%.?(%d*)%s*$")
if not h then
@@ -88,7 +79,6 @@ local function time(s: string): number?
return tonumber(h) :: number * 3600 + tonumber(m) :: number * 60 + tonumber(sec) :: number + fr
end
-- ASS text -> the shared markup
local TAGS = { i = "i", b = "b", u = "u", s = "s" }
function Ass.toMarkup(raw: string): string
local open: { [string]: boolean } = {}
@@ -129,7 +119,6 @@ function Ass.toMarkup(raw: string): string
return out
end
-- the shared markup -> ASS text
function Ass.fromMarkup(text: string): string
local s = string.gsub(text, "\r\n?", "\n")
s = string.gsub(s, "<(/?)(%a+)[^>]*>", function(close: string, tag: string): string
@@ -192,7 +181,7 @@ function Ass.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Subtitles?, E
end
if section == "script info" then
if string.sub(line, 1, 1) == ";" or string.sub(line, 1, 1) == "!" then
table.insert(infoOrder, line) -- a comment line, kept in place
table.insert(infoOrder, line) -- `;` / `!` comment line, kept in place
else
local k, v = string.match(line, "^([^:]+):%s?(.*)$")
if k then
@@ -265,7 +254,7 @@ function Ass.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Subtitles?, E
end
if ok then
if k == "Comment" then
cue.after = #subs.cues -- written back after that many dialogue lines
cue.after = #subs.cues
table.insert(comments, cue)
else
table.insert(subs.cues, cue :: any)
@@ -286,7 +275,6 @@ function Ass.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Subtitles?, E
subs.meta.comments = comments
end
if #sections > 0 then
-- drop trailing blank lines (the writer adds its own)
for _, sec in sections do
while #sec.lines > 0 and string.match(sec.lines[#sec.lines], "^%s*$") do
table.remove(sec.lines)
@@ -336,7 +324,6 @@ function Ass.encode(subs: Types.Subtitles, opts: EncodeOptions?): (buffer?, Err.
end
table.sort(rest)
table.move(rest, 1, #rest, #out + 1, out)
-- styles
local ssa = type(info.ScriptType) == "string" and string.lower(info.ScriptType) == "v4.00"
local sfmt: { string } = if type(meta.styleFormat) == "table" then meta.styleFormat else STYLE_FORMAT
table.insert(out, "")
@@ -374,7 +361,6 @@ function Ass.encode(subs: Types.Subtitles, opts: EncodeOptions?): (buffer?, Err.
end
table.insert(out, "Style: " .. table.concat(vals, ","))
end
-- events
local efmt: { string } = if type(meta.eventFormat) == "table" then meta.eventFormat else EVENT_FORMAT
if efmt[#efmt] ~= "Text" then
return nil, Err.new(Err.ARG, "meta.eventFormat must end with Text")
+8 -15
View File
@@ -1,11 +1,7 @@
--!strict
-- subtitle/srt: SubRip text (the de-facto format; matches ffmpeg's srt demuxer / muxer). Supports: numbered or
-- unnumbered cues, `HH:MM:SS,mmm --> HH:MM:SS,mmm` timings (also `.` decimals and 1..3 fraction digits), the trailing
-- `X1:.. Y1:..` coordinates (kept in cue.settings), multi-line text with blank lines inside a cue (a blank line ends
-- the cue only before an index / timing line), BOM, CRLF / CR line ends. Text is kept as written (its <i> / <b> /
-- <u> / <s> / <font> tags included): the shared markup of Subtitles cue text.
-- Encode: cues renumbered from 1 in order, times rounded to the millisecond, opts.crlf for CRLF line ends.
-- Not supported: positioning beyond keeping the coordinates; the tags are not validated.
-- subtitle/srt: SubRip text (de-facto; matches ffmpeg's srt demuxer / muxer). Supports: numbered or unnumbered
-- cues, `,` / `.` decimals, X1/Y1 coordinates (cue.settings), inner blank lines, BOM, CRLF / CR; text kept as is.
-- Not supported: positioning beyond keeping the coordinates; tags are not validated.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -24,16 +20,15 @@ local Srt = {
}
export type DecodeOptions = {
maxBytes: number?, -- refuse larger files (default 64 MB)
maxBytes: number?, -- default 64 MB
}
export type EncodeOptions = {
crlf: boolean?, -- CRLF line ends (default false: LF)
crlf: boolean?, -- default false (LF)
}
-- ------------------------------------------------------------------------------------------------ constants
local TIMING = "^%s*(%d+):(%d%d?):(%d%d?)[,.](%d+)%s*%-%->%s*(%d+):(%d%d?):(%d%d?)[,.](%d+)%s*(.-)%s*$"
-- text of the reader's range: BOM stripped, line ends normalised to LF; nil when it holds NUL bytes (binary)
local function text(r: Bytes.Reader): string?
local s = buffer.readstring(r.buf, r.base, r.limit - r.base)
if string.find(s, "\0", 1, true) then
@@ -49,7 +44,6 @@ local function frac(digits: string): number
return (tonumber(digits) :: number) / 10 ^ #digits
end
-- timing(line) -> start, stop, settings | nil
local function timing(line: string): (number?, number?, string?)
local h1, m1, s1, f1, h2, m2, s2, f2, rest = string.match(line, TIMING)
if not h1 then
@@ -88,7 +82,7 @@ function Srt.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Subtitles?, E
local n = #lines
local subs = Types.Subtitles.new()
local i = 1
local function isStart(k: number): boolean -- a cue starts at line k (index + timing, or a timing)
local function isStart(k: number): boolean
local l = lines[k]
if l == nil then
return false
@@ -112,7 +106,7 @@ function Srt.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Subtitles?, E
while i <= n do
local l = lines[i]
if string.match(l, "^%s*$") then
-- blank: the end of the cue unless more text follows before the next cue
-- a blank line ends the cue only before the next index / timing line
local k = i
while k <= n and string.match(lines[k], "^%s*$") do
k += 1
@@ -125,7 +119,7 @@ function Srt.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Subtitles?, E
end
i = k
elseif timing(l) then
break -- a cue without the blank separator
break
else
table.insert(body, l)
i += 1
@@ -167,7 +161,6 @@ function Srt.encode(subs: Types.Subtitles, opts: EncodeOptions?): (buffer?, Err.
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
-- blank lines inside the text are fine: the decoder ends a cue at a blank line only before the next cue
local body = string.gsub(c.text, "\r\n?", "\n")
local settings = if type(c.settings) == "string" and c.settings ~= "" and string.match(c.settings, "X1:") then " " .. c.settings else ""
out[i] = i .. nl .. stamp(c.start) .. " --> " .. stamp(c.stop) .. settings .. nl .. (string.gsub(body, "\n", nl)) .. nl
+4 -11
View File
@@ -1,10 +1,7 @@
--!strict
-- subtitle/vtt: WebVTT (W3C WebVTT, 2019 CR). Supports: the WEBVTT header (its trailing text and header lines kept in
-- meta.header), cue identifiers, `[hh:]mm:ss.ttt` timings, cue settings (cue.settings, as written), NOTE comments
-- (meta.notes), STYLE blocks (meta.css) and REGION blocks (meta.regions); BOM, CRLF / CR line ends. Cue text is kept
-- as written (<i> / <b> / <c.class> / <v Name> spans, timestamps, entities).
-- Encode: the header, STYLE / REGION blocks, then the cues (times to the millisecond); blank lines inside cue text
-- are dropped and `-->` escaped (WebVTT cue text cannot hold them).
-- 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")
@@ -23,7 +20,7 @@ local Vtt = {
}
export type DecodeOptions = {
maxBytes: number?, -- refuse larger files (default 64 MB)
maxBytes: number?, -- default 64 MB
}
export type EncodeOptions = {}
@@ -32,7 +29,6 @@ local function frac(digits: string): number
return (tonumber(digits) :: number) / 10 ^ #digits
end
-- one timestamp at the start of s: seconds, the rest | nil
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
@@ -45,7 +41,6 @@ local function timestamp(s: string): (number?, string)
return nil, s
end
-- timing(line) -> start, stop, settings | nil
local function timing(line: string): (number?, number?, string?)
local a, rest = timestamp((string.gsub(line, "^%s+", "")))
if not a then
@@ -110,7 +105,6 @@ function Vtt.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Subtitles?, E
i += 1
continue
end
-- a block: lines up to the next blank
local first = i
while i <= n and lines[i] ~= "" do
i += 1
@@ -160,7 +154,6 @@ local function stamp(t: number): string
return string.format("%02d:%02d:%02d.%03d", ms // 3600000, ms // 60000 % 60, ms // 1000 % 60, ms % 1000)
end
-- one block's text: no blank lines (they end a block), no `-->`
local function clean(s: string): string
s = string.gsub(s, "\r\n?", "\n")
s = string.gsub(s, "\n[ \t]*\n", "\n")