fix: 播放分开的音轨,避免只有画面没有声音
This commit is contained in:
@@ -75,6 +75,16 @@ def play_segment(url: str):
|
|||||||
return StreamingResponse(chunks(), media_type=media)
|
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")
|
@router.get("/{vid}/{cid}/index.m3u8")
|
||||||
def episode_playlist(vid: int, cid: int):
|
def episode_playlist(vid: int, cid: int):
|
||||||
token = sessions.active_token()
|
token = sessions.active_token()
|
||||||
|
|||||||
@@ -509,6 +509,8 @@ def episode_src(token: str, device: DeviceProfile | None, vid: int, cid: int) ->
|
|||||||
|
|
||||||
def playback_playlist(master_url: str) -> str:
|
def playback_playlist(master_url: str) -> str:
|
||||||
master_text = http.read_text(master_url)
|
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(
|
variant_rel = next(
|
||||||
(line.strip() for line in master_text.splitlines() if line.strip() and not line.startswith("#")),
|
(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)
|
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:
|
def _proxy_url(url: str) -> str:
|
||||||
return "/api/play/segment?url=" + quote(url, safe="")
|
return "/api/play/segment?url=" + quote(url, safe="")
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,13 @@ def test_guess_and_saved_playlist() -> None:
|
|||||||
video.m3u8
|
video.m3u8
|
||||||
"""
|
"""
|
||||||
assert audio_rendition_uris(master) == ["audio/audio.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:
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
source = Path(tmp)
|
source = Path(tmp)
|
||||||
|
|||||||
Reference in New Issue
Block a user