From 783a2fc185d64ad269265e54ab98f679f6279846 Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Sat, 5 Dec 2026 14:55:11 -0700 Subject: [PATCH] fix(tracks): show the audio bit depth and sample rate in the track listing only for a song --- docs/dev/service-architecture.md | 4 +-- docs/guide/downloading.md | 8 ++--- tests/core/test_audio_resolution.py | 45 +++++++++++++++++++++++++---- unshackle/commands/dl.py | 5 ++-- unshackle/core/tracks/audio.py | 6 +++- unshackle/core/tracks/tracks.py | 9 ++++-- 6 files changed, 59 insertions(+), 18 deletions(-) diff --git a/docs/dev/service-architecture.md b/docs/dev/service-architecture.md index 07293af6c..af504b65a 100644 --- a/docs/dev/service-architecture.md +++ b/docs/dev/service-architecture.md @@ -208,8 +208,8 @@ Each subclass adds its own required fields on top of `id_` and `service`: options, `-a`/`--acodec` and `--abitrate`, and its usual audio sorting. A movie or an episode uses the same options. `-q`/`--quality` sets the video height only and does not apply to music. The audio sort ranks a hi-res FLAC above a CD FLAC by `bitrate`. Set - `bit_depth` and `sample_rate` (Hz) on each `Audio` track, and `--list` shows them as - "24-bit / 96 kHz", so do not write that text into `name`. The same two keys in + `bit_depth` and `sample_rate` (Hz) on each `Audio` track, and `--list` shows them for a + `Song` as "24-bit / 96 kHz", so do not write that text into `name`. The same two keys in `Song.data` give the Hi-Res badge in the release listing. When the first song's `Song.data` has no bit depth or sample rate, or a lower one, `dl` diff --git a/docs/guide/downloading.md b/docs/guide/downloading.md index 41b4af209..878afea78 100644 --- a/docs/guide/downloading.md +++ b/docs/guide/downloading.md @@ -324,7 +324,7 @@ unshackle dl -a AAC -ab 320 EXAMPLE 81234567 A service can offer both a CD FLAC and a hi-res FLAC. The two have the same codec, so the bitrate is what separates them. Use `--list` to read the bitrates, then give `-ab` the one you want. When the service gives them, `--list` also shows the bit depth and - sample rate, such as "24-bit / 96 kHz". + sample rate of a song, such as "24-bit / 96 kHz". A movie or an episode does not show them. ## Languages @@ -920,9 +920,9 @@ tracks from those URLs. If such an export does not give the title a language, `o value: select the languages with `-l` and `-vl`. Each exported track keeps the values that the service set on it, also when the import parses -the manifest again: for example Atmos, the bit depth, the sample rate and the note at the end of -its `--list` line. A track that the export does not hold shows only what the manifest gives. A -song imports as a song, with its artist, album and track number. An export that an older +the manifest again: for example Atmos, the note at the end of its `--list` line, and the bit +depth and sample rate of a song. A track that the export does not hold shows only what the +manifest gives. A song imports as a song, with its artist, album and track number. An export that an older unshackle wrote records a song as a movie. Its import then stops at the video selection, because a song has no video track. Export the song again. diff --git a/tests/core/test_audio_resolution.py b/tests/core/test_audio_resolution.py index fec34ef8a..c3bc1a968 100644 --- a/tests/core/test_audio_resolution.py +++ b/tests/core/test_audio_resolution.py @@ -3,12 +3,15 @@ from __future__ import annotations import pytest +from rich.text import Text +from tests.core.test_music_renderer import flac_release, run_release +from tests.lang_selection.test_kind_only_selection import FakeService from unshackle.core.manifests.dash import DASH from unshackle.core.manifests.hls import HLS from unshackle.core.manifests.ism import ISM from unshackle.core.music.renderer import MusicRenderer -from unshackle.core.tracks import Audio, Tracks +from unshackle.core.tracks import Audio, Tracks, Video def dash_audio(set_attrs: str = "", rep_attrs: str = "") -> Audio: @@ -73,19 +76,49 @@ def test_hls_reads_bit_depth_and_sample_rate_when_the_playlist_states_them( assert (audio.bit_depth, audio.sample_rate) == expected +def listing(audio: Audio, music: bool) -> str: + """The audio line of the track listing as plain text.""" + (node,) = Tracks([audio]).tree(music=music)[0].children[0].children + return Text.from_markup(str(node.label)).plain + + @pytest.mark.parametrize( ("bit_depth", "sample_rate", "label"), [(24, 96000, "24-bit / 96 kHz"), (16, 44100, "16-bit / 44.1 kHz"), (None, 48000, "48 kHz"), (24, None, "24-bit")], ) -def test_the_listing_shows_bit_depth_and_sample_rate( +def test_a_music_listing_shows_bit_depth_and_sample_rate( bit_depth: int | None, sample_rate: int | None, label: str ) -> None: - audio = Audio(url="https://cdn.example/a.flac", language="en", bit_depth=bit_depth, sample_rate=sample_rate) - assert f" | {label}" in str(audio) + audio = Audio( + url="https://cdn.example/a.flac", language="en", bitrate=900_000, bit_depth=bit_depth, sample_rate=sample_rate + ) + assert f"| 900 kb/s | {label} | English" in listing(audio, music=True) -def test_the_listing_shows_neither_when_unset() -> None: - assert "bit" not in str(Audio(url="https://cdn.example/a.flac", language="en")) +def test_a_music_listing_shows_neither_when_unset() -> None: + assert "bit" not in listing(Audio(url="https://cdn.example/a.flac", language="en"), music=True) + + +@pytest.mark.parametrize("list_", [True, False], ids=["list", "download"]) +@pytest.mark.parametrize("song", [True, False], ids=["song", "episode"]) +def test_dl_shows_the_label_only_for_a_song( + monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, song: bool, list_: bool +) -> None: + album, tracks = flac_release() + titles = album if song else FakeService({1: {}}).get_titles() + if not song: + tracks.add(Video(id_="v", url="https://x/v", language="en", codec=Video.Codec.AVC, range_=Video.Range.SDR)) + monkeypatch.setattr("unshackle.commands.dl.mux_title", lambda *a, **k: ([], [])) + out = run_release(monkeypatch, caplog, titles, tracks, list_=list_, to_end=True) # type: ignore[arg-type] + assert "3075 kb/s" in out + assert ("3075 kb/s | 24-bit / 96 kHz" in out) is song + + +def test_a_video_listing_shows_neither() -> None: + audio = Audio(url="https://cdn.example/a.mp4", language="en", bit_depth=24, sample_rate=48000) + text = listing(audio, music=False) + assert "English" in text + assert "kHz" not in text and "24-bit" not in text @pytest.mark.parametrize("value", ["24", 24.0]) diff --git a/unshackle/commands/dl.py b/unshackle/commands/dl.py index b639177ac..f489c1624 100644 --- a/unshackle/commands/dl.py +++ b/unshackle/commands/dl.py @@ -2222,9 +2222,10 @@ class dl: exact_match=exact_lang, ) + music = isinstance(title, Song) if list_: redraw_header([audio for audio in title.tracks.audio if not acodec or audio.codec in acodec]) - available_tracks, _ = title.tracks.tree() + available_tracks, _ = title.tracks.tree(music=music) print_listing(listing_panel(available_tracks, "Available Tracks")) continue @@ -2260,7 +2261,7 @@ class dl: settle_post_script_group(title) continue - selected_tracks, tracks_progress_callables = title.tracks.tree(add_progress=True) + selected_tracks, tracks_progress_callables = title.tracks.tree(add_progress=True, music=music) log_event( "track_select", diff --git a/unshackle/core/tracks/audio.py b/unshackle/core/tracks/audio.py index 43550891e..0cb38c3ee 100644 --- a/unshackle/core/tracks/audio.py +++ b/unshackle/core/tracks/audio.py @@ -204,6 +204,10 @@ class Audio(Track): return False def __str__(self) -> str: + return self.line() + + def line(self, resolution: bool = False) -> str: + """The track line of the listing; ``resolution`` adds the bit depth and sample rate.""" return " | ".join( filter( None, @@ -222,7 +226,7 @@ class Audio(Track): ) ), f"{self.bitrate // 1000} kb/s" if self.bitrate else None, - self.resolution_label(self.bit_depth, self.sample_rate), + self.resolution_label(self.bit_depth, self.sample_rate) if resolution else None, self.get_track_name(), ", ".join(self.edition) if self.edition else None, self.note, diff --git a/unshackle/core/tracks/tracks.py b/unshackle/core/tracks/tracks.py index 86c05660b..dbbba4b41 100644 --- a/unshackle/core/tracks/tracks.py +++ b/unshackle/core/tracks/tracks.py @@ -249,7 +249,8 @@ class Tracks: if lines ) - def tree(self, add_progress: bool = False) -> tuple[Tree, list[Callable[..., None]]]: + def tree(self, add_progress: bool = False, music: bool = False) -> tuple[Tree, list[Callable[..., None]]]: + """The track listing; ``music`` shows the bit depth and sample rate of each audio track.""" all_tracks = [*list(self), *self.chapters, *self.attachments] progress_callables = [] @@ -262,6 +263,8 @@ class Tracks: track_type_plural = track_type.__name__ + ("s" if track_type != Audio and num_tracks != 1 else "") tracks_tree = tree.add(f"[repr.number]{num_tracks}[/] {track_type_plural}") for track in tracks: + text = track.line(resolution=True) if music and isinstance(track, Audio) else str(track) + label = escape(text[6:]) if add_progress and track_type not in (Chapter, Attachment): progress = Progress( SpinnerColumn(finished_text=""), @@ -306,11 +309,11 @@ class Tracks: progress_callables.append(update_track_progress) track_table = Table.grid() - track_table.add_row(escape(str(track)[6:]), style="text2") + track_table.add_row(label, style="text2") track_table.add_row(progress) tracks_tree.add(track_table) else: - tracks_tree.add(escape(str(track)[6:]), style="text2") + tracks_tree.add(label, style="text2") # Show Closed Captions right after Subtitles (even if no subtitle tracks exist) if track_type is Subtitle: