mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-03 06:25:10 +00:00
feat(tracks): add Audio bit_depth and sample_rate from manifests and carry them over remote
This commit is contained in:
@@ -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)
|
||||
Reference in New Issue
Block a user