Files
unshackle/tests/core/test_hls_repeated_entries.py
T

93 lines
3.4 KiB
Python

"""A master playlist that lists one variant or one media entry twice gives one track, not an error.
Some manifest proxies list a rendition twice now and then. The track id is a hash of the entry, so the second
copy raised "Duplicate track ID" and failed the whole title.
"""
from __future__ import annotations
import pytest
from unshackle.core.manifests.hls import HLS
URL = "https://example.test/master.m3u8"
AUDIO = '#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="en",NAME="English",LANGUAGE="en",CHANNELS="2",URI="audio/en.m3u8"'
def variant(bandwidth: int, path: str) -> str:
return (
f'#EXT-X-STREAM-INF:BANDWIDTH={bandwidth},RESOLUTION=3840x2160,CODECS="hvc1.2.4.L153.B0,mp4a.40.2",AUDIO="en"\n'
f"{path}\n"
)
def tracks(text: str):
return HLS.from_text(f"#EXTM3U\n{text}", URL).to_tracks(language="en")
def test_a_variant_listed_twice_gets_one_track() -> None:
result = tracks(variant(5_000_000, "video/uhd.m3u8") * 2)
assert [str(video.url) for video in result.videos] == ["https://example.test/video/uhd.m3u8"]
def test_a_media_entry_listed_twice_gets_one_track() -> None:
result = tracks(AUDIO + "\n" + AUDIO + "\n" + variant(5_000_000, "video/uhd.m3u8"))
assert [str(audio.url) for audio in result.audio] == ["https://example.test/audio/en.m3u8"]
def test_variants_with_the_same_attributes_and_other_links_stay_apart() -> None:
result = tracks(variant(5_000_000, "video/uhd.m3u8") + variant(5_000_000, "mirror/uhd.m3u8"))
assert len(result.videos) == 2
PLAIN = '#EXT-X-STREAM-INF:BANDWIDTH=5000000,RESOLUTION=3840x2160,CODECS="hvc1.2.4.L153.B0",VIDEO-RANGE=PQ'
COMPOSITE = PLAIN + ',SUPPLEMENTAL-CODECS="dvh1.08.06/db1p"'
def test_variants_of_one_link_with_another_video_range_stay_apart() -> None:
result = tracks(f"{PLAIN}\nuhd.m3u8\n" + f"{PLAIN.replace('=PQ', '=SDR')}\nuhd.m3u8\n")
assert len(result.videos) == 2
def test_variants_with_other_supplemental_codecs_and_other_links_stay_apart() -> None:
result = tracks(f"{PLAIN}\nhdr10.m3u8\n{COMPOSITE}\ndv.m3u8\n")
flags = {str(video.url).rsplit("/", 1)[-1]: video.dv_compatible_bitstream for video in result.videos}
assert flags == {"hdr10.m3u8": False, "dv.m3u8": True}
@pytest.mark.parametrize("first, second", [(PLAIN, COMPOSITE), (COMPOSITE, PLAIN)], ids=["plain", "composite"])
def test_one_link_listed_with_and_without_supplemental_codecs_keeps_the_dv_flag(first: str, second: str) -> None:
"""python-m3u8 drops SUPPLEMENTAL-CODECS, so the two entries count as one; the flag goes by link, not by entry."""
result = tracks(f"{first}\nuhd.m3u8\n{second}\nuhd.m3u8\n")
assert [video.dv_compatible_bitstream for video in result.videos] == [True]
def test_a_media_entry_listed_twice_with_other_channel_counts_keeps_both() -> None:
result = tracks(
AUDIO.replace('CHANNELS="2"', 'CHANNELS="6"')
+ "\n"
+ AUDIO.replace('CHANNELS="2"', 'CHANNELS="5.1"')
+ "\n"
+ variant(5_000_000, "video/uhd.m3u8")
)
assert len(result.audio) == 2
CC = '#EXT-X-MEDIA:TYPE=CLOSED-CAPTIONS,GROUP-ID="cc",NAME="CC1",INSTREAM-ID="CC1"'
CC_VARIANT = variant(5_000_000, "video/uhd.m3u8").replace('AUDIO="en"', 'AUDIO="en",CLOSED-CAPTIONS="cc"')
def test_a_closed_caption_entry_listed_twice_is_carried_once() -> None:
result = tracks(CC + "\n" + CC + "\n" + CC_VARIANT)
assert result.videos[0].closed_captions == [
{"language": None, "name": "CC1", "instream_id": "CC1", "characteristics": None}
]