fix(plex): subtitles stop blocking the request, and stop re-reading the file

Bug hunt 2026-09, items 8 and 12 — the two things the Plex player was measurably
lying about.

Item 8: picking a subtitle track showed nothing for a long time, and the reported
workaround was to stop/start until it suddenly worked. Measured on prod against a
17.06 GB MKV with two embedded SubRip tracks:

  full pass, one track                154 s
  full pass, both tracks in ONE run   111 s
  -c:s copy (no WebVTT conversion)    143 s
  from 1:00:00 to the end of file      66 s
  a 630 s window at 1:00:00, both       8 s

So the conversion was never the cost: ffmpeg streams every byte of the container
between its seek point and its stop point, and the array delivers ~140 MB/s. The
only lever is reading less. Three changes follow from that:

  * a window around the playhead is extracted and published FIRST (seconds), and
    the full pass completes behind it;
  * every embedded text track rides that same single read — picking Hungarian used
    to cost a full pass and then switching to English cost another one;
  * none of it happens inside a request any more. The endpoint answers 202 while a
    background worker extracts, the player polls /status (carrying its position, so
    the window follows the viewer), and one ffmpeg run finally has a timeout.

-copyts is deliberately absent from a windowed pass: with it ffmpeg ignores -t and
reads the whole container anyway (measured — a [0, 600 s] window still took the
full 145 s), so a window comes back on a zero-based clock and is shifted to
absolute time here. Cached tracks written by older builds have no coverage sidecar
and count as complete, so nothing is re-extracted after the upgrade.

The <track> is no longer mounted before the server has something to put in it; a
"Preparing subtitles: …" notice stands in, and a track that can never be shown says
why instead of leaving the viewer to conclude the picker does nothing.

Item 12: "Bitrate" read a constant 211 kbps on every remux, because it was the
master playlist's BANDWIDTH — which ffmpeg derives from the one stream whose rate
it knows, the -b:a 192k AAC track, plus 10%. It is a measurement now: bytes per
second of MEDIA from hls.js FRAG_LOADED, accumulated per fragment type and summed
(a multi-audio session's video and audio renditions cover the same media seconds,
so pooling them would report half the truth), and an honest dash until the first
fragment lands. The panel also finally names the audio and subtitle track in play.

Verified in the browser against the real Plex library, and every new test
mutation-checked.
This commit is contained in:
2026-09-10 19:31:05 +02:00
parent b8bff504d8
commit ed4ab4e13c
13 changed files with 1158 additions and 66 deletions
+59 -1
View File
@@ -21,6 +21,14 @@ export interface PlexStatsInput {
totalFrames: number; // getVideoPlaybackQuality().totalVideoFrames
bufferAheadS: number; // seconds buffered ahead of the playhead
hlsLevel: HlsLevelInfo | null; // null for direct play / Safari-native HLS
// MEASURED stream bitrate: bytes actually delivered per second of media, from hls.js's per-fragment
// load stats. Undefined until the first fragment lands (an honest dash — never a stand-in number).
// This row used to read the master playlist's BANDWIDTH, which for a remux session ffmpeg derives
// from the one thing it knows, the `-b:a 192k` AAC track, plus 10%: a constant 211 kbps on every
// remux regardless of the actual video. That is what the viewer correctly refused to believe.
measuredBps?: number;
audioLabel?: string; // the selected audio track, as the tracks menu names it
subLabel?: string; // the selected subtitle track (or the menu's "Off" entry)
sessionStartS: number; // media-clock offset the current session was opened at
abs: number; // current media time (absolute, session offset already folded in)
duration: number;
@@ -138,12 +146,16 @@ export function buildStatRows(s: PlexStatsInput): StatRow[] {
{
key: "bitrate",
labelKey: "plex.player.stats.bitrate",
value: fmtBitrate(s.hlsLevel?.bitrate),
value: fmtBitrate(s.measuredBps),
},
{ key: "level", labelKey: "plex.player.stats.level", value: fmtLevel(s.hlsLevel) },
);
}
rows.push(
// Which audio/subtitle track is actually playing. The player has switched both since E5 and the
// panel never said which one you landed on — the one question "stats for nerds" was being asked.
{ key: "audio", labelKey: "plex.player.stats.audio", value: s.audioLabel || DASH },
{ key: "subtitle", labelKey: "plex.player.stats.subtitle", value: s.subLabel || DASH },
{
key: "frames",
labelKey: "plex.player.stats.frames",
@@ -159,3 +171,49 @@ export function buildStatRows(s: PlexStatsInput): StatRow[] {
);
return rows;
}
// --- measured bitrate ---------------------------------------------------------------------------
interface FragSample {
type: string; // hls.js fragment type: "main" (video, and muxed audio) / "audio" / "subtitle"
bytes: number;
durationS: number; // MEDIA seconds the fragment carries (not how long it took to download)
}
export interface FragMeter {
add(sample: FragSample): void;
/** Bits per second of media, or undefined while nothing has been measured yet. */
bitrate(): number | undefined;
reset(): void;
}
/** Rolling meter over hls.js FRAG_LOADED stats: bytes delivered per second of MEDIA, which is the
* stream's real bitrate — as opposed to download throughput, which only measures the network.
*
* Kept per fragment TYPE and summed: in a multi-audio session the audio ships as its own rendition,
* so video and audio fragments cover the SAME media seconds. Pooling their bytes over the pooled
* durations would report half the truth; per-type rates add up to the whole. */
export function createFragMeter(window = 10): FragMeter {
const byType = new Map<string, FragSample[]>();
return {
add(sample) {
if (!(sample.bytes > 0) || !(sample.durationS > 0)) return; // pre-roll / init segments
const list = byType.get(sample.type) ?? [];
list.push(sample);
if (list.length > window) list.shift();
byType.set(sample.type, list);
},
bitrate() {
let total = 0;
for (const list of byType.values()) {
const bytes = list.reduce((n, f) => n + f.bytes, 0);
const secs = list.reduce((n, f) => n + f.durationS, 0);
if (secs > 0) total += (bytes * 8) / secs;
}
return total > 0 ? total : undefined;
},
reset() {
byType.clear();
},
};
}