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)