From 7e87a8a9235602e009f00e4b17db2e7306268a12 Mon Sep 17 00:00:00 2001 From: Andrew Parsons Date: Fri, 16 Oct 2026 20:01:03 -0700 Subject: [PATCH 1/2] fix: escape '%' in the output directory for ffmpeg frame patterns ffmpeg's image2 muxer parses every '%' in the output path as part of the sequence pattern. A work dir named after a video such as "Get Ahead of 95% in 11 Minutes" made ffmpeg write a single literal frame_%04d.jpg (or fail), so no frames were extracted. All three frame_%04d.jpg call sites now go through _frame_pattern(), which escapes '%' as '%%'. Adds a regression test across the keyframe and scene/uniform engines. Co-Authored-By: Claude Opus 5.5 --- skills/watch/scripts/frames.py | 12 +++++++++--- tests/test_frames.py | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/skills/watch/scripts/frames.py b/skills/watch/scripts/frames.py index d8194e74..7067dd93 100755 --- a/skills/watch/scripts/frames.py +++ b/skills/watch/scripts/frames.py @@ -22,6 +22,12 @@ MAX_FPS = 2.0 SCENE_THRESHOLD = 0.20 + + +def _frame_pattern(out_dir: Path) -> str: + """ffmpeg's image2 muxer treats every '%' in the path as a pattern token, so a + literal '%' in the directory (e.g. a video titled "95%") must be '%%'.""" + return str(out_dir.resolve()).replace("%", "%%") + "/frame_%04d.jpg" # Keep scene-detection results once we have at least this many distinct shots. # Below this the video is effectively static (screen recording, talking head), # so we fall back to uniform sampling. Matching the reference fork's behaviour, @@ -218,7 +224,7 @@ def extract( "ffmpeg", "-hide_banner", "-loglevel", "info", "-y", "-ss", f"{start:.6f}", "-to", f"{end:.6f}", "-i", str(Path(video_path).resolve()), "-vf", vf, *sync_args(), "-frames:v", str(max_frames), "-q:v", "4", - str(out_dir / "frame_%04d.jpg"), + _frame_pattern(out_dir), ]) if result.returncode != 0: raise SystemExit(f"ffmpeg frame extraction failed: {diagnostic(result.stderr)}") @@ -252,7 +258,7 @@ def extract_scene_candidates( for existing in out_dir.glob("frame_*.jpg"): existing.unlink() - output_pattern = str(out_dir / "frame_%04d.jpg") + output_pattern = _frame_pattern(out_dir) cmd: list[str] = [ "ffmpeg", "-hide_banner", @@ -606,7 +612,7 @@ def extract_keyframes( for existing in out_dir.glob("frame_*.jpg"): existing.unlink() - output_pattern = str(out_dir / "frame_%04d.jpg") + output_pattern = _frame_pattern(out_dir) cmd: list[str] = [ "ffmpeg", "-hide_banner", diff --git a/tests/test_frames.py b/tests/test_frames.py index a5e8b3b1..9ef92c4a 100644 --- a/tests/test_frames.py +++ b/tests/test_frames.py @@ -140,3 +140,19 @@ def test_single_frame_cap_keeps_first_real_frame(static_clip, tmp_path): def test_uniform_candidate_count_includes_deduplicated_frames(static_clip, tmp_path): out, meta = frames.extract_scene_or_uniform(str(static_clip), tmp_path / 'f', fps=2, target_frames=6) assert meta['candidate_count'] == len(out) + meta['deduped_count'] + + +@pytest.mark.parametrize("engine", ["keyframe", "scene_or_uniform"]) +@pytest.mark.parametrize("clip", ["cut_clip", "static_clip"]) +def test_percent_in_output_dir_is_literal(engine, clip, tmp_path, request): + # A folder named after a video like "Get Ahead of 95% in 11 Minutes" must not be + # read as part of ffmpeg's frame_%04d.jpg sequence pattern. + out_dir = tmp_path / "Ahead of 95% in 11 Minutes" / "f" + source = str(request.getfixturevalue(clip)) + if engine == "keyframe": + out, _ = frames.extract_keyframes(source, out_dir, max_frames=5) + else: + out, _ = frames.extract_scene_or_uniform(source, out_dir, fps=2.0, target_frames=5, max_frames=5) + files = sorted(out_dir.glob("frame_*.jpg")) + assert out and len(files) == len(out) + assert not (out_dir / "frame_%04d.jpg").exists() From ada62cf2a86ce09fd358074c95bdc821342c16c2 Mon Sep 17 00:00:00 2001 From: Andrew Parsons Date: Sun, 18 Oct 2026 17:26:43 -0700 Subject: [PATCH 2/2] fix: pick the metadata-language original when several -orig tracks exist select_caption() replaced any auto-mode match with originals[0], the alphabetically first '-orig' track. Some YouTube videos list more than one original ASR track (youtu.be/yf2OwchM9nw, an en-US video, offers both ar-orig and en-orig), so watch returned an Arabic transcript. With several originals, prefer the one matching the metadata language, else English-first. A single original still overrides dubbed metadata, as before. Adds a regression test. Co-Authored-By: Claude Opus 5.5 --- skills/watch/scripts/download.py | 10 +++++++++- tests/test_download.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/skills/watch/scripts/download.py b/skills/watch/scripts/download.py index fc8d0ff8..92729514 100755 --- a/skills/watch/scripts/download.py +++ b/skills/watch/scripts/download.py @@ -115,8 +115,16 @@ def english_first(key): # Unknown source language: prefer English over alphabet if keys: key = keys[0] # Auto mode must prefer original ASR to translated/dubbed metadata. + # With several originals (e.g. ar-orig and en-orig), take the one matching the + # metadata language, else English-first; never just the alphabetically first. if kind == 'automatic' and language == 'auto' and originals: - key = originals[0] + preferred = [k for k in originals if matches(k)] if target else [] + if preferred: + key = min(preferred, key=english_first) + elif len(originals) == 1: + key = originals[0] + elif not target: + key = min(originals, key=english_first) base = key.removesuffix('-orig') provenance = 'original' if original and base.split('-')[0] == original.split('-')[0] else 'unknown' if language != 'auto' and original and base.split('-')[0] != original.split('-')[0]: diff --git a/tests/test_download.py b/tests/test_download.py index 545ccbc2..87a0ed1a 100644 --- a/tests/test_download.py +++ b/tests/test_download.py @@ -155,3 +155,15 @@ def stages(cmd): def test_explicit_regional_language_is_not_silently_replaced(): assert download.select_caption({'subtitles': _tracks('en-US')}, 'en-GB') is None assert download.select_caption({'subtitles': _tracks('en-US')}, 'en')['key'] == 'en-US' + + +def test_multiple_originals_follow_metadata_language(): + # Seen on youtu.be/yf2OwchM9nw: an en-US video whose automatic captions list both + # ar-orig and en-orig. Alphabetical order must not pick the Arabic track. + info = {'extractor_key': 'Youtube', 'language': 'en-US', + 'automatic_captions': _tracks('ar', 'ar-orig', 'en', 'en-orig', 'fr')} + assert download.select_caption(info)['key'] == 'en-orig' + info['language'] = None + assert download.select_caption(info)['key'] == 'en-orig' # unknown language: English first + info['language'] = 'ar' + assert download.select_caption(info)['key'] == 'ar-orig'