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
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
"""An attachment with no given MIME type takes it from the file signature before the suffix."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
from collections.abc import Iterator
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.tracks.attachment import Attachment
|
||||
|
||||
downloader = importlib.import_module("unshackle.core.downloaders.requests")
|
||||
PNG = b"\x89PNG\r\n\x1a\n" + bytes(32)
|
||||
JPEG = b"\xff\xd8\xff\xe0" + bytes(32)
|
||||
|
||||
|
||||
def test_a_png_saved_as_jpg_is_image_png(tmp_path: Path) -> None:
|
||||
path = tmp_path / "thumb.jpg"
|
||||
path.write_bytes(PNG)
|
||||
assert Attachment(path=path).mime_type == "image/png"
|
||||
|
||||
|
||||
def test_an_unknown_signature_falls_back_to_the_suffix(tmp_path: Path) -> None:
|
||||
path = tmp_path / "font.ttf"
|
||||
path.write_bytes(b"junk" * 8)
|
||||
assert Attachment(path=path).mime_type == "application/x-truetype-font"
|
||||
|
||||
|
||||
def test_a_given_mime_type_stays(tmp_path: Path) -> None:
|
||||
path = tmp_path / "thumb.jpg"
|
||||
path.write_bytes(PNG)
|
||||
assert Attachment(path=path, mime_type="image/jpeg").mime_type == "image/jpeg"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def png_download(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
def fake_download(url: str, path: Path, **kwargs: object) -> Iterator[dict]:
|
||||
path.write_bytes(PNG)
|
||||
yield {}
|
||||
|
||||
monkeypatch.setattr(config.directories, "temp", tmp_path)
|
||||
monkeypatch.setattr(downloader, "download", fake_download)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("png_download")
|
||||
@pytest.mark.parametrize("mime_type", [None, "image/jpeg"])
|
||||
def test_a_url_attachment_takes_the_type_of_the_downloaded_file(mime_type: str | None) -> None:
|
||||
"""The suffix type also arrives as a given type when a remote server sends the attachment."""
|
||||
attachment = Attachment(url="https://cdn.example/thumb.jpg", mime_type=mime_type)
|
||||
attachment.download()
|
||||
assert attachment.mime_type == "image/png"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("png_download")
|
||||
def test_a_url_attachment_keeps_a_given_type_that_is_not_the_suffix_type() -> None:
|
||||
attachment = Attachment(url="https://cdn.example/thumb.jpg", mime_type="image/x-example")
|
||||
attachment.download()
|
||||
assert attachment.mime_type == "image/x-example"
|
||||
Reference in New Issue
Block a user