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
+1 -1
View File
@@ -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 | | [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 | | [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 | | [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 | | [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`) | | [Bento4](https://github.com/axiomatic-systems/Bento4) | ≥ 1.6.0-639 | Alternate DRM decryption (`mp4decrypt`) |
+1 -1
View File
@@ -73,7 +73,7 @@ unshackle dl [OPTIONS] SERVICE [SERVICE ARGS...]
|---|---|---| |---|---|---|
| `-p`, `--profile` | - | Profile for credentials and cookies. | | `-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. | | `-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`. | | `-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. | | `-vb`, `--vbitrate` | highest | Exact video bitrate in kbps. |
| `-ab`, `--abitrate` | highest | Exact audio bitrate in kbps. | | `-ab`, `--abitrate` | highest | Exact audio bitrate in kbps. |
+2 -1
View File
@@ -120,13 +120,14 @@ either enum **names** or their **values**, comma-separated.
| --- | --- | | --- | --- |
| `AVC` | `H.264` | | `AVC` | `H.264` |
| `HEVC` | `H.265` | | `HEVC` | `H.265` |
| `VVC` | `H.266` |
| `VC1` | `VC-1` | | `VC1` | `VC-1` |
| `VP8` | `VP8` | | `VP8` | `VP8` |
| `VP9` | `VP9` | | `VP9` | `VP9` |
| `AV1` | `AV1` | | `AV1` | `AV1` |
| `LCEVC` | `LCEVC` | | `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 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`. alone, so unshackle selects it only when you ask for it with `-v LCEVC`.
+2 -6
View File
@@ -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 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 (`<name>.<language>.AV3A.ts`). beside the output, named like a `--no-mux` audio file (`<name>.<language>.AV3A.ts`).
With an Audio Vivid decoder named `avs3Decoder` in a `binaries` folder or on `PATH`, unshackle also Audio Vivid needs its own decoder, which you supply. unshackle does not ship one. A title whose only
decodes the track to PCM and muxes that into the output. unshackle does not ship the decoder: the UWA tracks are Audio Vivid is not muxed.
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.
### 3D output ### 3D output
+11 -66
View File
@@ -1,19 +1,13 @@
from __future__ import annotations from __future__ import annotations
import logging
import stat
import sys
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import pytest from pymediainfo import MediaInfo
from tests.core.test_hdr10plus_probe import ts 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.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: def media_packet(pid: int, payload: bytes, start: bool) -> bytes:
@@ -35,53 +29,6 @@ def audio_vivid_ts(frames: list[bytes]) -> bytes:
return data 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: def tracks_with(tmp_path: Path, *codecs: Any) -> Tracks:
tracks = Tracks() tracks = Tracks()
tracks.videos = [Video(url="https://x.example/v.m3u8", language="zh", codec=Video.Codec.HEVC)] 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 return tracks
def test_without_a_decoder_audio_vivid_leaves_the_mux(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: def test_audio_vivid_leaves_the_mux(tmp_path: Path) -> None:
monkeypatch.setattr("unshackle.commands.dl.decode_audio_vivid", lambda path, log: None)
tracks = tracks_with(tmp_path, Audio.Codec.EC3, Audio.Codec.AV3A) 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.id for a in kept] == ["a1"]
assert [a.codec for a in tracks.audio] == [Audio.Codec.EC3] 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: def test_a_kept_track_is_named_like_a_no_mux_track(tmp_path: Path) -> None:
tracks = tracks_with(tmp_path, Audio.Codec.AV3A) 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"] 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] 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): def test_video_codec_aliases_resolve(spelling):
"""The API advertised H264/H265 while dropping them; both paths now resolve the same.""" """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(): 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") tracks.mux("Title", delete=False, output_path=tmp_path / "out.muxed.mkv")
command = captured[0].args 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) 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())
+8 -19
View File
@@ -1,6 +1,5 @@
from __future__ import annotations from __future__ import annotations
import copy
import html import html
import logging import logging
import math import math
@@ -136,7 +135,6 @@ from unshackle.core.utilities import (
time_elapsed_since, time_elapsed_since,
) )
from unshackle.core.utils import tags 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.bitrate import apply_real_bitrates
from unshackle.core.utils.click_types import ( from unshackle.core.utils.click_types import (
AUDIO_CODEC_LIST, AUDIO_CODEC_LIST,
@@ -517,20 +515,10 @@ def mux_skipped(no_mux: bool, title: Any) -> bool:
return no_mux and not isinstance(title, Song) return no_mux and not isinstance(title, Song)
def take_unmuxable_audio(tracks: Tracks, log: logging.Logger) -> list[Audio]: 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. """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.
"""
kept = [a for a in tracks.audio if a.codec == Audio.Codec.AV3A] 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] 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 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: 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 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 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() tagged_paths: set[Path] = set()
append_audio_codec_suffix = True append_audio_codec_suffix = True
skip_mux = mux_skipped(no_mux, title) 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: if kept_audio and not title.tracks:
title.tracks.audio = kept_audio title.tracks.audio = kept_audio
kept_audio = [] kept_audio = []
@@ -3982,9 +3972,8 @@ class dl:
self.completed_files.append(final_path) self.completed_files.append(final_path)
for kept in kept_audio: for kept in kept_audio:
assert kept.path is not None assert kept.path is not None
kept_path = final_path.with_name( ext = no_mux_extension(kept, kept.path, MediaInfo.parse(kept.path))
f"{final_path.stem}{track_file_suffix(kept)}{kept.path.suffix}" kept_path = final_path.with_name(f"{final_path.stem}{track_file_suffix(kept)}{ext}")
)
shutil.move(kept.path, kept_path) shutil.move(kept.path, kept_path)
self.completed_files.append(kept_path) self.completed_files.append(kept_path)
sidecar_files.setdefault(muxed_path, []).append(kept_path) sidecar_files.setdefault(muxed_path, []).append(kept_path)
+1 -1
View File
@@ -945,7 +945,7 @@ async def download(request: web.Request) -> web.Response:
- type: array - type: array
items: items:
type: string 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: acodec:
oneOf: oneOf:
- type: string - type: string
+1 -1
View File
@@ -23,7 +23,7 @@ class DownloadCancelled(Exception):
DRM_SORT_MAP = ["ClearKey", "Widevine"] DRM_SORT_MAP = ["ClearKey", "Widevine"]
LANGUAGE_MAX_DISTANCE = 5 # this is max to be considered "same", e.g., en, en-US, en-AU 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 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 = { DYNAMIC_RANGE_MAP = {
"HDR10": "HDR", "HDR10": "HDR",
"HDR10+": "HDR10P", "HDR10+": "HDR10P",
+45 -6
View File
@@ -131,13 +131,18 @@ def select_videos(
return selected return selected
def file_track_ids(path: Path) -> dict[str, int]: def mkvmerge_tracks(path: Path) -> list[dict[str, Any]]:
"""Return the mkvmerge ID of the first track of each type in a file. A TS file can hold its audio first.""" """The tracks ``mkvmerge -J`` reports for a file, empty when it reads none."""
res = subprocess.run( res = subprocess.run(
[str(binaries.MKVToolNix), "-J", str(path)], capture_output=True, text=True, encoding="utf-8", errors="replace" [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] = {} 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"]) ids.setdefault(track["type"], track["id"])
if not ids: if not ids:
logging.getLogger("Tracks").debug( logging.getLogger("Tracks").debug(
@@ -146,6 +151,32 @@ def file_track_ids(path: Path) -> dict[str, int]:
return ids or {"video": 0, "audio": 1} 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: 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.""" """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 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 delay = round((track.sample_start - sample_base) * 1000) if config.sample else 0
moov = read_top_level_box(source, b"moov") 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 "")]) 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 "") name = "" if isinstance(track, Video) else (track.get_track_name() or "")
if name: if name:
@@ -1029,6 +1060,7 @@ class Tracks:
) )
) )
remuxed: list[Path] = []
for i, vt in enumerate(self.videos): for i, vt in enumerate(self.videos):
if not vt.path or not vt.path.exists(): if not vt.path or not vt.path.exists():
raise ValueError("Video Track must be downloaded before muxing...") raise ValueError("Video Track must be downloaded before muxing...")
@@ -1043,7 +1075,12 @@ class Tracks:
else: else:
is_default = i == 0 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) v = ids.get("video", 0)
video_args = [ video_args = [
"--language", "--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): for i, at in enumerate(self.audio):
if not at.path or not at.path.exists(): if not at.path or not at.path.exists():
@@ -1282,6 +1319,8 @@ class Tracks:
finally: finally:
if chapters_path: if chapters_path:
chapters_path.unlink() chapters_path.unlink()
for path in remuxed:
path.unlink(missing_ok=True)
if delete: if delete:
for track in self: for track in self:
track.delete() track.delete()
-53
View File
@@ -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
+3 -3
View File
@@ -17,8 +17,8 @@ class VideoCodecChoice(click.Choice):
A custom Choice type for video codecs that accepts both enum names and values. A custom Choice type for video codecs that accepts both enum names and values.
Accepts both: Accepts both:
- Enum names: avc, hevc, vc1, vp8, vp9, av1 - Enum names: avc, hevc, vvc, vc1, vp8, vp9, av1
- Enum values: H.264, H.265, VC-1, 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: 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()): for choice in (codec.name.lower(), codec.value.lower()):
self._name_to_codec[choice] = codec self._name_to_codec[choice] = codec
aliases = {"h264": "AVC", "h265": "HEVC"} aliases = {"h264": "AVC", "h265": "HEVC", "h266": "VVC"}
for alias, target in aliases.items(): for alias, target in aliases.items():
if target in codec_enum.__members__: if target in codec_enum.__members__:
self._name_to_codec[alias] = codec_enum[target] self._name_to_codec[alias] = codec_enum[target]