diff --git a/main.py b/main.py index 2777905..a3c519d 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ import argparse import re import sys import threading +import uuid from concurrent.futures import ThreadPoolExecutor, as_completed from scenedetect import open_video, SceneManager from scenedetect.detectors import ContentDetector @@ -836,7 +837,23 @@ def auto_caption_clip(clip_path, transcript, clip_start, clip_end): # download time. A legacy clip whose name predates that budget can still # overflow — that raises OSError 36, which the except below turns into # "ship the clip uncaptioned" rather than a broken filename. - ass_path = os.path.join(output_dir, f"autosubs_{generation_id}_{stem}.ass") + # The .ass path is interpolated INTO an ffmpeg filter string + # (-vf ass='...'), where a literal apostrophe closes the quote and + # breaks the filter. Titles carry apostrophes constantly in English + # ("Earth's", "Don't"), so this name must stay free of the clip stem — + # which is exactly why /api/subtitle has always used a neutral + # "subs__.ass". Deriving it from the stem silently cost captions + # on every apostrophe title until 29-jul-2026. + # + # The OUTPUT name still carries the stem, and must: the modal's + # walk-back and _canonical_clip_file reconstruct the clean original + # from it. That one is only ever passed as an argv element, never + # inside a filter string, so quoting never applies to it. + # Unique per clip, not just per second: clips render in parallel + # (CLIP_WORKERS), so a bare timestamp would collide and let one clip + # burn another's captions. + ass_path = os.path.join( + output_dir, f"autosubs_{generation_id}_{uuid.uuid4().hex[:8]}.ass") out_path = os.path.join(output_dir, f"subtitled_{generation_id}_{stem}") if not _subs.generate_ass( diff --git a/subtitles.py b/subtitles.py index 9da7391..aa99cd2 100644 --- a/subtitles.py +++ b/subtitles.py @@ -88,7 +88,19 @@ def _log(message): def _escape_ffmpeg_filter_value(value): - """Escape a path/value for use inside a quoted FFmpeg filter argument.""" + """Escape a path/value for use inside a quoted FFmpeg filter argument. + + NOTE: an apostrophe in the path cannot be made safe here. ffmpeg's + filtergraph parser is not a shell — the shell idiom ``'\\''`` was tried on + 29-jul-2026 and is worse than doing nothing: it drops the apostrophe AND + swallows the following option, so ``ass='…Earth'\\''s.ass':fontsdir='…'`` + resolved to a filename of "…Earths.ass:fontsdir=…" and failed to open. + + The only reliable answer is to keep apostrophes OUT of any path that is + interpolated into a filter. Callers generate their own subtitle filenames, + so they control this: use a neutral name (``subs__.ass``), never one + derived from a video title. + """ return value.replace('\\', '/').replace(':', '\\:').replace("'", "\\'") diff --git a/tests/test_subtitles.py b/tests/test_subtitles.py index 6e26652..de710ce 100644 --- a/tests/test_subtitles.py +++ b/tests/test_subtitles.py @@ -289,3 +289,44 @@ class TestAutoCaptionDefaults: @staticmethod def _t(words): return {"segments": [{"words": words}]} + + +class TestFilterQuoting: + """Paths are interpolated INTO a single-quoted ffmpeg filter argument. + + Regression cover for captions silently failing in prod on 29-jul-2026: a + clip named "Inside Earth's Most Mysterious Temple" produced an .ass path + carrying that apostrophe, which ends the quoted argument early and kills + the burn. Apostrophes are constant in English titles. + + The fix is NOT smarter escaping. The shell idiom "'\\''" was tried and is + worse — ffmpeg's filtergraph parser is not a shell, so it dropped the + apostrophe and swallowed the following ":fontsdir=" option into the + filename. The fix is to keep apostrophes out of filter paths entirely. + """ + + def test_generated_subtitle_paths_carry_no_apostrophe(self): + # Both generators must name their own file, never derive it from a + # video title. This is the property that actually prevents the bug. + import re + src = open("main.py").read() + m = re.search(r'ass_path = os\.path\.join\(\s*output_dir,\s*f"([^"]+)"', src) + assert m, "auto-caption .ass path not found" + assert "{stem}" not in m.group(1), ( + f"auto-caption .ass name derives from the clip stem: {m.group(1)}") + + def test_auto_caption_ass_name_is_unique_per_clip(self): + # Clips render in parallel; a bare timestamp collides and lets one clip + # burn another's captions. + import re + src = open("main.py").read() + m = re.search(r'ass_path = os\.path\.join\(\s*output_dir,\s*f"([^"]+)"', src) + assert "uuid" in m.group(1), f"not unique per clip: {m.group(1)}" + + def test_colon_is_escaped(self): + from subtitles import _escape_ffmpeg_filter_value + assert "\\:" in _escape_ffmpeg_filter_value("C:/out/subs.ass") + + def test_plain_path_untouched(self): + from subtitles import _escape_ffmpeg_filter_value + assert _escape_ffmpeg_filter_value("/out/subs_0_123.ass") == "/out/subs_0_123.ass"