perf(subsync): avoid streaming remote media for embedded references

- Fetch embedded Jellyfin subtitle tracks through the Subtitles Stream endpoint instead of running ffmpeg over the HTTP stream
- Try external references before extracting embedded tracks on remote streams, and skip embedded extraction when opening the picker
- Run the sampled-audio fast path before the full MKV demux when a non-signs external or sidecar reference exists
- Escalate to a single-pass embedded and audio rip only when sampled verification is inconclusive
This commit is contained in:
sudacode committed 2026-11-26 22:07:13 -08:00
1 parent 904eccc4e7
commit 584dfa3608
4 files changed
+1106 -50

No files matched your search

+41
View File
@@ -4,6 +4,7 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {
buildJellyfinEmbeddedSubtitleUrl,
candidateExecutableNames,
codecToExtension,
extensionToFfmpegMuxer,
@@ -94,3 +95,43 @@ test('Windows candidate discovery excludes extensionless shims and escapes cmd.e
assert.ok(invocation.args[4]!.includes('^('));
assert.ok(invocation.args[4]!.includes('^)'));
});
test('buildJellyfinEmbeddedSubtitleUrl derives direct-play subtitle endpoints and rejects non-direct URLs', () => {
assert.equal(
buildJellyfinEmbeddedSubtitleUrl(
'http://pve-main:8096/Videos/item-1/stream?static=true&ApiKey=tok123&MediaSourceId=ms-1',
2,
'ass',
),
'http://pve-main:8096/Videos/item-1/ms-1/Subtitles/2/Stream.ass?ApiKey=tok123',
);
assert.equal(
buildJellyfinEmbeddedSubtitleUrl(
'https://media.example.test/jellyfin/Videos/item-2/stream.mkv?Static=True&api_key=tok456&mediaSourceId=ms-2',
4,
'ttml',
),
'https://media.example.test/jellyfin/Videos/item-2/ms-2/Subtitles/4/Stream.srt?ApiKey=tok456',
);
// Transcoded or non-static streams must not assume container ff-index matches Jellyfin stream index.
assert.equal(
buildJellyfinEmbeddedSubtitleUrl(
'http://pve-main:8096/Videos/item-1/main.m3u8?ApiKey=tok123&MediaSourceId=ms-1',
2,
'srt',
),
null,
);
assert.equal(
buildJellyfinEmbeddedSubtitleUrl(
'http://pve-main:8096/Videos/item-1/stream?ApiKey=tok123&MediaSourceId=ms-1',
2,
'srt',
),
null,
);
assert.equal(
buildJellyfinEmbeddedSubtitleUrl('https://cdn.example.test/episode.mkv', 2, 'srt'),
null,
);
});
+57
View File
@@ -250,6 +250,63 @@ export function extensionToFfmpegMuxer(extension: string): string {
return extension;
}
/**
* Derive Jellyfin's direct subtitle delivery endpoint for an embedded track on a
* direct-play stream URL (`.../Videos/{itemId}/stream?static=true&MediaSourceId=...&ApiKey=...`).
* Returns null for non-Jellyfin or transcoded URLs where stream indices may not match.
*/
export function buildJellyfinEmbeddedSubtitleUrl(
videoPath: string,
ffIndex: number,
extension: string,
): string | null {
if (!Number.isInteger(ffIndex) || ffIndex < 0 || !extension) return null;
let url: URL;
try {
url = new URL(videoPath);
} catch {
return null;
}
if (url.protocol !== 'http:' && url.protocol !== 'https:') return null;
const findParam = (pattern: RegExp): string | null => {
for (const [key, value] of url.searchParams) {
if (pattern.test(key) && value.trim().length > 0) {
return value.trim();
}
}
return null;
};
if (findParam(/^static$/i)?.toLowerCase() !== 'true') return null;
const apiKey = findParam(/^api_?key$/i);
const mediaSourceId = findParam(/^mediasourceid$/i);
if (!apiKey || !mediaSourceId) return null;
const match = /^(.*)\/videos\/([^/]+)\/stream(?:\.[^/]+)?$/i.exec(url.pathname);
if (!match) return null;
const basePath = match[1] ?? '';
const rawItemId = match[2] ?? '';
if (!rawItemId) return null;
let itemId = rawItemId;
try {
itemId = decodeURIComponent(rawItemId);
} catch {
// Keep raw segment if it contains malformed percent escapes.
}
const deliveryExt = extension === 'ttml' ? 'srt' : extension;
const subtitleUrl = new URL(
`${basePath}/Videos/${encodeURIComponent(itemId)}/${encodeURIComponent(mediaSourceId)}/Subtitles/${ffIndex}/Stream.${deliveryExt}`,
`${url.origin}/`,
);
subtitleUrl.searchParams.set('ApiKey', apiKey);
return subtitleUrl.toString();
}
export function runCommand(
executable: string,
args: string[],