From 50f56de9579757ab2ca55f0e8ead713c30d42854 Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Wed, 2 Dec 2026 16:50:51 -0700 Subject: [PATCH] fix(titles): name AC-4 Atmos in the {atmos} token from Audio.atmos of the tracks in the file --- docs/guide/output-and-naming.md | 2 +- tests/core/test_atmos_naming.py | 96 ++++++++++++++++++++++++ tests/core/test_tag_rules_integration.py | 4 +- unshackle/core/titles/title.py | 19 ++++- 4 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 tests/core/test_atmos_naming.py diff --git a/docs/guide/output-and-naming.md b/docs/guide/output-and-naming.md index 0de4d9d0..03b885f5 100644 --- a/docs/guide/output-and-naming.md +++ b/docs/guide/output-and-naming.md @@ -203,7 +203,7 @@ A `sports` folder template groups by `{year}`, or by the air year when the event | `audio` | Audio codec; rename it with [`codec_names`](../reference/configuration/output.md#codec_names) | `DDP`, `DD`, `AAC`, `DTS-X` | | `audio_channels` | Channel layout; bed.LFE.heights when the track has height channels | `5.1`, `2.0`, `5.1.4` | | `audio_full` | Codec + channels combined; a name that ends in a digit gets a separator | `DDP5.1`, `DTS-X.5.1.4`, `AC-4.2.0` | -| `atmos` | `Atmos` when any track carries JOC | `Atmos` | +| `atmos` | `Atmos` when an audio track in the file carries JOC or is marked Atmos, as in the `--list` row (MediaInfo shows no JOC for AC-4) | `Atmos` | | `dual` | `DUAL` for two audio languages (see [`dual_multi_mode`](../reference/configuration/download.md#dual_multi_mode)) | `DUAL` | | `multi` | `MULTi` for more than two audio languages | `MULTi` | | `dubbed` | `DUBBED` for a single audio language that is not the original (strict mode only) | `DUBBED` | diff --git a/tests/core/test_atmos_naming.py b/tests/core/test_atmos_naming.py new file mode 100644 index 00000000..ad261742 --- /dev/null +++ b/tests/core/test_atmos_naming.py @@ -0,0 +1,96 @@ +"""The {atmos} token of the naming context.""" + +from __future__ import annotations + +import types +from pathlib import Path + +import pytest + +from unshackle.core.titles.episode import Episode +from unshackle.core.titles.movie import Movie +from unshackle.core.titles.music import Song +from unshackle.core.tracks import Audio +from unshackle.core.wire import deserialize_audio + + +class DummyService: + pass + + +def media_info(*formats: str, joc: bool = False, path: Path | None = None) -> types.SimpleNamespace: + """A MediaInfo of the file at ``path`` with one 2.0 audio track per format, JOC only when asked.""" + tracks = [ + types.SimpleNamespace( + language="en", + format=fmt, + channel_layout="L R", + channellayout_original=None, + channel_s=2, + channels=2, + format_additionalfeatures="JOC" if joc else None, + joc=None, + ) + for fmt in formats + ] + general = [types.SimpleNamespace(complete_name=str(path))] if path else [] + return types.SimpleNamespace(general_tracks=general, video_tracks=[], audio_tracks=tracks) + + +def audio(codec: Audio.Codec, atmos: bool, language: str = "en") -> Audio: + extra = {"atmos": True} if atmos else None + return Audio(id_=f"{codec.name}-{language}", url="https://x/a", codec=codec, language=language, extra=extra) + + +TITLES = { + "movie": lambda: Movie(id_="movie-0001", service=DummyService, name="The Film", year=2024, language="en"), + "episode": lambda: Episode( + id_="episode-0001", service=DummyService, title="The Show", season=1, number=2, language="en" + ), + "song": lambda: Song( + id_="song-0001", + service=DummyService, + name="The Song", + artist="EXAMPLE", + album="The Album", + track=1, + language="en", + ), +} + + +@pytest.mark.parametrize("kind", TITLES) +def test_an_atmos_track_names_a_file_mediainfo_shows_without_joc(kind: str) -> None: + title = TITLES[kind]() + title.tracks.add(audio(Audio.Codec.AC4, atmos=True)) + assert title.build_template_context(media_info("AC-4"))["atmos"] == "Atmos" + + +def test_a_track_from_the_remote_server_carries_atmos() -> None: + title = TITLES["song"]() + title.tracks.add( + deserialize_audio({"id": "a", "url": "https://x/a", "codec": "AC4", "language": "en", "atmos": True}) + ) + assert title.build_template_context(media_info("AC-4"))["atmos"] == "Atmos" + + +def test_joc_in_mediainfo_still_names_atmos() -> None: + assert TITLES["movie"]().build_template_context(media_info("E-AC-3", joc=True))["atmos"] == "Atmos" + + +def test_a_file_without_the_atmos_track_is_not_named_atmos() -> None: + """With the audio split by codec, the AAC file of a title with an Atmos track has no Atmos.""" + title = TITLES["movie"]() + title.tracks.add(audio(Audio.Codec.AC4, atmos=True)) + title.tracks.add(audio(Audio.Codec.AAC, atmos=False)) + assert title.build_template_context(media_info("AAC"))["atmos"] == "" + + +def test_a_file_of_one_track_reads_only_that_track(tmp_path: Path) -> None: + """A track saved on its own is not named Atmos for another track of the same codec.""" + title = TITLES["movie"]() + atmos, plain = audio(Audio.Codec.EC3, atmos=True), audio(Audio.Codec.EC3, atmos=False, language="de") + atmos.path, plain.path = tmp_path / "en.mp4", tmp_path / "de.mp4" + title.tracks.add([atmos, plain]) + assert title.build_template_context(media_info("E-AC-3", path=plain.path))["atmos"] == "" + assert title.build_template_context(media_info("E-AC-3", path=atmos.path))["atmos"] == "Atmos" diff --git a/tests/core/test_tag_rules_integration.py b/tests/core/test_tag_rules_integration.py index d0405757..b3a7f52f 100644 --- a/tests/core/test_tag_rules_integration.py +++ b/tests/core/test_tag_rules_integration.py @@ -90,7 +90,9 @@ def test_rules_run_after_lang_tag(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(config, "tag_rules", [{"when": {"lang_tag": "SUBBED"}, "tag": "SUBGRP"}]) movie = make_movie() - movie.tracks = TrackStub(audio=[types.SimpleNamespace(language=Language.get("ja"))], subtitles=[]) # type: ignore[assignment] + movie.tracks = TrackStub( + audio=[types.SimpleNamespace(language=Language.get("ja"), atmos=False, codec=None, path=None)], subtitles=[] + ) # type: ignore[assignment] context = movie.build_base_template_context(make_media_info(["ja"])) assert context["lang_tag"] == "SUBBED" diff --git a/unshackle/core/titles/title.py b/unshackle/core/titles/title.py index 0e1ad832..170275d6 100644 --- a/unshackle/core/titles/title.py +++ b/unshackle/core/titles/title.py @@ -1,6 +1,7 @@ from __future__ import annotations from abc import abstractmethod +from pathlib import Path from typing import Any from langcodes import Language @@ -73,6 +74,9 @@ class Title: def build_base_template_context(self, media_info: MediaInfo, show_service: bool = True) -> dict: """Build base template context dictionary from MediaInfo. + {atmos} also reads Audio.atmos of the title tracks in the file, since MediaInfo shows no + JOC on AC-4: the track the file was parsed from, else each track of a codec the file holds. + Extracts video, audio, HDR, HFR, and multi-language information shared across all title types. Subclasses should call this and extend the returned dict with their specific fields (e.g., season/episode). @@ -183,7 +187,20 @@ class Title: channel_count = primary_audio_track.channel_s or primary_audio_track.channels or 0 channels = float(channel_count) - has_atmos = any("JOC" in (t.format_additionalfeatures or "") or t.joc for t in media_info.audio_tracks) + file_path = next( + (Path(g.complete_name) for g in getattr(media_info, "general_tracks", ()) if g.complete_name), None + ) + file_codecs = {AUDIO_CODEC_MAP.get(t.format, t.format) for t in media_info.audio_tracks} + file_audio = [a for a in self.tracks.audio if file_path and a.path == file_path] + if not file_audio: + file_audio = [ + a + for a in self.tracks.audio + if a.codec and AUDIO_CODEC_MAP.get(a.codec.value, a.codec.value) in file_codecs + ] + has_atmos = any( + "JOC" in (t.format_additionalfeatures or "") or t.joc for t in media_info.audio_tracks + ) or any(a.atmos for a in file_audio) if heights: channels_str = f"{len(positions) - heights - lfe}.{lfe}.{heights}"