mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-16 05:55:09 +00:00
H.266 is remuxed to Matroska with FFmpeg 8+ before mkvmerge, named H.266 and selectable with -v h266; MP4Box names TS inputs correctly, TS track files keep .ts, and core no longer runs an Audio Vivid decoder.
363 lines
15 KiB
Python
363 lines
15 KiB
Python
"""Container selection for audio Matroska cannot name.
|
|
|
|
DTS:X ships in two shapes. Profile 2 (DTS-UHD, sample entry dtsx/dtsy) has no Matroska
|
|
CodecID, so mkvmerge stores it as an A_QUICKTIME passthrough and no reader can name the
|
|
codec; those titles have to go to MP4. Profile 1 rides inside DTS-HD Master Audio
|
|
(dtsc/dtse/dtsh/
|
|
dtsl), which Matroska maps to A_DTS and stores properly, so it must stay in Matroska.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import struct
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from unshackle.core import binaries
|
|
from unshackle.core.tracks import Audio, Subtitle, Tracks, Video
|
|
from unshackle.core.tracks.track import (
|
|
fix_data_reference_indexes,
|
|
has_mp4_only_audio_entry,
|
|
iter_top_level_boxes,
|
|
strip_duplicate_init_boxes,
|
|
)
|
|
|
|
|
|
def box(box_type: bytes, payload: bytes = b"") -> bytes:
|
|
return struct.pack(">I", 8 + len(payload)) + box_type + payload
|
|
|
|
|
|
def write_mp4(path: Path, sample_entry: bytes) -> Path:
|
|
"""An MP4 whose moov holds one audio sample entry of the given 4CC."""
|
|
entry = box(sample_entry, b"\x00" * 20)
|
|
path.write_bytes(box(b"ftyp", b"isom") + box(b"moov", box(b"trak", box(b"stsd", entry))))
|
|
return path
|
|
|
|
|
|
@pytest.mark.parametrize("fourcc", [b"dtsx", b"dtsy", b"mha1", b"mha2", b"mhm1", b"mhm2"])
|
|
def test_dts_uhd_and_mpeg_h_sample_entries_are_detected(tmp_path: Path, fourcc: bytes) -> None:
|
|
assert has_mp4_only_audio_entry(write_mp4(tmp_path / "a.mp4", fourcc)) is True
|
|
|
|
|
|
@pytest.mark.parametrize("fourcc", [b"dtsc", b"dtse", b"dtsh", b"dtsl", b"ec-3", b"mp4a"])
|
|
def test_other_dts_profiles_stay_in_matroska(tmp_path: Path, fourcc: bytes) -> None:
|
|
"""
|
|
DTS:X Profile 1 is DTS-HD Master Audio underneath and Matroska stores it as
|
|
A_DTS, so it must not be pulled out of Matroska by the DTS:X container rule.
|
|
"""
|
|
assert has_mp4_only_audio_entry(write_mp4(tmp_path / "a.mp4", fourcc)) is False
|
|
|
|
|
|
def test_a_file_with_no_moov_is_not_detected(tmp_path: Path) -> None:
|
|
path = tmp_path / "a.mp4"
|
|
path.write_bytes(box(b"ftyp", b"isom") + box(b"mdat", b"dtsx" * 8))
|
|
assert has_mp4_only_audio_entry(path) is False, "a 4CC in mdat is not a sample entry"
|
|
|
|
|
|
def build_tracks(tmp_path: Path, audio_entry: bytes) -> Tracks:
|
|
video = Video(id_="v1", url="https://x/v", codec=Video.Codec.AVC, language="en", width=1920, height=1080, fps=24)
|
|
audio = Audio(id_="a1", url="https://x/a", codec=Audio.Codec.DTSX, language="en", bitrate=448000)
|
|
video.path = write_mp4(tmp_path / "v.mp4", b"avc1")
|
|
audio.path = write_mp4(tmp_path / "a.mp4", audio_entry)
|
|
return Tracks([video, audio])
|
|
|
|
|
|
class FakePopen:
|
|
"""Stands in for MP4Box, recording the command it was given."""
|
|
|
|
stdout_text = ""
|
|
|
|
def __init__(self, args, **kwargs):
|
|
self.args = args
|
|
self.kwargs = kwargs
|
|
self.stdout = io.StringIO(FakePopen.stdout_text or "ISO File Writing: |== | (50/100)\n")
|
|
|
|
def wait(self) -> int:
|
|
return 0
|
|
|
|
|
|
def test_dts_uhd_audio_routes_the_mux_to_mp4(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
captured: list[FakePopen] = []
|
|
|
|
def fake_popen(args, **kwargs):
|
|
p = FakePopen(args, **kwargs)
|
|
captured.append(p)
|
|
Path(args[-1]).write_bytes(b"muxed")
|
|
return p
|
|
|
|
monkeypatch.setattr(binaries, "MP4Box", Path("MP4Box"), raising=False)
|
|
monkeypatch.setattr(subprocess, "Popen", fake_popen)
|
|
|
|
tracks = build_tracks(tmp_path, b"dtsx")
|
|
out, returncode, errors = tracks.mux("Title", delete=False, output_path=tmp_path / "out.muxed.mkv")
|
|
|
|
assert out.suffix == ".mp4", "a DTS-UHD title must not be delivered as Matroska"
|
|
assert returncode == 0 and not errors
|
|
command = captured[0].args
|
|
assert command[:2] == ["MP4Box", "-for-test"]
|
|
assert command[-2:] == ["-new", str(out)]
|
|
assert f"{tmp_path / 'a.mp4'}:lang=en" in command
|
|
# the child must not inherit the terminal that the live progress tree is drawing on
|
|
assert captured[0].kwargs["stdout"] is subprocess.PIPE
|
|
assert captured[0].kwargs["stderr"] is subprocess.STDOUT
|
|
|
|
|
|
def test_other_audio_still_muxes_with_mkvmerge(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(binaries, "MP4Box", None, raising=False)
|
|
monkeypatch.setattr("unshackle.core.tracks.tracks.binaries.MKVToolNix", None)
|
|
|
|
tracks = build_tracks(tmp_path, b"dtsh")
|
|
# reaching the mkvmerge requirement proves the DTS-UHD branch was not taken; MP4Box
|
|
# is None here too, so the MP4 branch would have raised about MP4Box instead
|
|
with pytest.raises(RuntimeError, match="MKVToolNix"):
|
|
tracks.mux("Title", delete=False, output_path=tmp_path / "out.muxed.mkv")
|
|
|
|
|
|
def test_a_missing_mp4box_names_the_binary(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(binaries, "MP4Box", None, raising=False)
|
|
tracks = build_tracks(tmp_path, b"dtsx")
|
|
with pytest.raises(RuntimeError, match="MP4Box"):
|
|
tracks.mux("Title", delete=False, output_path=tmp_path / "out.muxed.mkv")
|
|
|
|
|
|
def test_substation_alpha_subtitles_are_left_out(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""MP4 has no ASS format and tx3g would drop the styling that picked it."""
|
|
captured: list[FakePopen] = []
|
|
|
|
def fake_popen(args, **kwargs):
|
|
p = FakePopen(args, **kwargs)
|
|
captured.append(p)
|
|
Path(args[-1]).write_bytes(b"muxed")
|
|
return p
|
|
|
|
monkeypatch.setattr(binaries, "MP4Box", Path("MP4Box"), raising=False)
|
|
monkeypatch.setattr(subprocess, "Popen", fake_popen)
|
|
|
|
tracks = build_tracks(tmp_path, b"dtsx")
|
|
ass = Subtitle(id_="s1", url="https://x/s", codec=Subtitle.Codec.SubStationAlphav4, language="en")
|
|
ass.path = tmp_path / "s.ass"
|
|
ass.path.write_text("[Script Info]\n")
|
|
vtt = Subtitle(id_="s2", url="https://x/s2", codec=Subtitle.Codec.WebVTT, language="es")
|
|
vtt.path = tmp_path / "s.vtt"
|
|
vtt.path.write_text("WEBVTT\n")
|
|
tracks.add([ass, vtt])
|
|
|
|
tracks.mux("Title", delete=False, output_path=tmp_path / "out.muxed.mkv")
|
|
command = " ".join(captured[0].args)
|
|
assert "s.ass" not in command
|
|
assert "s.vtt:lang=es" in command
|
|
|
|
|
|
def test_mp4_subtitles_get_the_script_their_region_implies(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
captured: list[FakePopen] = []
|
|
|
|
def fake_popen(args: list[str], **kwargs: object) -> FakePopen:
|
|
p = FakePopen(args, **kwargs)
|
|
captured.append(p)
|
|
Path(args[-1]).write_bytes(b"muxed")
|
|
return p
|
|
|
|
monkeypatch.setattr(binaries, "MP4Box", Path("MP4Box"), raising=False)
|
|
monkeypatch.setattr(subprocess, "Popen", fake_popen)
|
|
|
|
tracks = build_tracks(tmp_path, b"dtsx")
|
|
vtt = Subtitle(id_="s1", url="https://x/s", codec=Subtitle.Codec.WebVTT, language="zh-TW")
|
|
vtt.path = tmp_path / "s.vtt"
|
|
vtt.path.write_text("WEBVTT\n")
|
|
tracks.add(vtt)
|
|
|
|
tracks.mux("Title", delete=False, output_path=tmp_path / "out.muxed.mkv")
|
|
command = captured[0].args
|
|
assert f"{tmp_path / 's.vtt'}:lang=zh-Hant-TW:tkhd=enable,movie,preview:ID=-1" in command
|
|
assert any(arg.endswith("=Chinese (Traditional)") for arg in command)
|
|
|
|
|
|
def test_mp4box_errors_come_back_to_the_caller(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""A failed mux does not raise; the caller inspects the return code and lines."""
|
|
|
|
class FailingPopen(FakePopen):
|
|
def __init__(self, args, **kwargs):
|
|
super().__init__(args, **kwargs)
|
|
self.stdout = io.StringIO("Error importing track: not supported\n")
|
|
|
|
def wait(self) -> int:
|
|
return 1
|
|
|
|
monkeypatch.setattr(binaries, "MP4Box", Path("MP4Box"), raising=False)
|
|
monkeypatch.setattr(subprocess, "Popen", FailingPopen)
|
|
|
|
tracks = build_tracks(tmp_path, b"dtsx")
|
|
_, returncode, errors = tracks.mux("Title", delete=False, output_path=tmp_path / "out.muxed.mkv")
|
|
assert returncode == 2, "MP4Box exits 1 on a failed import, and an exit with no output is no warning"
|
|
assert errors == ["Error importing track: not supported"]
|
|
|
|
|
|
def test_an_mp4box_warning_with_output_stays_a_warning(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
class WarningPopen(FakePopen):
|
|
def __init__(self, args, **kwargs):
|
|
super().__init__(args, **kwargs)
|
|
Path(args[-1]).write_bytes(b"muxed")
|
|
|
|
def wait(self) -> int:
|
|
return 1
|
|
|
|
monkeypatch.setattr(binaries, "MP4Box", Path("MP4Box"), raising=False)
|
|
monkeypatch.setattr(subprocess, "Popen", WarningPopen)
|
|
|
|
tracks = build_tracks(tmp_path, b"dtsx")
|
|
_, returncode, _ = tracks.mux("Title", delete=False, output_path=tmp_path / "out.muxed.mkv")
|
|
assert returncode == 1
|
|
|
|
|
|
def init_pair(sample_entry: bytes, stamp: bytes = b"\x00\x00\x00\x01") -> bytes:
|
|
"""An ftyp+moov pair; the stamp stands in for the fetch time a packager writes."""
|
|
stsd = box(b"stsd", box(sample_entry, b"\x00" * 20))
|
|
return box(b"ftyp", b"isom") + box(b"moov", box(b"mvhd", stamp) + box(b"trak", stsd))
|
|
|
|
|
|
def fragment(payload: bytes) -> bytes:
|
|
return box(b"moof", b"\x00" * 8) + box(b"mdat", payload)
|
|
|
|
|
|
def test_repeated_inits_are_dropped_when_the_stsd_matches(tmp_path: Path) -> None:
|
|
"""
|
|
An HLS discontinuity gives each period its own init, so a merged track holds several.
|
|
Only the stsd decides how samples decode, and a packager stamps each init with the
|
|
fetch time, so copies that differ only in that stamp are safe to drop.
|
|
"""
|
|
src = tmp_path / "a.mp4"
|
|
src.write_bytes(
|
|
init_pair(b"dtsx", b"\x00\x00\x00\x01")
|
|
+ fragment(b"one")
|
|
+ init_pair(b"dtsx", b"\x00\x00\x00\x99")
|
|
+ fragment(b"two")
|
|
)
|
|
dst = tmp_path / "clean.mp4"
|
|
|
|
assert strip_duplicate_init_boxes(src, dst) == 2, "the second ftyp and moov should be dropped"
|
|
types = [t for t, _, _ in iter_top_level_boxes(dst)]
|
|
assert types == [b"ftyp", b"moov", b"moof", b"mdat", b"moof", b"mdat"]
|
|
assert dst.read_bytes().count(b"mdat") == 2, "no sample data may be lost"
|
|
|
|
|
|
def test_a_changed_stsd_is_never_dropped(tmp_path: Path) -> None:
|
|
"""A later init describing a different format is a real change; dropping it corrupts."""
|
|
src = tmp_path / "a.mp4"
|
|
src.write_bytes(init_pair(b"dtsx") + fragment(b"one") + init_pair(b"ec-3") + fragment(b"two"))
|
|
dst = tmp_path / "clean.mp4"
|
|
|
|
assert strip_duplicate_init_boxes(src, dst) == 0
|
|
assert not dst.exists(), "the caller must keep the original when the format changes"
|
|
|
|
|
|
def test_a_single_init_is_left_alone(tmp_path: Path) -> None:
|
|
src = tmp_path / "a.mp4"
|
|
src.write_bytes(init_pair(b"dtsx") + fragment(b"one"))
|
|
dst = tmp_path / "clean.mp4"
|
|
assert strip_duplicate_init_boxes(src, dst) == 0
|
|
assert not dst.exists()
|
|
|
|
|
|
def test_an_unreadable_input_aborts_instead_of_shipping_a_placeholder(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""
|
|
MP4Box answers a file its ISOM reader refused by importing it through ffmpeg, which
|
|
writes the samples under a placeholder 4CC. mkvmerge-style return codes treat 1 as a
|
|
warning, so that shipped a whole title with an unnameable audio track.
|
|
"""
|
|
|
|
class RefusingPopen(FakePopen):
|
|
def __init__(self, args, **kwargs):
|
|
super().__init__(args, **kwargs)
|
|
self.stdout = io.StringIO("[IsoMedia] error while opening /tmp/a.mp4, error=Invalid IsoMedia File\n")
|
|
|
|
def wait(self) -> int:
|
|
return 0
|
|
|
|
monkeypatch.setattr(binaries, "MP4Box", Path("MP4Box"), raising=False)
|
|
monkeypatch.setattr(subprocess, "Popen", RefusingPopen)
|
|
|
|
tracks = build_tracks(tmp_path, b"dtsx")
|
|
_, returncode, errors = tracks.mux("Title", delete=False, output_path=tmp_path / "out.muxed.mkv")
|
|
|
|
assert returncode >= 2, "a refused input must abort the run, not pass as a warning"
|
|
assert errors
|
|
|
|
|
|
def two_entry_video(path: Path, dref_count: int = 1, entry_body: int = 78, decoy: bytes = b"") -> Path:
|
|
"""A video MP4 whose second sample entry names data reference 2; ``decoy`` goes in the trak before mdia."""
|
|
|
|
def entry(index: int) -> bytes:
|
|
return box(b"dvh1", (b"\x00" * 6 + struct.pack(">H", index) + b"\x00" * 70)[:entry_body])
|
|
|
|
dref = box(b"dref", struct.pack(">II", 0, dref_count) + box(b"url ", b"\x00\x00\x00\x01"))
|
|
stsd = box(b"stsd", struct.pack(">II", 0, 2) + entry(1) + entry(2))
|
|
minf = box(b"minf", box(b"dinf", dref) + box(b"stbl", stsd))
|
|
path.write_bytes(box(b"ftyp", b"isom") + box(b"moov", box(b"trak", decoy + box(b"mdia", minf))) + fragment(b"x"))
|
|
return path
|
|
|
|
|
|
def repointed(data: bytes) -> bytes:
|
|
"""``data`` with its second dvh1 entry pointed at data reference 1."""
|
|
expected = bytearray(data)
|
|
index = expected.rindex(b"dvh1") + 4 + 6
|
|
expected[index : index + 2] = b"\x00\x01"
|
|
return bytes(expected)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"decoy",
|
|
[
|
|
pytest.param(b"", id="plain"),
|
|
pytest.param(box(b"meta", box(b"dinf", box(b"dref", struct.pack(">II", 0, 0)))), id="meta-dref-before-mdia"),
|
|
],
|
|
)
|
|
def test_a_sample_entry_with_a_missing_data_reference_is_pointed_at_the_first(tmp_path: Path, decoy: bytes) -> None:
|
|
"""A packager can give a second sample entry data_reference_index 2 with one dref entry; MP4Box refuses it.
|
|
|
|
Only the media's own dref counts, not one under a meta box before mdia.
|
|
"""
|
|
path = two_entry_video(tmp_path / "v.mp4", decoy=decoy)
|
|
expected = repointed(path.read_bytes())
|
|
|
|
assert fix_data_reference_indexes(path) == 1
|
|
assert path.read_bytes() == expected, "only the second entry's index may change"
|
|
assert fix_data_reference_indexes(path) == 0
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("dref_count", "entry_body"),
|
|
[pytest.param(0, 78, id="empty-dref"), pytest.param(1, 4, id="short-entry"), pytest.param(1, 0, id="empty-entry")],
|
|
)
|
|
def test_malformed_entries_are_left_alone(tmp_path: Path, dref_count: int, entry_body: int) -> None:
|
|
"""With no dref entry to point at, or no room for the index, the file must not change."""
|
|
path = two_entry_video(tmp_path / "v.mp4", dref_count=dref_count, entry_body=entry_body)
|
|
before = path.read_bytes()
|
|
|
|
assert fix_data_reference_indexes(path) == 0
|
|
assert path.read_bytes() == before
|
|
|
|
|
|
def test_the_mp4_mux_hands_mp4box_the_repointed_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
seen: dict[str, bytes] = {}
|
|
|
|
def fake_popen(args: list[str], **kwargs: object) -> FakePopen:
|
|
source = next(arg for arg in args if arg.startswith(str(tmp_path / "v.mp4")))
|
|
seen["video"] = Path(source.split(":lang=")[0]).read_bytes()
|
|
Path(args[-1]).write_bytes(b"muxed")
|
|
return FakePopen(args, **kwargs)
|
|
|
|
monkeypatch.setattr(binaries, "MP4Box", Path("MP4Box"), raising=False)
|
|
monkeypatch.setattr(subprocess, "Popen", fake_popen)
|
|
|
|
tracks = build_tracks(tmp_path, b"dtsx")
|
|
original = two_entry_video(tracks.videos[0].path).read_bytes()
|
|
_, returncode, _ = tracks.mux("Title", delete=False, output_path=tmp_path / "out.muxed.mkv")
|
|
|
|
assert returncode == 0
|
|
assert seen["video"] == repointed(original)
|