Files
plezy/lib/utils/codec_utils.dart
T
edde746 f4ce60611b fix(subtitles): let the server deliver subtitles on a transcode
Two regressions since 2.9.1 broke subtitles on transcoded playback. Since
a1b6a8971 sidecars load with the media behind a 10s open guard, so a
subtitle URL the server is slow to serve — Jellyfin extracting an
embedded stream while its transcoder spins up — tripped the guard: stop,
reopen without subtitles, "Selected subtitles could not be loaded"
snackbar, and an emptied subtitle menu. Since 2b3853a88 every embedded
Plex subtitle was handed to the player as a sidecar whose URL is the
original container, so a transcode also range-read and demuxed the
source over HTTP — for a 40 GB remux, purely to find a subtitle track —
which is also why PGS never appeared: the client was handed a container
to demux rather than a rendition to play.

Delivery is the server's job again, backported from the AVPlayer branch
(42ba01440, the subtitle subset of 6852ac274, and a3da81e83) and adapted
to main's mpv backend:

Plex burns every embedded track (subtitles=burn); only a real external
file with a /library/streams key stays a client-fetched sidecar. A burn
is a re-encode, so directPlay is withdrawn — a real PMS answers HTTP 400
to directPlay=1 with burn — and the burn is aimed by selecting the
stream on the part first via the selectStreams PUT, because the decision
endpoint ignores subtitleStreamID alongside subtitles=burn. An
unaimable or undeliverable burn (dvb_teletext) refuses the transcode and
falls back to warned direct play rather than welding the wrong language
in or silently dropping the caption. Main's per-preset
directPlay/directStream pinning is kept; verified against a live PMS
that burn works under directStream=0.

Jellyfin never offers image formats as External, so bitmaps fall through
to Encode and are burned; text External is withheld per request when the
effective selection — including the server's DefaultSubtitleStreamIndex —
is embedded, and offered when it is a real file, so a file is delivered
as a file and never fetched twice. The burned row is excluded from the
sidecars; remaining text rows stay extractable, which is how a secondary
track still renders over a transcode. Sidecar URLs now use the format
extension the endpoint expects instead of the reported codec name.

The controls and selection layers learn what burning means: burn
eligibility is the codec's property, so burned rows stay selectable in
the menu; any change away from a burned selection renegotiates with the
server instead of pretending a local switch worked; the visibility
shortcut explains itself instead of doing nothing; and the track manager
is told when the primary is server-rendered so it stops waiting out a
thirty-second deadline for a native track that is already pixels.

Verified: analyzer parity, clean_translations --check --strict, full
flutter test (5749), and decision-level runs against live Plex and
Jellyfin servers — text and PGS burn decisions, the directPlay=1+burn
400, External file delivery, an unchanged no-burn baseline, and a real
burn session serving its playlist. The pre-commit aggregate was bypassed
for pre-existing main-state findings outside this diff: 21 format-drifted
files and three unused test seams in lib/main.dart.

close #1738

Refs #1815, #1622.
2026-08-09 07:30:47 +02:00

142 lines
3.9 KiB
Dart

/// Utility class for codec-related operations.
///
/// Provides centralized codec name mappings, file extension lookups,
/// and display name formatting.
class CodecUtils {
CodecUtils._();
static String getSubtitleExtension(String? codec) {
if (codec == null) return 'srt';
switch (codec.toLowerCase()) {
case 'subrip':
case 'srt':
return 'srt';
case 'ass':
case 'ssa':
return 'ass';
case 'webvtt':
case 'vtt':
return 'vtt';
case 'mov_text':
return 'srt';
case 'pgs':
case 'pgssub':
case 'hdmv_pgs_subtitle':
return 'sup';
case 'dvd_subtitle':
case 'dvdsub':
case 'vobsub':
case 'dvb_sub':
case 'dvb_subtitle':
return 'sub';
default:
return 'srt';
}
}
static bool isTextSubtitleCodec(String? codec) {
if (codec == null) return false;
return switch (codec.toLowerCase()) {
'srt' || 'subrip' || 'ass' || 'ssa' || 'webvtt' || 'vtt' || 'mov_text' => true,
_ => false,
};
}
/// Image-based (bitmap) subtitle codecs. Plex burns these into the video
/// when the selected output transport cannot carry a bitmap subtitle
/// rendition.
static bool isImageSubtitleCodec(String? codec) {
if (codec == null) return false;
return switch (codec.toLowerCase()) {
'pgs' ||
'pgssub' ||
'hdmv_pgs_subtitle' ||
'dvd_subtitle' ||
'dvdsub' ||
'vobsub' ||
'dvb_sub' ||
// Jellyfin's own spelling, which is what the transcode profile asks it to burn.
'dvbsub' ||
'dvb_subtitle' => true,
_ => false,
};
}
/// Subtitle codecs Plex can deliver in a transcode. Text codecs can become
/// segmented HLS WebVTT; image codecs can be burned into the video.
static bool isTranscodableSubtitleCodec(String? codec) {
return isTextSubtitleCodec(codec) || isImageSubtitleCodec(codec);
}
/// Formats a subtitle codec name to a user-friendly display format.
///
/// Converts internal codec names like 'SUBRIP' to friendly names like 'SRT'.
static String formatSubtitleCodec(String codec) {
final upper = codec.toUpperCase();
return switch (upper) {
'SUBRIP' => 'SRT',
'DVD_SUBTITLE' => 'DVD',
'WEBVTT' => 'VTT',
'HDMV_PGS_SUBTITLE' => 'PGS',
'MOV_TEXT' => 'MOV',
_ => upper,
};
}
/// Formats a video codec name to a user-friendly display format.
///
/// Converts internal codec names like 'hevc' to friendly names like 'HEVC'.
static String formatVideoCodec(String codec) {
final lower = codec.toLowerCase();
return switch (lower) {
'h264' || 'avc1' || 'avc' => 'H.264',
'hevc' || 'h265' || 'hev1' => 'HEVC',
'av1' => 'AV1',
'vp8' => 'VP8',
'vp9' => 'VP9',
'mpeg2video' || 'mpeg2' => 'MPEG-2',
'mpeg4' => 'MPEG-4',
'vc1' => 'VC-1',
_ => codec.toUpperCase(),
};
}
/// Formats an audio channel count as a friendly layout name (2 → 'Stereo',
/// 6 → '5.1'). Returns null when [channels] is null or not positive.
static String? formatAudioChannels(int? channels) {
if (channels == null || channels <= 0) return null;
return switch (channels) {
1 => 'Mono',
2 => 'Stereo',
3 => '3.0',
4 => '4.0',
5 => '4.1',
6 => '5.1',
7 => '6.1',
8 => '7.1',
_ => '${channels}ch',
};
}
/// Formats an audio codec name to a user-friendly display format.
static String formatAudioCodec(String codec) {
final lower = codec.toLowerCase();
return switch (lower) {
'aac' => 'AAC',
'ac3' => 'AC3',
'eac3' || 'ec3' => 'E-AC3',
'truehd' => 'TrueHD',
'dts' => 'DTS',
'dca' => 'DTS',
'dtshd' || 'dts-hd' => 'DTS-HD',
'flac' => 'FLAC',
'mp3' || 'mp3float' => 'MP3',
'opus' => 'Opus',
'vorbis' => 'Vorbis',
'pcm_s16le' || 'pcm_s24le' || 'pcm' => 'PCM',
_ => codec.toUpperCase(),
};
}
}