fix(mux): mux H.266 as VVC and leave Audio Vivid decoding to services

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.
This commit is contained in:
imSp4rky
2026-11-15 21:42:53 -07:00
parent 270662e9b2
commit 08481d3a33
14 changed files with 151 additions and 161 deletions
+11 -66
View File
@@ -1,19 +1,13 @@
from __future__ import annotations
import logging
import stat
import sys
from pathlib import Path
from typing import Any
import pytest
from pymediainfo import MediaInfo
from tests.core.test_hdr10plus_probe import ts
from unshackle.commands.dl import take_unmuxable_audio, track_file_suffix
from unshackle.commands.dl import no_mux_extension, take_unmuxable_audio, track_file_suffix
from unshackle.core.tracks import Audio, Tracks, Video
from unshackle.core.utils import audio_vivid
LOG = logging.getLogger("test")
def media_packet(pid: int, payload: bytes, start: bool) -> bytes:
@@ -35,53 +29,6 @@ def audio_vivid_ts(frames: list[bytes]) -> bytes:
return data
def test_the_elementary_stream_is_every_pes_payload_in_order(tmp_path: Path) -> None:
frames = [bytes([n]) * size for n, size in ((1, 40), (2, 400), (3, 175))]
source, out = tmp_path / "a.ts", tmp_path / "a.av3a"
source.write_bytes(audio_vivid_ts(frames))
assert audio_vivid.extract_audio_vivid(source, out)
assert out.read_bytes() == b"".join(frames)
def test_a_ts_without_audio_vivid_extracts_nothing(tmp_path: Path) -> None:
source = tmp_path / "a.ts"
source.write_bytes(ts((0x0F, b"")))
assert not audio_vivid.extract_audio_vivid(source, tmp_path / "a.av3a")
def fake_decoder(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, *, fail: bool = False) -> None:
"""A stand-in decoder that copies its input to its output, or exits 3; a .cmd on Windows."""
if sys.platform == "win32":
decoder = tmp_path / "avs3Decoder.cmd"
decoder.write_text("@exit /b 3\r\n" if fail else '@copy /y "%~1" "%~2" >nul\r\n')
else:
decoder = tmp_path / "avs3Decoder"
decoder.write_text("#!/bin/sh\nexit 3\n" if fail else '#!/bin/sh\ncp "$1" "$2"\n')
decoder.chmod(decoder.stat().st_mode | stat.S_IEXEC)
monkeypatch.setattr(audio_vivid.binaries, "avs3Decoder", decoder, raising=False)
def test_the_decoder_gets_the_stream_and_writes_the_wav(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
fake_decoder(tmp_path, monkeypatch)
source = tmp_path / "a.ts"
source.write_bytes(audio_vivid_ts([b"frame"]))
wav = audio_vivid.decode_audio_vivid(source, LOG)
assert wav == tmp_path / "a.wav" and wav.read_bytes() == b"frame"
assert not (tmp_path / "a.av3a").exists()
def test_a_failing_decoder_warns_once_and_leaves_no_files(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
fake_decoder(tmp_path, monkeypatch, fail=True)
source = tmp_path / "a.ts"
source.write_bytes(audio_vivid_ts([b"frame"]))
with caplog.at_level(logging.WARNING):
assert audio_vivid.decode_audio_vivid(source, LOG) is None
assert caplog.messages == ["Audio Vivid decode of a.ts failed"]
assert sorted(p.name for p in tmp_path.iterdir() if not p.name.startswith("avs3")) == ["a.ts"]
def tracks_with(tmp_path: Path, *codecs: Any) -> Tracks:
tracks = Tracks()
tracks.videos = [Video(url="https://x.example/v.m3u8", language="zh", codec=Video.Codec.HEVC)]
@@ -92,22 +39,20 @@ def tracks_with(tmp_path: Path, *codecs: Any) -> Tracks:
return tracks
def test_without_a_decoder_audio_vivid_leaves_the_mux(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("unshackle.commands.dl.decode_audio_vivid", lambda path, log: None)
def test_audio_vivid_leaves_the_mux(tmp_path: Path) -> None:
tracks = tracks_with(tmp_path, Audio.Codec.EC3, Audio.Codec.AV3A)
kept = take_unmuxable_audio(tracks, LOG)
kept = take_unmuxable_audio(tracks)
assert [a.id for a in kept] == ["a1"]
assert [a.codec for a in tracks.audio] == [Audio.Codec.EC3]
def test_with_a_decoder_a_wav_copy_joins_the_mux(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("unshackle.commands.dl.decode_audio_vivid", lambda path, log: path.with_suffix(".wav"))
tracks = tracks_with(tmp_path, Audio.Codec.AV3A)
kept = take_unmuxable_audio(tracks, LOG)
assert [a.id for a in kept] == ["a0"] and kept[0].codec == Audio.Codec.AV3A
assert [(a.id, a.codec, a.path) for a in tracks.audio] == [("a0", Audio.Codec.PCM, tmp_path / "a0.wav")]
def test_a_kept_track_is_named_like_a_no_mux_track(tmp_path: Path) -> None:
tracks = tracks_with(tmp_path, Audio.Codec.AV3A)
assert [track_file_suffix(t) for t in (tracks.videos[0], tracks.audio[0])] == [".HEVC", ".zh.AV3A"]
def test_a_kept_ts_file_saved_as_mp4_gets_the_ts_extension(tmp_path: Path) -> None:
source = tmp_path / "Audio_a0.mp4"
source.write_bytes(audio_vivid_ts([b"frame"] * 40))
track = tracks_with(tmp_path, Audio.Codec.AV3A).audio[0]
assert no_mux_extension(track, source, MediaInfo.parse(source)) == ".ts"
+2 -2
View File
@@ -217,10 +217,10 @@ def test_empty_video_codec_tokens_are_skipped(value):
assert VIDEO_CODEC_LIST.convert(value) == [Video.Codec.AVC, Video.Codec.HEVC]
@pytest.mark.parametrize("spelling", ["H264", "h265", "H.265", "hevc"])
@pytest.mark.parametrize("spelling", ["H264", "h265", "H.265", "hevc", "H266", "h.266", "vvc"])
def test_video_codec_aliases_resolve(spelling):
"""The API advertised H264/H265 while dropping them; both paths now resolve the same."""
assert VIDEO_CODEC_LIST.convert(spelling)[0] in (Video.Codec.AVC, Video.Codec.HEVC)
assert VIDEO_CODEC_LIST.convert(spelling)[0] in (Video.Codec.AVC, Video.Codec.HEVC, Video.Codec.VVC)
def test_audio_codec_list_shell_complete():
+1 -1
View File
@@ -171,7 +171,7 @@ def test_mp4_subtitles_get_the_script_their_region_implies(tmp_path: Path, monke
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" in command
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)
+73
View File
@@ -0,0 +1,73 @@
from __future__ import annotations
import subprocess
from pathlib import Path
import pytest
from pymediainfo import MediaInfo
from unshackle.core import binaries
from unshackle.core.config import config
from unshackle.core.drm.verify import read_top_level_box
from unshackle.core.titles import Movie
from unshackle.core.tracks import Tracks, Video
from unshackle.core.tracks import tracks as tracks_module
def vvc_mp4(path: Path) -> None:
"""Encode a few frames of H.266 into an MP4, or skip when FFmpeg has no VVC encoder."""
if not binaries.FFMPEG:
pytest.skip("needs ffmpeg")
encode = subprocess.run(
[binaries.FFMPEG, "-v", "error", "-f", "lavfi", "-i", "testsrc=s=128x72:r=24:d=0.25"]
+ ["-c:v", "libvvenc", "-preset", "faster", str(path)],
capture_output=True,
)
if encode.returncode:
pytest.skip("ffmpeg has no libvvenc")
def vvc_tracks(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Tracks:
monkeypatch.setattr(config.directories, "temp", tmp_path / "temp", raising=False)
(tmp_path / "temp").mkdir()
video = Video(url="https://x.example/v.m3u8", language="en", codec=Video.Codec.VVC, id_="v")
video.path = tmp_path / "v.mp4"
vvc_mp4(video.path)
tracks = Tracks()
tracks.videos = [video]
return tracks
def video_codecs(path: Path) -> list[str]:
return [t["properties"]["codec_id"] for t in tracks_module.mkvmerge_tracks(path) if t["type"] == "video"]
def test_an_h266_track_is_muxed_as_vvc(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
if not binaries.MKVToolNix:
pytest.skip("needs mkvmerge")
tracks = vvc_tracks(tmp_path, monkeypatch)
out, code, _ = tracks.mux("t", delete=False, audio_expected=False, output_path=tmp_path / "out.mkv")
assert code == 0 and video_codecs(out) == ["V_MPEGI/ISO/VVC"]
assert tracks.videos[0].path == tmp_path / "v.mp4" and tracks.videos[0].path.exists()
assert not list((tmp_path / "temp").iterdir())
movie = Movie(id_="movie-0001", service=type("EXAMPLE", (), {}), name="Some Movie", year=2024)
assert ".H.266" in movie.get_filename(MediaInfo.parse(out))
def test_the_mp4box_route_gets_the_original_h266_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
if not getattr(binaries, "MP4Box", None):
pytest.skip("needs MP4Box")
tracks = vvc_tracks(tmp_path, monkeypatch)
tracks.videos[0].stereo = True
out, code, _ = tracks.mux("t", delete=False, audio_expected=False, output_path=tmp_path / "out.mkv")
moov = read_top_level_box(out, b"moov") or b""
assert code == 0 and out.suffix == ".mp4" and b"vvc1" in moov and b"FFID" not in moov
def test_a_remux_with_the_wrong_codec_id_stops_the_mux(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
tracks = vvc_tracks(tmp_path, monkeypatch)
old_ffmpeg = [{"type": "video", "properties": {"codec_id": "V_MS/VFW/FOURCC"}}]
monkeypatch.setattr(tracks_module, "mkvmerge_tracks", lambda path: old_ffmpeg)
with pytest.raises(RuntimeError, match="FFmpeg wrote H.266 as V_MS/VFW/FOURCC"):
tracks_module.remux_vvc(tracks.videos[0].path)
assert not list((tmp_path / "temp").iterdir())