From 356d6fb984a63637a42480d4a4d63f512c63acc9 Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Tue, 11 Aug 2026 21:24:04 -0600 Subject: [PATCH] fix(dl): treat audio muxed into the video as an available language A muxed stream carries its audio in the video track, so -l orig no longer fails when the only separate audio tracks are dubs. --- .../test_embedded_audio_langs.py | 32 ++++++++++++ .../test_mux_embedded_audio_metadata.py | 52 +++++++++++++++++++ unshackle/commands/dl.py | 10 ++-- unshackle/core/tracks/tracks.py | 2 + unshackle/core/utilities.py | 13 +++++ 5 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 tests/lang_selection/test_embedded_audio_langs.py create mode 100644 tests/tracks/test_mux_embedded_audio_metadata.py diff --git a/tests/lang_selection/test_embedded_audio_langs.py b/tests/lang_selection/test_embedded_audio_langs.py new file mode 100644 index 00000000..5148f70f --- /dev/null +++ b/tests/lang_selection/test_embedded_audio_langs.py @@ -0,0 +1,32 @@ +from types import SimpleNamespace + +import pytest + +from unshackle.core.utilities import embedded_audio_langs + + +def video(**data): + return SimpleNamespace(data=data) + + +def test_no_videos(): + assert embedded_audio_langs([], True) == [] + + +def test_video_without_a_declaration(): + assert embedded_audio_langs([video(), video(audio_language="")], True) == [] + + +def test_declared_language_is_returned(): + assert embedded_audio_langs([video(audio_language="zh-CN")], True) == ["zh-CN"] + + +def test_every_declaring_video_counts(): + videos = [video(audio_language="zh-CN"), video(), video(audio_language="th")] + assert embedded_audio_langs(videos, True) == ["zh-CN", "th"] + + +@pytest.mark.parametrize("videos", [[video(audio_language="zh-CN")], [video(audio_language="zh-CN"), video()]]) +def test_dropping_the_video_drops_its_audio(videos): + # --audio-only / --no-video discard the track the audio lives in, so nothing is embedded + assert embedded_audio_langs(videos, False) == [] diff --git a/tests/tracks/test_mux_embedded_audio_metadata.py b/tests/tracks/test_mux_embedded_audio_metadata.py new file mode 100644 index 00000000..83191c97 --- /dev/null +++ b/tests/tracks/test_mux_embedded_audio_metadata.py @@ -0,0 +1,52 @@ +import pytest + +from unshackle.core import binaries +from unshackle.core.tracks import Tracks, Video + + +def make_video(track_id: str, **data) -> Video: + video = Video( + id_=track_id, + url=f"https://example.test/{track_id}.m3u8", + language="zh", + codec=Video.Codec.AVC, + range_=Video.Range.SDR, + width=1920, + height=1080, + bitrate=5_000_000, + ) + video.data.update(data) + return video + + +def mux_until_mkvmerge_check(tracks: Tracks) -> None: + # the embedded-audio metadata pass runs before the mkvmerge lookup, so a missing + # binary stops the mux right after the state under test has been written + with pytest.raises(RuntimeError, match="MKVToolNix"): + tracks.mux("Test Title") + + +def test_names_undeclared_video(monkeypatch): + monkeypatch.setattr(binaries, "MKVToolNix", None) + video = make_video("v1") + tracks = Tracks(video) + + mux_until_mkvmerge_check(tracks) + + assert video.needs_repack + assert video.data["audio_language"] == "zh" + assert video.data["audio_language_name"] == "Chinese" + + +def test_service_declaration_is_preserved(monkeypatch): + monkeypatch.setattr(binaries, "MKVToolNix", None) + declared = make_video("v1", audio_language="th", audio_language_name="Thai [Original]") + undeclared = make_video("v2") + tracks = Tracks([declared, undeclared]) + + mux_until_mkvmerge_check(tracks) + + assert declared.data["audio_language"] == "th" + assert declared.data["audio_language_name"] == "Thai [Original]" + assert not declared.needs_repack + assert undeclared.data["audio_language"] == "zh" diff --git a/unshackle/commands/dl.py b/unshackle/commands/dl.py index b2e2558a..ef3095c3 100644 --- a/unshackle/commands/dl.py +++ b/unshackle/commands/dl.py @@ -76,6 +76,7 @@ from unshackle.core.tracks.hybrid import Hybrid from unshackle.core.tracks.track import assert_fragments_decrypted, has_encrypted_sample_entry from unshackle.core.utilities import ( as_requested, + embedded_audio_langs, excluded_language_tags, find_font_with_fallbacks, find_missing_langs, @@ -3125,10 +3126,12 @@ class dl: if a_orig_token in audio_languages: a_orig_token = None + embedded_langs = embedded_audio_langs(title.tracks.videos, keep_videos) + if not any(tok in processed_lang for tok in ("best", "all")): missing_a_langs = find_missing_langs( processed_lang, - [a.language for a in title.tracks.audio], + [a.language for a in title.tracks.audio] + embedded_langs, exact=exact_lang, ) if missing_a_langs: @@ -3153,8 +3156,9 @@ class dl: title.tracks.audio = select_best_audio( title.tracks.audio, processed_lang, acodec, audio_description, exact_lang ) - if not title.tracks.audio: - # empty only when 'orig' was the sole request and did not resolve + if not title.tracks.audio and not embedded_langs: + # empty when 'orig' was the sole request, did not resolve, and the + # video carries no audio of its own to fall back on self.log.error( f"There's no {as_requested(processed_lang, a_orig_token) or 'orig'} " f"Audio Track, cannot continue..." diff --git a/unshackle/core/tracks/tracks.py b/unshackle/core/tracks/tracks.py index faa5ce26..2a09761f 100644 --- a/unshackle/core/tracks/tracks.py +++ b/unshackle/core/tracks/tracks.py @@ -523,6 +523,8 @@ class Tracks: lang_name = video_track.language.display_name() for video in self.videos: + if video.data.get("audio_language"): + continue video.needs_repack = True video.data["audio_language"] = lang_code video.data["audio_language_name"] = lang_name diff --git a/unshackle/core/utilities.py b/unshackle/core/utilities.py index 2a53735f..e28ac03b 100644 --- a/unshackle/core/utilities.py +++ b/unshackle/core/utilities.py @@ -222,6 +222,19 @@ def keep_forced_subtitle( return match_func(language, forced_s_lang) +def embedded_audio_langs(videos: Sequence[Any], keep_videos: bool) -> list[str]: + """ + Return the audio languages carried inside video tracks rather than beside them. + + A muxed stream keeps its audio in the video track, which a service declares by setting + ``data["audio_language"]``. That audio is only available while the video is kept, so + dropping the video (``--audio-only``, ``--no-video``) drops the language with it. + """ + if not keep_videos: + return [] + return [video.data["audio_language"] for video in videos if video.data.get("audio_language")] + + def find_missing_langs( requested: Sequence[str], available: Sequence[Union[str, Language, None]],