From 08481d3a334bc10d147f4f9a3e7c6302cae97fb3 Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Sun, 15 Nov 2026 21:42:53 -0700 Subject: [PATCH] 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. --- docs/getting-started/installation.md | 2 +- docs/guide/cli-reference.md | 2 +- docs/guide/downloading.md | 3 +- docs/guide/output-and-naming.md | 8 +-- tests/core/test_audio_vivid.py | 77 ++++-------------------- tests/core/test_click_types.py | 4 +- tests/core/test_mp4_container_routing.py | 2 +- tests/tracks/test_vvc_mux.py | 73 ++++++++++++++++++++++ unshackle/commands/dl.py | 27 +++------ unshackle/core/api/routes.py | 2 +- unshackle/core/constants.py | 2 +- unshackle/core/tracks/tracks.py | 51 ++++++++++++++-- unshackle/core/utils/audio_vivid.py | 53 ---------------- unshackle/core/utils/click_types.py | 6 +- 14 files changed, 151 insertions(+), 161 deletions(-) create mode 100644 tests/tracks/test_vvc_mux.py delete mode 100644 unshackle/core/utils/audio_vivid.py diff --git a/docs/getting-started/installation.md b/docs/getting-started/installation.md index e1c2ebb3..5ee07b4c 100644 --- a/docs/getting-started/installation.md +++ b/docs/getting-started/installation.md @@ -141,7 +141,7 @@ warning and prints the `directories` lines to add. | --- | --- | --- | | [Python](https://www.python.org/) | 3.11 to 3.14 | Running unshackle itself | | [uv](https://docs.astral.sh/uv/) | ≥ 0.5 | Installing and running unshackle | -| [FFmpeg](https://ffmpeg.org/) | ≥ 6.0 | Media processing and analysis | +| [FFmpeg](https://ffmpeg.org/) | ≥ 6.0, or ≥ 8.0 for H.266 | Media processing and analysis | | [MKVToolNix](https://mkvtoolnix.download/) | ≥ 80 | MKV muxing and metadata editing | | [shaka-packager](https://github.com/shaka-project/shaka-packager) | 2.6.1, or ≥ 3.10.0 | DRM decryption | | [Bento4](https://github.com/axiomatic-systems/Bento4) | ≥ 1.6.0-639 | Alternate DRM decryption (`mp4decrypt`) | diff --git a/docs/guide/cli-reference.md b/docs/guide/cli-reference.md index 0ad6268d..ba4d8071 100644 --- a/docs/guide/cli-reference.md +++ b/docs/guide/cli-reference.md @@ -73,7 +73,7 @@ unshackle dl [OPTIONS] SERVICE [SERVICE ARGS...] |---|---|---| | `-p`, `--profile` | - | Profile for credentials and cookies. | | `-q`, `--quality` | best | Resolution(s), comma-separated, e.g. `1080,720`. `-q 1080` also matches anamorphic tracks by 16:9 canvas. | -| `-v`, `--vcodec` | any | Video codec(s). Accepts names or values: `AVC`/`H.264`/`H264`, `HEVC`/`H.265`/`H265`, `VC1`/`VC-1`, `VP8`, `VP9`, `AV1`. | +| `-v`, `--vcodec` | any | Video codec(s). Accepts names or values: `AVC`/`H.264`/`H264`, `HEVC`/`H.265`/`H265`, `VVC`/`H.266`/`H266`, `VC1`/`VC-1`, `VP8`, `VP9`, `AV1`. | | `-a`, `--acodec` | any | Audio codec(s), comma-separated. Accepts `AAC`, `AC3`/`DD`, `EC3`/`DD+`/`eac3`/`ddp`, `AC4`/`AC-4`, `OPUS`, `OGG`/`VORB`/`vorbis`, `DTS`, `DTSX`/`DTS-X`, `ALAC`, `FLAC`, `MP3`. | | `-vb`, `--vbitrate` | highest | Exact video bitrate in kbps. | | `-ab`, `--abitrate` | highest | Exact audio bitrate in kbps. | diff --git a/docs/guide/downloading.md b/docs/guide/downloading.md index f2f0c451..4390d3db 100644 --- a/docs/guide/downloading.md +++ b/docs/guide/downloading.md @@ -120,13 +120,14 @@ either enum **names** or their **values**, comma-separated. | --- | --- | | `AVC` | `H.264` | | `HEVC` | `H.265` | +| `VVC` | `H.266` | | `VC1` | `VC-1` | | `VP8` | `VP8` | | `VP9` | `VP9` | | `AV1` | `AV1` | | `LCEVC` | `LCEVC` | -`H264` and `H265` are aliases of `AVC` and `HEVC`. +`H264`, `H265` and `H266` are aliases of `AVC`, `HEVC` and `VVC`. An LCEVC (MPEG-5 Part 2) track only adds detail on top of its base video track and cannot play alone, so unshackle selects it only when you ask for it with `-v LCEVC`. diff --git a/docs/guide/output-and-naming.md b/docs/guide/output-and-naming.md index 3494d9d5..fa9d01bc 100644 --- a/docs/guide/output-and-naming.md +++ b/docs/guide/output-and-naming.md @@ -456,12 +456,8 @@ No muxer carries Audio Vivid (AVS3-P3): Matroska has no CodecID for it, and FFmp write it to MP4. unshackle leaves an Audio Vivid track out of the mux and saves its original MPEG-TS file beside the output, named like a `--no-mux` audio file (`..AV3A.ts`). -With an Audio Vivid decoder named `avs3Decoder` in a `binaries` folder or on `PATH`, unshackle also -decodes the track to PCM and muxes that into the output. unshackle does not ship the decoder: the UWA -reference decoder's licence limits its use. A service that delivers Audio Vivid can declare the decoder so -that `unshackle env check` lists it. - -Without the decoder, a title whose only tracks are Audio Vivid is not muxed. +Audio Vivid needs its own decoder, which you supply. unshackle does not ship one. A title whose only +tracks are Audio Vivid is not muxed. ### 3D output diff --git a/tests/core/test_audio_vivid.py b/tests/core/test_audio_vivid.py index 14ad75ed..ab93a758 100644 --- a/tests/core/test_audio_vivid.py +++ b/tests/core/test_audio_vivid.py @@ -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" diff --git a/tests/core/test_click_types.py b/tests/core/test_click_types.py index f57cf0f9..5e997aea 100644 --- a/tests/core/test_click_types.py +++ b/tests/core/test_click_types.py @@ -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(): diff --git a/tests/core/test_mp4_container_routing.py b/tests/core/test_mp4_container_routing.py index f491609d..0b41a40a 100644 --- a/tests/core/test_mp4_container_routing.py +++ b/tests/core/test_mp4_container_routing.py @@ -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) diff --git a/tests/tracks/test_vvc_mux.py b/tests/tracks/test_vvc_mux.py new file mode 100644 index 00000000..3abd2489 --- /dev/null +++ b/tests/tracks/test_vvc_mux.py @@ -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()) diff --git a/unshackle/commands/dl.py b/unshackle/commands/dl.py index 341d90a2..5f30b9c6 100644 --- a/unshackle/commands/dl.py +++ b/unshackle/commands/dl.py @@ -1,6 +1,5 @@ from __future__ import annotations -import copy import html import logging import math @@ -136,7 +135,6 @@ from unshackle.core.utilities import ( time_elapsed_since, ) from unshackle.core.utils import tags -from unshackle.core.utils.audio_vivid import decode_audio_vivid from unshackle.core.utils.bitrate import apply_real_bitrates from unshackle.core.utils.click_types import ( AUDIO_CODEC_LIST, @@ -517,20 +515,10 @@ def mux_skipped(no_mux: bool, title: Any) -> bool: return no_mux and not isinstance(title, Song) -def take_unmuxable_audio(tracks: Tracks, log: logging.Logger) -> list[Audio]: - """Remove and return the Audio Vivid tracks, which no muxer carries, to be saved beside the output. - - With the user's decoder, each one also goes back into the mux as a WAV copy. - """ +def take_unmuxable_audio(tracks: Tracks) -> list[Audio]: + """Remove and return the Audio Vivid tracks, which no muxer carries, to be saved beside the output.""" kept = [a for a in tracks.audio if a.codec == Audio.Codec.AV3A] tracks.audio = [a for a in tracks.audio if a.codec != Audio.Codec.AV3A] - for track in kept: - wav = decode_audio_vivid(track.path, log) if track.path else None - if wav: - decoded = copy.copy(track) - decoded.path = wav - decoded.codec = Audio.Codec.PCM - tracks.audio.append(decoded) return kept @@ -547,8 +535,10 @@ def track_file_suffix(track: Any) -> str: def no_mux_extension(track: Any, path: Path, media_info: MediaInfo) -> str: - """The extension of a --no-mux track file: an audio track in an MPEG-4 file is saved as .m4a.""" + """The extension of a track file saved on its own: .ts for MPEG-TS, .m4a for an audio track in MPEG-4.""" container = media_info.general_tracks[0].format if media_info.general_tracks else None + if container == "MPEG-TS": + return ".ts" return ".m4a" if isinstance(track, Audio) and path.suffix == ".mp4" and container == "MPEG-4" else path.suffix @@ -3567,7 +3557,7 @@ class dl: tagged_paths: set[Path] = set() append_audio_codec_suffix = True skip_mux = mux_skipped(no_mux, title) - kept_audio = [] if skip_mux else take_unmuxable_audio(title.tracks, self.log) + kept_audio = [] if skip_mux else take_unmuxable_audio(title.tracks) if kept_audio and not title.tracks: title.tracks.audio = kept_audio kept_audio = [] @@ -3982,9 +3972,8 @@ class dl: self.completed_files.append(final_path) for kept in kept_audio: assert kept.path is not None - kept_path = final_path.with_name( - f"{final_path.stem}{track_file_suffix(kept)}{kept.path.suffix}" - ) + ext = no_mux_extension(kept, kept.path, MediaInfo.parse(kept.path)) + kept_path = final_path.with_name(f"{final_path.stem}{track_file_suffix(kept)}{ext}") shutil.move(kept.path, kept_path) self.completed_files.append(kept_path) sidecar_files.setdefault(muxed_path, []).append(kept_path) diff --git a/unshackle/core/api/routes.py b/unshackle/core/api/routes.py index d655bcaf..e7fc815f 100644 --- a/unshackle/core/api/routes.py +++ b/unshackle/core/api/routes.py @@ -945,7 +945,7 @@ async def download(request: web.Request) -> web.Response: - type: array items: type: string - description: Video codec(s) to download (e.g., "HEVC" or ["AVC", "HEVC"]) - accepts AVC, H.264, H264, HEVC, H.265, H265, VC1, VC-1, VP8, VP9, AV1, LCEVC (default - []) + description: Video codec(s) to download (e.g., "HEVC" or ["AVC", "HEVC"]) - accepts AVC, H.264, H264, HEVC, H.265, H265, VVC, H.266, H266, VC1, VC-1, VP8, VP9, AV1, LCEVC (default - []) acodec: oneOf: - type: string diff --git a/unshackle/core/constants.py b/unshackle/core/constants.py index 767305cc..177f106c 100644 --- a/unshackle/core/constants.py +++ b/unshackle/core/constants.py @@ -23,7 +23,7 @@ class DownloadCancelled(Exception): DRM_SORT_MAP = ["ClearKey", "Widevine"] LANGUAGE_MAX_DISTANCE = 5 # this is max to be considered "same", e.g., en, en-US, en-AU LANGUAGE_EXACT_DISTANCE = 0 # exact match only, no variants -VIDEO_CODEC_MAP = {"AVC": "H.264", "HEVC": "H.265"} +VIDEO_CODEC_MAP = {"AVC": "H.264", "HEVC": "H.265", "VVC": "H.266"} DYNAMIC_RANGE_MAP = { "HDR10": "HDR", "HDR10+": "HDR10P", diff --git a/unshackle/core/tracks/tracks.py b/unshackle/core/tracks/tracks.py index f8f5232b..da934b14 100644 --- a/unshackle/core/tracks/tracks.py +++ b/unshackle/core/tracks/tracks.py @@ -131,13 +131,18 @@ def select_videos( return selected -def file_track_ids(path: Path) -> dict[str, int]: - """Return the mkvmerge ID of the first track of each type in a file. A TS file can hold its audio first.""" +def mkvmerge_tracks(path: Path) -> list[dict[str, Any]]: + """The tracks ``mkvmerge -J`` reports for a file, empty when it reads none.""" res = subprocess.run( [str(binaries.MKVToolNix), "-J", str(path)], capture_output=True, text=True, encoding="utf-8", errors="replace" ) + return json.loads(res.stdout or "{}").get("tracks") or [] + + +def file_track_ids(path: Path) -> dict[str, int]: + """Return the mkvmerge ID of the first track of each type in a file. A TS file can hold its audio first.""" ids: dict[str, int] = {} - for track in json.loads(res.stdout or "{}").get("tracks") or []: + for track in mkvmerge_tracks(path): ids.setdefault(track["type"], track["id"]) if not ids: logging.getLogger("Tracks").debug( @@ -146,6 +151,32 @@ def file_track_ids(path: Path) -> dict[str, int]: return ids or {"video": 0, "audio": 1} +def remux_vvc(source: Path) -> Path: + """Remux an H.266 file to a temp Matroska file with FFmpeg, the one input mkvmerge keeps as VVC. + + mkvmerge stores VVC from an MP4 as V_QUICKTIME, and FFmpeg before 8.0 writes another codec ID. Both exit 0 + with video no player decodes, so the codec ID FFmpeg wrote is checked. + """ + if not binaries.FFMPEG: + raise EnvironmentError('FFmpeg executable "ffmpeg" was not found but is required to mux H.266.') + output = config.directories.temp / f"vvcmux_{source.stem}_{os.getpid()}_{threading.get_ident()}.mkv" + cmd: list[Union[str, Path]] = [binaries.FFMPEG, "-nostdin", "-y", "-i", source, "-map", "0:v", "-map", "0:a?"] + cmd += ["-map_metadata", "-1", "-fflags", "+bitexact", "-c", "copy", output] + run_step( + cmd, + status="Remuxing H.266 for mkvmerge...", + output=output, + label="ffmpeg H.266 remux", + ) + codec = next( + (t.get("properties", {}).get("codec_id") for t in mkvmerge_tracks(output) if t["type"] == "video"), None + ) + if codec != "V_MPEGI/ISO/VVC": + output.unlink(missing_ok=True) + raise RuntimeError(f"FFmpeg wrote H.266 as {codec}, muxing H.266 needs FFmpeg 8.0 or later") + return output + + def mp4_track_count(moov: Optional[bytes]) -> int: """How many tracks ``MP4Box -add`` takes from a file: each ``trak`` of its moov, or 1 for a file with none.""" return sum(1 for kind, _, _ in children(moov or b"", 8) if kind == b"trak") or 1 @@ -793,7 +824,7 @@ class Tracks: delay = round((track.sample_start - sample_base) * 1000) if config.sample else 0 moov = read_top_level_box(source, b"moov") - flags = "" if moov else ":tkhd=enable,movie,preview" + flags = "" if moov else ":tkhd=enable,movie,preview:ID=-1" cl.extend(["-add", f"{source}:lang={track.mux_language}{flags}" + (f":delay={delay}" if delay else "")]) name = "" if isinstance(track, Video) else (track.get_track_name() or "") if name: @@ -1029,6 +1060,7 @@ class Tracks: ) ) + remuxed: list[Path] = [] for i, vt in enumerate(self.videos): if not vt.path or not vt.path.exists(): raise ValueError("Video Track must be downloaded before muxing...") @@ -1043,7 +1075,12 @@ class Tracks: else: is_default = i == 0 - ids = file_track_ids(vt.path) + source = vt.path + if vt.codec == Video.Codec.VVC and source.suffix.lower() != ".mkv": + source = remux_vvc(source) + remuxed.append(source) + + ids = file_track_ids(source) v = ids.get("video", 0) video_args = [ "--language", @@ -1091,7 +1128,7 @@ class Tracks: ] ) - cl.extend(video_args + ["(", str(vt.path), ")"]) + cl.extend(video_args + ["(", str(source), ")"]) for i, at in enumerate(self.audio): if not at.path or not at.path.exists(): @@ -1282,6 +1319,8 @@ class Tracks: finally: if chapters_path: chapters_path.unlink() + for path in remuxed: + path.unlink(missing_ok=True) if delete: for track in self: track.delete() diff --git a/unshackle/core/utils/audio_vivid.py b/unshackle/core/utils/audio_vivid.py deleted file mode 100644 index 813cab23..00000000 --- a/unshackle/core/utils/audio_vivid.py +++ /dev/null @@ -1,53 +0,0 @@ -from __future__ import annotations - -import logging -from pathlib import Path -from typing import Optional - -from unshackle.core import binaries -from unshackle.core.utils.hdr10plus import PROBE_BYTES, TS_PACKET, ts_payload, ts_pid, ts_pmt -from unshackle.core.utils.subprocess import run_step - - -def extract_audio_vivid(ts_path: Path, out_path: Path) -> bool: - """Write the Audio Vivid elementary stream of an MPEG-TS file to ``out_path``; False when it holds none.""" - from unshackle.core.tracks import Audio - - with ts_path.open("rb") as ts: - entries = ts_pmt(ts.read(PROBE_BYTES)) or [] - pid = next((p for p, kind, reg in entries if Audio.Codec.from_ts(kind, reg) == Audio.Codec.AV3A), None) - if pid is None: - return False - ts.seek(0) - with out_path.open("wb") as out: - while packet := ts.read(TS_PACKET): - if len(packet) < TS_PACKET or packet[0] != 0x47 or ts_pid(packet) != pid: - continue - payload = ts_payload(packet) - if packet[1] & 0x40 and payload[:3] == b"\x00\x00\x01" and len(payload) > 8: - payload = payload[9 + payload[8] :] - out.write(payload) - return out_path.stat().st_size > 0 - - -def decode_audio_vivid(ts_path: Path, log: logging.Logger) -> Optional[Path]: - """Decode an Audio Vivid MPEG-TS file to WAV with the user's ``avs3Decoder``, or None without one or on failure. - - unshackle does not ship the decoder: the UWA reference decoder's licence limits its use and redistribution. - A service that delivers Audio Vivid declares it in ``get_binaries()``, so ``env check`` lists it. - """ - decoder: Optional[Path] = getattr(binaries, "avs3Decoder", None) - if not decoder: - return None - stream, wav = ts_path.with_suffix(".av3a"), ts_path.with_suffix(".wav") - try: - if not extract_audio_vivid(ts_path, stream): - raise ValueError("no Audio Vivid stream in the file") - run_step([decoder, stream, wav], status="Decoding Audio Vivid...", output=wav, label="Audio Vivid decode") - except (OSError, ValueError, RuntimeError) as e: - log.warning(f"Audio Vivid decode of {ts_path.name} failed") - log.debug(f"Audio Vivid decode of {ts_path.name}: {e}") - return None - finally: - stream.unlink(missing_ok=True) - return wav diff --git a/unshackle/core/utils/click_types.py b/unshackle/core/utils/click_types.py index 86ef0247..5303c1f3 100644 --- a/unshackle/core/utils/click_types.py +++ b/unshackle/core/utils/click_types.py @@ -17,8 +17,8 @@ class VideoCodecChoice(click.Choice): A custom Choice type for video codecs that accepts both enum names and values. Accepts both: - - Enum names: avc, hevc, vc1, vp8, vp9, av1 - - Enum values: H.264, H.265, VC-1, VP8, VP9, AV1 + - Enum names: avc, hevc, vvc, vc1, vp8, vp9, av1 + - Enum values: H.264, H.265, H.266, VC-1, VP8, VP9, AV1 """ def __init__(self, codec_enum: type[Video.Codec]) -> None: @@ -28,7 +28,7 @@ class VideoCodecChoice(click.Choice): for choice in (codec.name.lower(), codec.value.lower()): self._name_to_codec[choice] = codec - aliases = {"h264": "AVC", "h265": "HEVC"} + aliases = {"h264": "AVC", "h265": "HEVC", "h266": "VVC"} for alias, target in aliases.items(): if target in codec_enum.__members__: self._name_to_codec[alias] = codec_enum[target]