Skip to content

The only downloader you need. 下载器的集大成者。

fix(m3u8): 带厂商自定义属性的清单解析失败

XiaoYouChRcf2b2ba4b071
diff --git a/features/m3u8_pack/pack.py b/features/m3u8_pack/pack.py
index 9b3f783f..45798875 100644
--- a/features/m3u8_pack/pack.py
+++ b/features/m3u8_pack/pack.py
@@ -37,6 +37,12 @@ MEDIA_SUFFIXES = {
MANIFEST_SUFFIXES = {".m3u8", ".m3u", ".mpd"}
MANIFEST_CONTENT_TYPES = {"application/vnd.apple.mpegurl", "application/x-mpegurl", "application/mpegurl",
"audio/mpegurl", "audio/x-mpegurl", "application/dash+xml"}
+# Only the tags we read: m3u8 crashes on vendor attributes in other tags, which RFC 8216 says to ignore.
+READ_TAGS = ("#EXTINF", "#EXT-X-ENDLIST", "#EXT-X-STREAM-INF", "#EXT-X-MEDIA:")
+
+
+def parsePlaylist(body: str, uri: str | None = None) -> m3u8.M3U8:
+ return m3u8.loads(body, uri=uri, custom_tags_parser=lambda line, *_: not line.startswith(READ_TAGS))
class M3U8Parser(TaskParser):
@@ -104,7 +110,7 @@ class M3U8Parser(TaskParser):
elif isVimeo:
isLive = False
else:
- playlist = m3u8.loads(body, uri=manifestUrl)
+ playlist = parsePlaylist(body, uri=manifestUrl)
if playlist.segments:
isLive = not playlist.is_endlist
elif playlist.playlists:
@@ -122,7 +128,7 @@ class M3U8Parser(TaskParser):
variantBody = await variantResponse.text()
finally:
variantClient.close()
- isLive = not m3u8.loads(variantBody, uri=variantUrl).is_endlist
+ isLive = not parsePlaylist(variantBody, uri=variantUrl).is_endlist
except Exception as e:
logger.warning("取变体清单判活失败, 按点播处理: {}", repr(e))
isLive = False
@@ -228,7 +234,7 @@ class M3U8Parser(TaskParser):
else:
renditions = [
(media.language or "", media.name or "")
- for media in m3u8.loads(body).media if (media.type or "").upper() == "SUBTITLES"
+ for media in parsePlaylist(body).media if (media.type or "").upper() == "SUBTITLES"
]
except Exception as e:
logger.warning("枚举字幕失败: {}", repr(e))
@@ -250,7 +256,7 @@ class M3U8Parser(TaskParser):
return []
def _hlsStreams(self, body: str) -> list[dict]:
- playlist = m3u8.loads(body)
+ playlist = parsePlaylist(body)
streams = []
for variant in playlist.playlists:
resolution = variant.stream_info.resolution
diff --git a/tests/m3u8_pack/test_task_parser.py b/tests/m3u8_pack/test_task_parser.py
index 7c453eec..ad45e661 100644
--- a/tests/m3u8_pack/test_task_parser.py
+++ b/tests/m3u8_pack/test_task_parser.py
@@ -52,6 +52,27 @@ async def test_parse_playlist_without_suffix_adds_stream(features, startServer):
assert not task.isLive
+# Trimmed from Bilibili live: vendor attributes on Content Steering and variants.
+VENDOR_MASTER = (
+ b'#EXTM3U\n'
+ b'#EXT-X-CONTENT-STEERING:SERVER-URI="reserved",PATHWAY-ID="https://cdn.example",'
+ b'BILI-PATHWAY-PRIORITY="https://cdn.example",BILI-CTR-MODE=1,BILI-TTL=150\n'
+ b'#EXT-X-STREAM-INF:BANDWIDTH=1996000,RESOLUTION=1920x1080,CODECS="hvc1.1.6.L150,mp4a.40.2",BILI-QN=10000\n'
+ b'index.m3u8?qn=10000\n'
+ b'#EXT-X-STREAM-INF:BANDWIDTH=871556,RESOLUTION=1280x720,CODECS="hvc1.1.6.L120,mp4a.40.2",BILI-QN=250\n'
+ b'index.m3u8?qn=250\n'
+)
+
+
+async def test_parse_master_with_vendor_attributes_adds_stream(features, startServer):
+ url = await startServer(buildTypedHandler(VENDOR_MASTER, "application/vnd.apple.mpegurl"))
+
+ task = await features.parse(TaskOptions(url=url))
+
+ assert isinstance(task, M3U8Task)
+ assert [(stream["width"], stream["height"]) for stream in task.streams] == [(1920, 1080), (1280, 720)]
+
+
async def test_parse_broken_playlist_without_suffix_adds_plain_file(features, startServer):
url = await startServer(buildTypedHandler(b"<html>expired</html>", "application/vnd.apple.mpegurl"))