# Subtitle fixtures (tests/gen/subtitles.luau, for tests/subtitle/srt.luau, vtt.luau and ass.luau) with # ffmpeg as the reference: a random SRT script converted by ffmpeg to SRT / WebVTT / ASS, and hand-written WebVTT / # ASS / SSA files with their features, converted by ffmpeg to SRT. Every case carries the expected cues # ({ start_ms, stop_ms, plain text }) so the tests compare our decode with ffmpeg's view of the same file. import os, random, re, shutil, subprocess, tempfile WORDS = ["alpha", "beta", "gamma", "delta", "omega", "café", "naïve", "日本", "fox", "dog", "42", "hello", "world"] def ffmpeg(ctx, src_bytes, src_ext, dst_ext): with tempfile.TemporaryDirectory() as d: src = os.path.join(d, "in." + src_ext) dst = os.path.join(d, "out." + dst_ext) open(src, "wb").write(src_bytes) argv = ["ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-i", src, dst] if shutil.which("ffmpeg"): subprocess.run(argv, check=True, capture_output=True) else: ctx.nix("ffmpeg", argv) return open(dst, "rb").read() def plain(s): return re.sub(r"{[^}]*}", "", re.sub(r"<[^>]*>", "", s)).replace("&", "&") def srt_time(ms): return "%02d:%02d:%02d,%03d" % (ms // 3600000, ms // 60000 % 60, ms // 1000 % 60, ms % 1000) def random_srt(rng, n): cues, t = [], 500 for i in range(n): start = t + rng.randrange(0, 3000) stop = start + rng.randrange(300, 5000) t = stop lines = [] for _ in range(rng.randrange(1, 3)): words = [rng.choice(WORDS) for _ in range(rng.randrange(1, 6))] if rng.random() < 0.3: k = rng.randrange(len(words)) tag = rng.choice("ibu") words[k] = "<%s>%s" % (tag, words[k], tag) lines.append(" ".join(words)) cues.append((start, stop, "\n".join(lines))) text = "".join("%d\n%s --> %s\n%s\n\n" % (i + 1, srt_time(a), srt_time(b), t) for i, (a, b, t) in enumerate(cues)) return text.encode(), [{"start": a, "stop": b, "plain": plain(t)} for a, b, t in cues] VTT = """WEBVTT - a test file Kind: captions STYLE ::cue { color: yellow } REGION id:r1 width:40% NOTE this is a comment over two lines intro 00:01.000 --> 00:04.500 align:start position:10% Hello there 00:05.250 --> 00:07.000 first line second line 01:00:02.000 --> 01:00:03.123 line:0 an hour in & more """ ASS = """[Script Info] ; Script generated by hand Title: test ScriptType: v4.00+ PlayResX: 640 PlayResY: 480 [V4+ Styles] Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding Style: Default,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1 Style: Top,Verdana,24,&H0000FFFF,&H000000FF,&H00000000,&H80000000,-1,0,0,0,100,100,0,0,1,2,0,8,10,10,10,1 [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:01.00,0:00:03.50,Default,Bob,0,0,0,,Hello, {\\i1}world{\\i0}! Comment: 0,0:00:02.00,0:00:04.00,Default,,0,0,0,,a comment line Dialogue: 1,0:00:04.20,0:00:06.00,Top,,0,0,0,,{\\an8\\b1}Bold top{\\b0}\\Nsecond line Dialogue: 0,0:01:02.03,0:01:05.99,Default,,0,0,0,,{\\pos(10,20)}commas, inside, the text [Fonts] fontname: x.ttf ABCDEF """ SSA = """[Script Info] Title: old ScriptType: v4.00 [V4 Styles] Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding Style: Default,Tahoma,24,16777215,65535,65535,-2147483640,-1,0,1,3,0,2,30,30,30,0,0 [Events] Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: Marked=0,0:00:00.50,0:00:02.00,Default,,0000,0000,0000,,old style Dialogue: Marked=0,0:00:02.50,0:00:04.25,Default,,0000,0000,0000,,{\\i1}two{\\i0}\\Nlines """ def parse_srt(data): """the cues of an SRT (ffmpeg's output) -> [{start, stop, plain}]""" out = [] text = data.decode("utf-8-sig").replace("\r\n", "\n") for block in re.split(r"\n\s*\n", text.strip()): lines = block.split("\n") for k, l in enumerate(lines): m = re.match(r"(\d+):(\d+):(\d+)[,.](\d+) --> (\d+):(\d+):(\d+)[,.](\d+)", l) if m: g = [int(x) for x in m.groups()] out.append({"start": ((g[0] * 60 + g[1]) * 60 + g[2]) * 1000 + g[3], "stop": ((g[4] * 60 + g[5]) * 60 + g[6]) * 1000 + g[7], "plain": plain("\n".join(lines[k + 1:]))}) break return out def generate(ctx): rng = random.Random(7) src, cues = random_srt(rng, 40) out = {"srt": src, "cues": cues} for ext in ("srt", "vtt", "ass"): out["ffmpeg_" + ext] = ffmpeg(ctx, src, "srt", ext) hand = [] for name, ext, data in (("vtt", "vtt", VTT), ("ass", "ass", ASS), ("ssa", "ssa", SSA)): b = data.encode() ref = ffmpeg(ctx, b, ext, "srt") hand.append({"name": name, "data": b, "ffmpeg_srt": ref, "cues": parse_srt(ref)}) out["hand"] = hand ctx.emit("subtitles", out)