mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-03 06:25:10 +00:00
fix(tracks): sniff attachment MIME from file bytes and keep ffmpeg autobsf on repack
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
"""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"]
|
||||
@@ -107,24 +107,6 @@ def test_a_repackage_failure_names_the_ffmpeg_reason(tmp_path: Path, monkeypatch
|
||||
track.repackage()
|
||||
|
||||
|
||||
@POSIX_STUBS
|
||||
def test_a_malformed_aac_repackage_retries_with_the_adts_filter(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
script = (
|
||||
'case "$*" in *aac_adtstoasc*) for last; do :; done; echo ok > "$last"; exit 0;; esac\n'
|
||||
"echo 'Malformed AAC bitstream detected' >&2; exit 1"
|
||||
)
|
||||
monkeypatch.setattr(binaries, "FFMPEG", stub(tmp_path, "ffmpeg", script))
|
||||
track = Audio("http://example.test/a.mp4", "en", codec=Audio.Codec.AAC)
|
||||
track.path = tmp_path / "a.mp4"
|
||||
track.path.write_bytes(b"audio")
|
||||
|
||||
track.repackage()
|
||||
|
||||
assert track.path == tmp_path / "a_repack.mp4" and track.path.read_text() == "ok\n"
|
||||
|
||||
|
||||
@POSIX_STUBS
|
||||
def test_a_subtitleedit_failure_names_the_reason_it_wrote_to_stdout(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
|
||||
Reference in New Issue
Block a user