From 54d0460cfb5f229a94e9736eb1bcd977a87d44fa Mon Sep 17 00:00:00 2001 From: Nixevol Date: Thu, 29 Oct 2026 07:19:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=92=AD=E6=94=BE=E5=88=86=E5=BC=80?= =?UTF-8?q?=E7=9A=84=E9=9F=B3=E8=BD=A8=EF=BC=8C=E9=81=BF=E5=85=8D=E5=8F=AA?= =?UTF-8?q?=E6=9C=89=E7=94=BB=E9=9D=A2=E6=B2=A1=E6=9C=89=E5=A3=B0=E9=9F=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/routes/play.py | 10 ++++++++++ server/services/playlist.py | 38 +++++++++++++++++++++++++++++++++++++ tests/test_audio_remux.py | 7 +++++++ 3 files changed, 55 insertions(+) diff --git a/server/routes/play.py b/server/routes/play.py index e71d8d8..cb33a02 100644 --- a/server/routes/play.py +++ b/server/routes/play.py @@ -75,6 +75,16 @@ def play_segment(url: str): return StreamingResponse(chunks(), media_type=media) +@router.get("/media") +def play_media(url: str): + target = _public_url(url) + try: + text = playlists.media_playlist(target) + except RuntimeError as exc: + raise HTTPException(status_code=502, detail=str(exc)) from exc + return Response(text, media_type="application/vnd.apple.mpegurl", headers={"Cache-Control": "no-store"}) + + @router.get("/{vid}/{cid}/index.m3u8") def episode_playlist(vid: int, cid: int): token = sessions.active_token() diff --git a/server/services/playlist.py b/server/services/playlist.py index 5b8d06b..a82b215 100644 --- a/server/services/playlist.py +++ b/server/services/playlist.py @@ -509,6 +509,8 @@ def episode_src(token: str, device: DeviceProfile | None, vid: int, cid: int) -> def playback_playlist(master_url: str) -> str: master_text = http.read_text(master_url) + if audio_rendition_uris(master_text): + return rewrite_master_for_playback(master_text, master_url) variant_rel = next( (line.strip() for line in master_text.splitlines() if line.strip() and not line.startswith("#")), "", @@ -521,6 +523,42 @@ def playback_playlist(master_url: str) -> str: return _rewrite_playlist(http.read_text(variant_url), variant_url) +def media_playlist(url: str) -> str: + return _rewrite_playlist(http.read_text(url), url) + + +def rewrite_master_for_playback(text: str, base: str) -> str: + lines = [] + for line in text.splitlines(): + stripped = line.strip() + if not stripped: + lines.append(line) + continue + if stripped.startswith("#"): + lines.append(_rewrite_master_tag(line, base)) + continue + lines.append(_playlist_proxy_url(urljoin(base, stripped))) + return "\n".join(lines) + "\n" + + +def _playlist_proxy_url(url: str) -> str: + return "/api/play/media?url=" + quote(url, safe="") + + +def _rewrite_master_tag(line: str, base: str) -> str: + updated = line + if "TYPE=AUDIO" in line.upper() and re.search(r"DEFAULT=NO", line, re.I): + updated = re.sub(r"DEFAULT=NO", "DEFAULT=YES", updated, count=1, flags=re.I) + + def replace(match: re.Match) -> str: + target = urljoin(base, match.group(1)) + path = target.split("?", 1)[0].lower() + proxy = _playlist_proxy_url(target) if path.endswith(".m3u8") else _proxy_url(target) + return 'URI="' + proxy + '"' + + return re.sub(r'URI="([^"]*)"', replace, updated) + + def _proxy_url(url: str) -> str: return "/api/play/segment?url=" + quote(url, safe="") diff --git a/tests/test_audio_remux.py b/tests/test_audio_remux.py index be84e72..c6400ce 100644 --- a/tests/test_audio_remux.py +++ b/tests/test_audio_remux.py @@ -23,6 +23,13 @@ def test_guess_and_saved_playlist() -> None: video.m3u8 """ assert audio_rendition_uris(master) == ["audio/audio.m3u8"] + from server.services.playlist import rewrite_master_for_playback + + played = rewrite_master_for_playback(master, "https://cdn.example/show/playlist.m3u8") + assert "DEFAULT=YES" in played + assert "/api/play/media?url=" in played + assert "audio%2Faudio.m3u8" in played or "audio/audio.m3u8" in played + assert played.strip().splitlines()[-1].startswith("/api/play/media?url=") with tempfile.TemporaryDirectory() as tmp: source = Path(tmp)