fix(plex): play subtitles as WebVTT tracks (external sidecar subs no longer crash)
Selecting a subtitle restarted the HLS session with `-map 0:s:{ord}`, assuming an
EMBEDDED stream. Films whose subs are external sidecar .srt files (Plex reports
them, but they aren't in the mkv) matched no stream; the master-playlist's
declared subtitle group then made ffmpeg fail → "Playback couldn't start".
Subtitles now go through a new GET /api/plex/subtitle/{rk}/{ord} → text/vtt
(external subs fetched from Plex via the stream key + SRT→VTT; embedded text subs
extracted with ffmpeg; image subs → 415), served as native <video><track> that
the browser overlays. So choosing/switching a subtitle is instant with NO session
restart, and stream.py drops all subtitle muxing (`-sn`, no master playlist).
Image-based subs (PGS/VobSub) are marked text=false and hidden in the picker.
Verified on prod's Nymphomaniac Vol. II: HU sidecar → 1693 WebVTT cues, no crash.
This commit is contained in:
parent
335e4d7c76
commit
c8c027d0fc
7 changed files with 206 additions and 89 deletions
|
|
@ -48,24 +48,17 @@ class HlsSession:
|
|||
self.last_access = time.time()
|
||||
|
||||
|
||||
def _ffmpeg_cmd(
|
||||
src: Path, start_s: float, out_dir: Path, audio_ord: int | None, sub_ord: int | None
|
||||
) -> list[str]:
|
||||
def _ffmpeg_cmd(src: Path, start_s: float, out_dir: Path, audio_ord: int | None) -> list[str]:
|
||||
args = ["ffmpeg", "-nostdin", "-loglevel", "error"]
|
||||
if start_s > 0:
|
||||
args += ["-ss", f"{start_s:.3f}"] # fast keyframe seek before -i
|
||||
args += ["-i", str(src)]
|
||||
# Video always stream-copied. The selected audio track (default = first) is transcoded to AAC.
|
||||
# A selected subtitle track is muxed in as a WebVTT rendition (ffmpeg aligns it to this session's
|
||||
# timeline, so it stays in sync even after a seek-restart); no selection → drop subtitles.
|
||||
args += ["-map", "0:v:0", "-map", f"0:a:{audio_ord if audio_ord is not None else 0}?"]
|
||||
if sub_ord is not None:
|
||||
args += ["-map", f"0:s:{sub_ord}?", "-c:s", "webvtt"]
|
||||
else:
|
||||
args += ["-sn"]
|
||||
# Video always stream-copied; the selected audio track (default = first) is transcoded to AAC.
|
||||
# Subtitles are NOT muxed here — they're served as standalone WebVTT tracks (GET /subtitle) that
|
||||
# the browser overlays, so choosing a subtitle no longer restarts this session (and external
|
||||
# sidecar subs, which aren't in the file, work — the old `-map 0:s` broke on those).
|
||||
args += ["-map", "0:v:0", "-map", f"0:a:{audio_ord if audio_ord is not None else 0}?", "-sn"]
|
||||
args += ["-c:v", "copy", "-c:a", "aac", "-ac", "2", "-b:a", "192k"]
|
||||
if sub_ord is not None:
|
||||
args += ["-c:s", "webvtt"]
|
||||
args += [
|
||||
"-f", "hls",
|
||||
"-hls_time", str(_SEG_SECONDS),
|
||||
|
|
@ -77,10 +70,6 @@ def _ffmpeg_cmd(
|
|||
"-hls_segment_type", "mpegts",
|
||||
"-hls_flags", "independent_segments+temp_file",
|
||||
]
|
||||
if sub_ord is not None:
|
||||
# A subtitle rendition needs a MASTER playlist tying the video variant to the WebVTT group
|
||||
# (ffmpeg generates the subtitle segments per-session, so they stay in sync after a seek).
|
||||
args += ["-master_pl_name", "master.m3u8", "-var_stream_map", "v:0,a:0,s:0,sgroup:subs"]
|
||||
args += ["-hls_segment_filename", str(out_dir / "seg_%d.ts"), str(out_dir / "index.m3u8")]
|
||||
return args
|
||||
|
||||
|
|
@ -111,10 +100,10 @@ def start_session(
|
|||
item: PlexItem,
|
||||
start_s: float,
|
||||
audio_ord: int | None = None,
|
||||
sub_ord: int | None = None,
|
||||
) -> HlsSession | None:
|
||||
"""(Re)start the HLS remux for an item at the given offset, with an optional selected audio /
|
||||
subtitle track (ordinals among their stream type). Returns None if the local file can't be read."""
|
||||
"""(Re)start the HLS remux for an item at the given offset, with an optional selected audio track
|
||||
(ordinal among audio streams). Subtitles are separate WebVTT tracks (not muxed here). Returns
|
||||
None if the local file can't be read."""
|
||||
src = paths.local_media_path(db, item.file_path)
|
||||
if src is None:
|
||||
return None
|
||||
|
|
@ -124,21 +113,18 @@ def start_session(
|
|||
old = _sessions.pop(key, None)
|
||||
if old is not None:
|
||||
_kill(old)
|
||||
directory = _HLS_ROOT / f"{key}_{int(start_s)}_{audio_ord}_{sub_ord}"
|
||||
directory = _HLS_ROOT / f"{key}_{int(start_s)}_{audio_ord}"
|
||||
shutil.rmtree(directory, ignore_errors=True)
|
||||
directory.mkdir(parents=True, exist_ok=True)
|
||||
proc = subprocess.Popen(
|
||||
_ffmpeg_cmd(src, start_s, directory, audio_ord, sub_ord),
|
||||
_ffmpeg_cmd(src, start_s, directory, audio_ord),
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
entry = "master.m3u8" if sub_ord is not None else "index.m3u8"
|
||||
s = HlsSession(key, directory, proc, start_s, entry)
|
||||
s = HlsSession(key, directory, proc, start_s, "index.m3u8")
|
||||
_sessions[key] = s
|
||||
_enforce_cap()
|
||||
log.info(
|
||||
"plex hls session start key=%s start=%.1f audio=%s sub=%s", key, start_s, audio_ord, sub_ord
|
||||
)
|
||||
log.info("plex hls session start key=%s start=%.1f audio=%s", key, start_s, audio_ord)
|
||||
return s
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue