Files
unshackle/tests/core/test_audio_resolution.py
T

145 lines
6.0 KiB
Python

"""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 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, Video
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
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_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", 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_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])
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