subtitle formats: comments are specs only
This commit is contained in:
1 parent
5d4addfc01
commit
6f8456246f
3 files changed
+19
-47
No files matched your search
@@ -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")
|
||||
|
||||
Reference in new issue
Block a user