fix: 播放分开的音轨,避免只有画面没有声音
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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="")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user