fix(hls): give a variant or media entry listed twice one track

This commit is contained in:
imSp4rky
2026-11-29 20:32:24 -07:00
parent 4bcc15dfc4
commit 30ed17054d
2 changed files with 51 additions and 2 deletions
+41
View File
@@ -0,0 +1,41 @@
"""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
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
+10 -2
View File
@@ -192,6 +192,7 @@ class HLS:
All Track objects' URL will be to another M3U(8) document. However, these documents All Track objects' URL will be to another M3U(8) document. However, these documents
will be Invariant Playlists and contain the list of segments URIs among other metadata. will be Invariant Playlists and contain the list of segments URIs among other metadata.
A variant or a media entry that the master lists twice gets one track.
""" """
session_keys = list(self.manifest.session_keys or []) session_keys = list(self.manifest.session_keys or [])
if not session_keys: if not session_keys:
@@ -236,7 +237,12 @@ class HLS:
(raw.get("stream_info") or {}).get("req_video_layout") (raw.get("stream_info") or {}).get("req_video_layout")
for raw in (getattr(self.manifest, "data", None) or {}).get("playlists", []) for raw in (getattr(self.manifest, "data", None) or {}).get("playlists", [])
] ]
variants: set[str] = set()
for playlist, video_layout in zip(self.manifest.playlists, video_layouts, strict=False): for playlist, video_layout in zip(self.manifest.playlists, video_layouts, strict=False):
variant = str(playlist)
if variant in variants:
continue
variants.add(variant)
video_codec: Video.Codec | None = None video_codec: Video.Codec | None = None
audio_codec: Audio.Codec | None = None audio_codec: Audio.Codec | None = None
primary_track_type: type[Audio] | type[Video] primary_track_type: type[Audio] | type[Video]
@@ -273,7 +279,7 @@ class HLS:
tracks.add( tracks.add(
primary_track_type( primary_track_type(
id_=hex(crc32(str(playlist).encode()))[2:], id_=hex(crc32(variant.encode()))[2:],
url=urljoin(playlist.base_uri, playlist.uri), url=urljoin(playlist.base_uri, playlist.uri),
codec=cast(Any, video_codec or audio_codec), codec=cast(Any, video_codec or audio_codec),
language=language, # HLS manifests do not seem to have language info language=language, # HLS manifests do not seem to have language info
@@ -302,9 +308,11 @@ class HLS:
) )
) )
entries: set[str] = set()
for media in self.manifest.media: for media in self.manifest.media:
if not media.uri: if not media.uri or str(media) in entries:
continue continue
entries.add(str(media))
joc = 0 joc = 0
track_type: type[Audio] | type[Subtitle] track_type: type[Audio] | type[Subtitle]