fix(tracks): retry ccextractor on the repackaged file

This commit is contained in:
imSp4rky
2026-10-31 12:21:19 -06:00
parent cbeff379fb
commit f2747076a6
2 changed files with 46 additions and 4 deletions
+41
View File
@@ -0,0 +1,41 @@
"""Tests for the repackaged-copy retry in ``Video.ccextractor``."""
from __future__ import annotations
import subprocess
from pathlib import Path
from typing import Any
import pytest
from langcodes import Language
from unshackle.core.tracks import Video
from unshackle.core.tracks import video as video_module
def test_retry_runs_on_the_repackaged_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
original = tmp_path / "v.mp4"
original.write_bytes(b"x")
repacked = tmp_path / "v_repack.mp4"
track = Video(id_="v", url="https://example.test/v.m3u8", language="en", codec=Video.Codec.AVC)
track.path = original
def fake_repackage() -> None:
original.unlink()
repacked.write_bytes(b"x")
track.path = repacked
inputs: list[Path] = []
def fake_run(args: list[Any], **_: Any) -> None:
inputs.append(Path(args[-1]))
if not Path(args[-1]).exists():
raise subprocess.CalledProcessError(2, args) # ccextractor: no input file
raise subprocess.CalledProcessError(10, args) # ccextractor: no captions found
monkeypatch.setattr(video_module.binaries, "CCExtractor", Path("/usr/bin/ccextractor"))
monkeypatch.setattr(video_module.subprocess, "run", fake_run)
monkeypatch.setattr(track, "repackage", fake_repackage)
assert track.ccextractor("cc", tmp_path / "cc.srt", Language.get("en")) is None
assert inputs == [original, repacked]
+5 -4
View File
@@ -655,9 +655,9 @@ class Video(Track):
raise EnvironmentError("ccextractor executable was not found.")
out_path = Path(out_path)
video_path, ccextractor = self.path, binaries.CCExtractor
ccextractor = binaries.CCExtractor
def run_ccextractor(fallback: str) -> bool:
def run_ccextractor(video_path: Path, fallback: str) -> bool:
cc_start = time.monotonic()
try:
subprocess.run(
@@ -692,9 +692,10 @@ class Video(Track):
# Try on the original file first (preserves container-level CC data like c608 boxes),
# then fall back to repacked file (ccextractor can fail on some container formats).
if not run_ccextractor("retrying on a repackaged copy"):
if not run_ccextractor(self.path, "retrying on a repackaged copy"):
# repackage() deletes the original file and moves self.path to the repacked copy
self.repackage()
run_ccextractor("skipping CC extraction")
run_ccextractor(self.path, "skipping CC extraction")
if out_path.exists():
cc_track = Subtitle(