fix(titles): name AC-4 Atmos in the {atmos} token from Audio.atmos of the tracks in the file

This commit is contained in:
imSp4rky
2026-12-02 16:50:51 -07:00
parent bedcbe1569
commit 50f56de957
4 changed files with 118 additions and 3 deletions
+96
View File
@@ -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"
+3 -1
View File
@@ -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"