"""Repackaging an MPEG-TS with ADTS AAC into MP4 runs one clean FFmpeg pass.""" from __future__ import annotations import json import subprocess from collections.abc import Callable, Iterator from pathlib import Path import pytest from langcodes import Language from unshackle.core import binaries from unshackle.core.tracks import Video from unshackle.core.utilities import close_debug_logger, init_debug_logger pytestmark = pytest.mark.skipif(binaries.FFMPEG is None, reason="requires ffmpeg") @pytest.fixture def tool_run_levels(tmp_path: Path) -> Iterator[Callable[[], list[str]]]: """Enable the debug log; the fixture value reads the levels of its tool_run records.""" log = tmp_path / "debug.jsonl" init_debug_logger(log_path=log, enabled=True) def levels() -> list[str]: close_debug_logger() entries = [json.loads(line) for line in log.read_text(encoding="utf-8").splitlines()] return [e["level"] for e in entries if e["operation"] == "tool_run"] yield levels close_debug_logger() def video_at(path: Path) -> Video: video = Video(url="https://cdn.example/v.mp4", language=Language.get("en")) video.path = path return video def test_adts_aac_repackages_in_one_pass_with_no_error_record( tmp_path: Path, tool_run_levels: Callable[[], list[str]] ) -> None: source = tmp_path / "v.mp4" subprocess.run( [str(binaries.FFMPEG), "-nostdin", "-v", "error", "-y"] + ["-f", "lavfi", "-i", "testsrc2=size=160x90:rate=24:duration=1", "-f", "lavfi", "-i", "sine=duration=1"] + ["-c:v", "mpeg4", "-c:a", "aac", "-f", "mpegts", str(source)], check=True, ) video = video_at(source) video.repackage() assert video.path and video.path.stat().st_size > 0 assert tool_run_levels() == ["DEBUG"] def test_a_real_repackage_failure_still_logs_an_error(tmp_path: Path, tool_run_levels: Callable[[], list[str]]) -> None: source = tmp_path / "v.mp4" source.write_bytes(b"not media") with pytest.raises(subprocess.CalledProcessError): video_at(source).repackage() assert tool_run_levels() == ["ERROR"]