mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-03 06:25:10 +00:00
feat(music): redraw the album header with the quality of the first song's chosen audio track
This commit is contained in:
@@ -233,6 +233,11 @@ Each subclass adds its own required fields on top of `id_` and `service`:
|
|||||||
"24-bit / 96 kHz", so do not write that text into `name`. The same two keys in
|
"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.
|
`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`
|
||||||
|
copies them from the best selected lossless track (FLAC, ALAC or PCM) of the first song.
|
||||||
|
If that changes the Quality row, `dl` shows the release header again. A lossy track
|
||||||
|
changes nothing.
|
||||||
|
|
||||||
A `Song` is an ordinary `Title` on the one generic download path. unshackle has no
|
A `Song` is an ordinary `Title` on the one generic download path. unshackle has no
|
||||||
music-specific service API and no separate music download loop. The
|
music-specific service API and no separate music download loop. The
|
||||||
`GROUP_AUDIO_DOWNLOADS` flag, `get_music_track_options` and `get_music_collection_label`
|
`GROUP_AUDIO_DOWNLOADS` flag, `get_music_track_options` and `get_music_collection_label`
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from collections.abc import Callable
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import jsonpickle
|
import jsonpickle
|
||||||
@@ -7,6 +8,7 @@ from tests.lang_selection.test_kind_only_selection import FakeService, run
|
|||||||
from unshackle.core.console import console
|
from unshackle.core.console import console
|
||||||
from unshackle.core.music.renderer import MusicRenderer
|
from unshackle.core.music.renderer import MusicRenderer
|
||||||
from unshackle.core.titles.music import Album, Music, Song
|
from unshackle.core.titles.music import Album, Music, Song
|
||||||
|
from unshackle.core.tracks import Audio, Tracks
|
||||||
|
|
||||||
|
|
||||||
class DummyService:
|
class DummyService:
|
||||||
@@ -225,3 +227,132 @@ def test_the_track_line_shows_the_song_as_given(
|
|||||||
)
|
)
|
||||||
def test_is_hires_quality_reads_sample_rate_not_bitrate(label: str, hires: bool) -> None:
|
def test_is_hires_quality_reads_sample_rate_not_bitrate(label: str, hires: bool) -> None:
|
||||||
assert MusicRenderer.is_hires_quality(label) is hires
|
assert MusicRenderer.is_hires_quality(label) is hires
|
||||||
|
|
||||||
|
|
||||||
|
def flac_release(**data: Any) -> tuple[Album, Tracks]:
|
||||||
|
"""One release of two songs with `data` on its first song, and one 24-bit / 96 kHz FLAC track."""
|
||||||
|
music = Album([song(1, "Album 1", "Artist"), song(2, "Album 1", "Artist")])
|
||||||
|
music[0].data = {"duration": 60, **data}
|
||||||
|
tracks = Tracks()
|
||||||
|
tracks.add(
|
||||||
|
Audio(
|
||||||
|
id_="a",
|
||||||
|
url="https://x/a",
|
||||||
|
language="en",
|
||||||
|
codec=Audio.Codec.FLAC,
|
||||||
|
bitrate=3_075_000,
|
||||||
|
bit_depth=24,
|
||||||
|
sample_rate=96000,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return music, tracks
|
||||||
|
|
||||||
|
|
||||||
|
def run_release(
|
||||||
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
music: Music,
|
||||||
|
tracks: Tracks | Callable[[Song], Tracks],
|
||||||
|
**flags: Any,
|
||||||
|
) -> str:
|
||||||
|
service = FakeService({})
|
||||||
|
service.get_titles = lambda: music # type: ignore[assignment]
|
||||||
|
service.get_tracks = tracks if callable(tracks) else lambda title: tracks # type: ignore[method-assign, assignment]
|
||||||
|
with console.capture() as out:
|
||||||
|
run(monkeypatch, caplog, {}, service=service, lang=["en"], **flags)
|
||||||
|
return out.get()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("list_", [False, True])
|
||||||
|
@pytest.mark.parametrize("data", [{}, {"bit_depth": 16, "sample_rate": 44100}], ids=["missing", "lower"])
|
||||||
|
def test_the_header_is_drawn_again_with_the_quality_of_the_first_chosen_track(
|
||||||
|
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, data: dict, list_: bool
|
||||||
|
) -> None:
|
||||||
|
music, tracks = flac_release(**data)
|
||||||
|
|
||||||
|
out = run_release(monkeypatch, caplog, music, tracks, list_=list_)
|
||||||
|
|
||||||
|
assert out.count("Hi-Res Lossless") == 1
|
||||||
|
assert out.count("Length") == 2
|
||||||
|
assert out.count("Tracklist") == 1
|
||||||
|
assert music[0].data | {"bit_depth": 24, "sample_rate": 96000, "lossless": True} == music[0].data
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_header_is_not_drawn_again_when_its_quality_stays_the_same(
|
||||||
|
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
|
||||||
|
) -> None:
|
||||||
|
music, tracks = flac_release(bit_depth=24, sample_rate=192000, lossless=True)
|
||||||
|
|
||||||
|
out = run_release(monkeypatch, caplog, music, tracks)
|
||||||
|
|
||||||
|
assert out.count("Length") == 1
|
||||||
|
assert music[0].data["sample_rate"] == 192000
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_collection_of_different_releases_is_not_drawn_again(
|
||||||
|
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
|
||||||
|
) -> None:
|
||||||
|
_, tracks = flac_release()
|
||||||
|
music = mixed(artist="Own Artist")
|
||||||
|
|
||||||
|
out = run_release(monkeypatch, caplog, music, tracks)
|
||||||
|
|
||||||
|
assert out.count("Length") == 1
|
||||||
|
assert "Quality" not in out
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_header_is_drawn_again_once_per_release(
|
||||||
|
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
|
||||||
|
) -> None:
|
||||||
|
music, tracks = flac_release()
|
||||||
|
higher = Tracks()
|
||||||
|
higher.add(
|
||||||
|
Audio(id_="b", url="https://x/b", language="en", codec=Audio.Codec.FLAC, bit_depth=24, sample_rate=192000)
|
||||||
|
)
|
||||||
|
|
||||||
|
out = run_release(monkeypatch, caplog, music, lambda title: tracks if title is music[0] else higher, to_end=True)
|
||||||
|
|
||||||
|
assert out.count("Length") == 2
|
||||||
|
assert music[0].data["sample_rate"] == 96000
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_later_first_song_fills_the_release_song(
|
||||||
|
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
|
||||||
|
) -> None:
|
||||||
|
music, tracks = flac_release()
|
||||||
|
|
||||||
|
out = run_release(monkeypatch, caplog, music, tracks, wanted=["1x2"])
|
||||||
|
|
||||||
|
assert out.count("Hi-Res Lossless") == 1
|
||||||
|
assert music[0].data["bit_depth"] == 24
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_best_of_several_chosen_tracks_fills_the_header() -> None:
|
||||||
|
music, tracks = flac_release()
|
||||||
|
lossy = Audio(id_="e", url="https://x/e", language="en", codec=Audio.Codec.EC3, sample_rate=48000)
|
||||||
|
|
||||||
|
assert MusicRenderer().redraw_header(music, [lossy, *tracks.audio]) is not None
|
||||||
|
assert music[0].data | {"bit_depth": 24, "sample_rate": 96000, "lossless": True} == music[0].data
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_lossless_flag_the_service_set_stays() -> None:
|
||||||
|
music, tracks = flac_release(bit_depth=24, sample_rate=96000, lossless=False)
|
||||||
|
|
||||||
|
assert MusicRenderer().redraw_header(music, tracks.audio) is None
|
||||||
|
assert music[0].data["lossless"] is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_lossy_first_track_draws_no_header_again(
|
||||||
|
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
|
||||||
|
) -> None:
|
||||||
|
music, _ = flac_release()
|
||||||
|
lossy = Tracks()
|
||||||
|
lossy.add(
|
||||||
|
Audio(id_="e", url="https://x/e", language="en", codec=Audio.Codec.EC3, bitrate=768_000, sample_rate=48000)
|
||||||
|
)
|
||||||
|
|
||||||
|
out = run_release(monkeypatch, caplog, music, lossy)
|
||||||
|
|
||||||
|
assert out.count("Length") == 1
|
||||||
|
assert "Quality" not in out
|
||||||
|
assert music[0].data == {"duration": 60}
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ from unshackle.core.events import events
|
|||||||
from unshackle.core.export_name import season_episodes
|
from unshackle.core.export_name import season_episodes
|
||||||
from unshackle.core.export_writer import ExportWriter
|
from unshackle.core.export_writer import ExportWriter
|
||||||
from unshackle.core.launch import LaunchKeys, launch, load_service_config
|
from unshackle.core.launch import LaunchKeys, launch, load_service_config
|
||||||
|
from unshackle.core.music.renderer import MusicRenderer
|
||||||
from unshackle.core.mux_step import mux_title
|
from unshackle.core.mux_step import mux_title
|
||||||
from unshackle.core.placement import SAMPLE_DIR, Placement, sample_name
|
from unshackle.core.placement import SAMPLE_DIR, Placement, sample_name
|
||||||
from unshackle.core.providers.tvdb import SEASON_TYPES
|
from unshackle.core.providers.tvdb import SEASON_TYPES
|
||||||
@@ -85,7 +86,7 @@ from unshackle.core.services import ROUTING_PARAMS, Services
|
|||||||
from unshackle.core.temp import low_free_space, temp_root, with_task_temp
|
from unshackle.core.temp import low_free_space, temp_root, with_task_temp
|
||||||
from unshackle.core.title_metadata import Enrichment, TitleMetadata, apply_enrich
|
from unshackle.core.title_metadata import Enrichment, TitleMetadata, apply_enrich
|
||||||
from unshackle.core.title_source import KeyArrival, TitleSource
|
from unshackle.core.title_source import KeyArrival, TitleSource
|
||||||
from unshackle.core.titles import Movie, Movies, Series, Song, Title_T
|
from unshackle.core.titles import Movie, Movies, Music, Series, Song, Title_T
|
||||||
from unshackle.core.titles.episode import Episode
|
from unshackle.core.titles.episode import Episode
|
||||||
from unshackle.core.tracks import Audio, Subtitle, Tracks, Video
|
from unshackle.core.tracks import Audio, Subtitle, Tracks, Video
|
||||||
from unshackle.core.tracks.attachment import Attachment
|
from unshackle.core.tracks.attachment import Attachment
|
||||||
@@ -2026,6 +2027,16 @@ class dl:
|
|||||||
if not mux_skipped(no_mux, title):
|
if not mux_skipped(no_mux, title):
|
||||||
settle_post_script_group(title)
|
settle_post_script_group(title)
|
||||||
|
|
||||||
|
header_music = titles if isinstance(titles, Music) else None
|
||||||
|
|
||||||
|
def redraw_header(audio: Sequence[Audio]) -> None:
|
||||||
|
"""Draw the release header again, once, when the first chosen audio track changes its Quality row."""
|
||||||
|
nonlocal header_music
|
||||||
|
if header_music is not None and audio:
|
||||||
|
if header := MusicRenderer().redraw_header(header_music, audio):
|
||||||
|
print_listing(header)
|
||||||
|
header_music = None
|
||||||
|
|
||||||
for i, title in enumerate(titles):
|
for i, title in enumerate(titles):
|
||||||
if not title_wanted(title, wanted):
|
if not title_wanted(title, wanted):
|
||||||
continue
|
continue
|
||||||
@@ -2189,6 +2200,7 @@ class dl:
|
|||||||
)
|
)
|
||||||
|
|
||||||
if list_:
|
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()
|
||||||
print_listing(listing_panel(available_tracks, "Available Tracks"))
|
print_listing(listing_panel(available_tracks, "Available Tracks"))
|
||||||
continue
|
continue
|
||||||
@@ -2202,6 +2214,7 @@ class dl:
|
|||||||
if isinstance(outcome, Skip):
|
if isinstance(outcome, Skip):
|
||||||
skip_title(title, outcome.reason, outcome.kind_only)
|
skip_title(title, outcome.reason, outcome.kind_only)
|
||||||
continue
|
continue
|
||||||
|
redraw_header(title.tracks.audio)
|
||||||
range_ = outcome.ranges
|
range_ = outcome.ranges
|
||||||
|
|
||||||
receipt_entries = archive_entries(self.service, title, archive_options) if archive else []
|
receipt_entries = archive_entries(self.service, title, archive_options) if archive else []
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
from collections.abc import Sequence
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from rich.console import Group, RenderableType
|
from rich.console import Group, RenderableType
|
||||||
@@ -14,6 +15,8 @@ from unshackle.core.music.extract import first_number, first_text, format_durati
|
|||||||
from unshackle.core.titles.music import Music, Song, release_song
|
from unshackle.core.titles.music import Music, Song, release_song
|
||||||
from unshackle.core.tracks.audio import Audio
|
from unshackle.core.tracks.audio import Audio
|
||||||
|
|
||||||
|
LOSSLESS_CODECS = (Audio.Codec.FLAC, Audio.Codec.ALAC, Audio.Codec.PCM)
|
||||||
|
|
||||||
|
|
||||||
class MusicRenderer:
|
class MusicRenderer:
|
||||||
"""Render a Music release for the CLI title listing.
|
"""Render a Music release for the CLI title listing.
|
||||||
@@ -55,19 +58,7 @@ class MusicRenderer:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
length = self.format_total_duration(getattr(music, "total_duration", None) or self.sum_duration(music))
|
length = self.format_total_duration(getattr(music, "total_duration", None) or self.sum_duration(music))
|
||||||
bit_depth, sample_rate = self.resolution(data, metadata)
|
quality = self.header_quality(music, data, metadata)
|
||||||
quality = self.quality_summary(
|
|
||||||
first_text(
|
|
||||||
getattr(music, "quality", None),
|
|
||||||
data.get("quality"),
|
|
||||||
metadata.get("quality"),
|
|
||||||
metadata.get("quality_label"),
|
|
||||||
),
|
|
||||||
lossless=self.as_bool(self.first_value(data.get("lossless"), metadata.get("lossless"))),
|
|
||||||
hires=self.as_bool(self.first_value(data.get("hires"), metadata.get("hires"))),
|
|
||||||
bit_depth=bit_depth,
|
|
||||||
sample_rate=sample_rate,
|
|
||||||
)
|
|
||||||
|
|
||||||
grid = Table.grid(padding=(0, 2))
|
grid = Table.grid(padding=(0, 2))
|
||||||
grid.add_column(style="bright_black", no_wrap=True)
|
grid.add_column(style="bright_black", no_wrap=True)
|
||||||
@@ -97,6 +88,50 @@ class MusicRenderer:
|
|||||||
|
|
||||||
return grid
|
return grid
|
||||||
|
|
||||||
|
def header_quality(self, music: Music, data: dict[str, Any], metadata: dict[str, Any]) -> str:
|
||||||
|
bit_depth, sample_rate = self.resolution(data, metadata)
|
||||||
|
return self.quality_summary(
|
||||||
|
first_text(
|
||||||
|
getattr(music, "quality", None),
|
||||||
|
data.get("quality"),
|
||||||
|
metadata.get("quality"),
|
||||||
|
metadata.get("quality_label"),
|
||||||
|
),
|
||||||
|
lossless=self.as_bool(self.first_value(data.get("lossless"), metadata.get("lossless"))),
|
||||||
|
hires=self.as_bool(self.first_value(data.get("hires"), metadata.get("hires"))),
|
||||||
|
bit_depth=bit_depth,
|
||||||
|
sample_rate=sample_rate,
|
||||||
|
)
|
||||||
|
|
||||||
|
def redraw_header(self, music: Music, audio: Sequence[Audio]) -> Table | None:
|
||||||
|
"""
|
||||||
|
Fill the release song's missing or lower bit depth and sample rate, and a missing lossless flag, from the best
|
||||||
|
lossless track of `audio`.
|
||||||
|
|
||||||
|
Return the header when that changes its Quality row, else None. A lossy track fills nothing, and a collection
|
||||||
|
of several releases has no release song, so both are left as they are.
|
||||||
|
"""
|
||||||
|
release = release_song(music)
|
||||||
|
lossless = [track for track in audio if track.codec in LOSSLESS_CODECS]
|
||||||
|
if not release or not lossless:
|
||||||
|
return None
|
||||||
|
if release.data is None:
|
||||||
|
release.data = {}
|
||||||
|
if not isinstance(release.data, dict):
|
||||||
|
return None
|
||||||
|
data = release.data
|
||||||
|
metadata = m if isinstance(m := data.get("metadata"), dict) else {}
|
||||||
|
before = self.header_quality(music, data, metadata)
|
||||||
|
bit_depth, sample_rate = self.resolution(data, metadata)
|
||||||
|
best = max(lossless, key=lambda track: (track.bit_depth or 0, track.sample_rate or 0))
|
||||||
|
if best.bit_depth and best.bit_depth > (bit_depth or 0):
|
||||||
|
data["bit_depth"] = best.bit_depth
|
||||||
|
if best.sample_rate and best.sample_rate > (sample_rate or 0):
|
||||||
|
data["sample_rate"] = best.sample_rate
|
||||||
|
if self.first_value(data.get("lossless"), metadata.get("lossless")) is None:
|
||||||
|
data["lossless"] = True
|
||||||
|
return self.render_header(music) if self.header_quality(music, data, metadata) != before else None
|
||||||
|
|
||||||
def render_tracks(self, music: Music) -> Panel:
|
def render_tracks(self, music: Music) -> Panel:
|
||||||
total = len(music)
|
total = len(music)
|
||||||
track_label = "Track" if total == 1 else "Tracks"
|
track_label = "Track" if total == 1 else "Tracks"
|
||||||
|
|||||||
Reference in New Issue
Block a user