feat(tracks): add Audio bit_depth and sample_rate from manifests and carry them over remote

This commit is contained in:
imSp4rky
2026-12-02 15:24:01 -07:00
parent 2c305c475f
commit 3b6bd02e46
14 changed files with 268 additions and 35 deletions
+130
View File
@@ -0,0 +1,130 @@
"""An audio track carries its bit depth and sample rate from the manifest or the service, and shows them."""
from __future__ import annotations
import pytest
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
def dash_audio(set_attrs: str = "", rep_attrs: str = "") -> Audio:
text = f"""<?xml version="1.0"?>
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" type="static" mediaPresentationDuration="PT8S" minBufferTime="PT2S">
<Period>
<AdaptationSet contentType="audio" mimeType="audio/mp4" lang="en"{set_attrs}>
<Representation id="a1" codecs="flac" bandwidth="2000000"{rep_attrs}>
<SegmentTemplate media="a1_$Number$.m4s" initialization="a1_init.mp4" duration="4" timescale="1"/>
</Representation>
</AdaptationSet>
</Period>
</MPD>"""
(audio,) = DASH.from_text(text, "https://cdn.example/x.mpd").to_tracks(language="en").audio
return audio
@pytest.mark.parametrize(
("set_attrs", "rep_attrs", "rate"),
[
("", ' audioSamplingRate="96000"', 96000),
(' audioSamplingRate="44100"', "", 44100),
(' audioSamplingRate="44100"', ' audioSamplingRate="192000"', 192000),
("", ' audioSamplingRate="24000 48000"', 48000),
("", "", None),
("", ' audioSamplingRate="high"', None),
("", ' audioSamplingRate="²"', None),
],
)
def test_dash_reads_the_sample_rate(set_attrs: str, rep_attrs: str, rate: int | None) -> None:
audio = dash_audio(set_attrs, rep_attrs)
assert (audio.sample_rate, audio.bit_depth) == (rate, None)
def test_ism_reads_the_sample_rate() -> None:
text = """<?xml version="1.0"?>
<SmoothStreamingMedia MajorVersion="2" MinorVersion="0" Duration="40000000" TimeScale="10000000">
<StreamIndex Type="audio" Name="audio" Language="en" Url="QualityLevels({Bitrate})/Fragments(audio={start_time})">
<QualityLevel Index="0" Bitrate="128000" AudioTag="255" SamplingRate="44100" Channels="2" BitsPerSample="16"
CodecPrivateData="1210"/>
<c t="0" d="20000000" r="2"/>
</StreamIndex>
</SmoothStreamingMedia>"""
(audio,) = ISM.from_text(text, "https://cdn.example/x.ism/manifest").to_tracks().audio
assert (audio.sample_rate, audio.bit_depth) == (44100, None)
@pytest.mark.parametrize(
("attrs", "expected"),
[(",BIT-DEPTH=24,SAMPLE-RATE=96000", (24, 96000)), ("", (None, None)), (",SAMPLE-RATE=x", (None, None))],
)
def test_hls_reads_bit_depth_and_sample_rate_when_the_playlist_states_them(
attrs: str, expected: tuple[int | None, int | None]
) -> None:
text = (
"#EXTM3U\n"
f'#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="a",NAME="x",LANGUAGE="en",CHANNELS="2",URI="a.m3u8"{attrs}\n'
'#EXT-X-STREAM-INF:BANDWIDTH=1000000,CODECS="avc1.640028,alac",AUDIO="a"\n'
"v.m3u8\n"
)
(audio,) = HLS.from_text(text, "https://cdn.example/master.m3u8").to_tracks(language="en").audio
assert (audio.bit_depth, audio.sample_rate) == expected
@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(
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)
def test_the_listing_shows_neither_when_unset() -> None:
assert "bit" not in str(Audio(url="https://cdn.example/a.flac", language="en"))
@pytest.mark.parametrize("value", ["24", 24.0])
def test_a_bit_depth_that_is_not_an_int_is_refused(value: object) -> None:
with pytest.raises(TypeError):
Audio(url="https://cdn.example/a.flac", language="en", bit_depth=value) # type: ignore[arg-type]
@pytest.mark.parametrize(
("label", "bit_depth", "sample_rate", "hires"),
[
("FLAC", 24, 48000, True),
("FLAC", 16, 96000, True),
("FLAC 24-bit/96kHz", 16, 44100, False),
("FLAC 24-bit/96kHz", None, None, True),
("FLAC 24-bit/44.1kHz", None, 44100, True),
("16-bit / 96 kHz", 16, None, True),
],
)
def test_hires_is_decided_by_the_fields_before_the_label(
label: str, bit_depth: int | None, sample_rate: int | None, hires: bool
) -> None:
assert MusicRenderer.is_hires_quality(label, bit_depth, sample_rate) is hires
def test_a_song_with_fields_and_no_quality_label_shows_them_with_the_badge() -> None:
from tests.core.test_music_renderer import song
line = MusicRenderer().option_from_song(song(1, "A", None, data={"bit_depth": 24, "sample_rate": 96000}))
assert line.plain.startswith("24-bit / 96 kHz")
assert "Hi-Res" in line.plain
def test_the_header_reads_hires_from_the_fields() -> None:
assert MusicRenderer.quality_summary("FLAC", bit_depth=24, sample_rate=48000) == "Hi-Res Lossless"
assert MusicRenderer.quality_summary("FLAC", bit_depth=16, sample_rate=44100) == "Lossless"
assert MusicRenderer.quality_summary("", bit_depth=24, sample_rate=96000) == "24-bit / 96 kHz"
def test_tracks_that_differ_only_in_bit_depth_are_both_kept() -> None:
a, b = (Audio(id_=str(n), url="https://cdn.example/a.flac", language="en", bit_depth=n) for n in (16, 24))
assert Tracks.signature(a) != Tracks.signature(b)
+35 -3
View File
@@ -131,7 +131,12 @@ def song() -> Song:
disc=1,
year=2022,
language="en",
data={"release_date": "2022-01-02", "composer": "Example Composer", "metadata": {"comment": "nested"}},
data={
"release_date": "2022-01-02",
"composer": "Example Composer",
"sample_rate": 96000,
"metadata": {"comment": "nested"},
},
album_artist="Example Band",
release_type="single",
total_tracks=12,
@@ -205,6 +210,8 @@ def audio(**extra: Any) -> Audio:
channels="5.1",
joc=16,
descriptive=True,
bit_depth=24,
sample_rate=48000,
id_="audio-0001",
)
fields.update(extra)
@@ -283,6 +290,8 @@ def track_fields(track: Any) -> dict[str, Any]:
joc=track.joc,
atmos=track.atmos,
descriptive=track.descriptive,
bit_depth=track.bit_depth,
sample_rate=track.sample_rate,
)
elif isinstance(track, Subtitle):
out.update(cc=track.cc, sdh=track.sdh, forced=track.forced)
@@ -402,7 +411,12 @@ REST_SONG = {
"lyrics_suffix": ".lrc",
"artwork_url": "https://img.example/song.jpg",
"position": 7,
"music_data": {"release_date": "2022-01-02", "composer": "Example Composer", "comment": "nested"},
"music_data": {
"release_date": "2022-01-02",
"composer": "Example Composer",
"sample_rate": 96000,
"comment": "nested",
},
}
META_EPISODE = {
@@ -478,6 +492,8 @@ REST_AUDIO = {
"atmos": True,
"joc": 16,
"descriptive": True,
"bit_depth": 24,
"sample_rate": 48000,
"language": "en",
"is_original_lang": False,
"name": None,
@@ -552,6 +568,8 @@ EXPORT_AUDIO = {
"channels": 5.1,
"joc": 16,
"descriptive": True,
"bit_depth": 24,
"sample_rate": 48000,
}
EXPORT_SUBTITLE = {
@@ -618,6 +636,8 @@ AUDIO_FIELDS = {
"joc": 16,
"atmos": True,
"descriptive": True,
"bit_depth": 24,
"sample_rate": 48000,
}
SUBTITLE_FIELDS = {
@@ -784,7 +804,12 @@ def test_build_title_from_rest_song() -> None:
"id": "song-0001",
"language": "en",
"year": 2022,
"data": {"release_date": "2022-01-02", "composer": "Example Composer", "comment": "nested"},
"data": {
"release_date": "2022-01-02",
"composer": "Example Composer",
"sample_rate": 96000,
"comment": "nested",
},
"name": "Example Song",
**{key: REST_SONG[key] for key in SONG_FIELDS},
}
@@ -921,6 +946,11 @@ def test_export_audio() -> None:
assert list(audio().to_dict()) == list(EXPORT_AUDIO)
def test_export_audio_from_an_older_version_has_no_bit_depth_or_sample_rate() -> None:
old = {k: v for k, v in json_copy(EXPORT_AUDIO).items() if k not in ("bit_depth", "sample_rate")}
assert track_fields(track_from_dict(old)) == {**AUDIO_FIELDS, "bit_depth": None, "sample_rate": None}
def test_export_subtitle() -> None:
assert subtitle().to_dict() == EXPORT_SUBTITLE
assert list(subtitle().to_dict()) == list(EXPORT_SUBTITLE)
@@ -986,6 +1016,8 @@ def test_deserialize_a_track_from_an_older_server() -> None:
"joc": None,
"atmos": True,
"descriptive": False,
"bit_depth": None,
"sample_rate": None,
}
odd_subtitle = deserialize_subtitle({"id": "s-old", "codec": "NOPE", "note": 5})
assert track_fields(odd_subtitle) == {