Compare commits

...
3 Commits
8 changed files with 354 additions and 69 deletions

No files matched your search

@@ -0,0 +1,5 @@
type: fixed
area: jellyfin
- Jellyfin playback now selects the Japanese subtitle track, and starts subtitle annotations, as soon as that track downloads instead of waiting for every other subtitle track. Tracks now download in parallel, so a slow embedded track Jellyfin has to extract no longer delays the primary subtitles.
- Jellyfin episodes with image-based embedded subtitles (PGS, DVD, DVB) now auto-select the Japanese and English tracks. SubMiner no longer requests those tracks as text, and a subtitle track that fails to download no longer cancels selection of the others.
+4
View File
@@ -0,0 +1,4 @@
type: fixed
area: jellyfin
- Timing review previews and mined card audio now use the audio track you are playing on Jellyfin direct-play streams, instead of the track Jellyfin marks as default.
+24
View File
@@ -213,6 +213,30 @@ test('resolveMediaGenerationInput reads file-local mpv request options', async (
});
});
test('resolveMediaGenerationInput keeps stream selection for a directly played remote container', async () => {
const resolver = (
mediaSource as typeof mediaSource & {
resolveMediaGenerationInput?: StructuredMediaResolver;
}
).resolveMediaGenerationInput;
assert.equal(typeof resolver, 'function');
// Jellyfin direct play: mpv opens the URL as-is, so it can hold several audio streams.
const jellyfinUrl = 'http://jellyfin.local:8096/Videos/abc/stream?static=true';
const result = await resolver!(
{
currentVideoPath: jellyfinUrl,
requestProperty: async (name: string) =>
name === 'stream-open-filename' ? jellyfinUrl : null,
},
'audio',
);
assert.equal(result?.path, jellyfinUrl);
assert.equal(result?.source, 'stream-open-filename');
assert.equal(result?.singleResolvedStream, false);
});
test('resolveMediaGenerationInput prefers a ready cached media file for YouTube extraction', async () => {
const resolver = (
mediaSource as typeof mediaSource & {
+4 -1
View File
@@ -400,12 +400,15 @@ export async function resolveMediaGenerationInput(
return result;
}
if (streamOpenFilename) {
// A URL that yt-dlp resolved (e.g. youtube.com -> googlevideo) is one picked stream.
// When mpv opens the path as-is (e.g. Jellyfin direct play), it is the full container,
// so mpv's audio stream index still applies.
const result = await toResolvedMediaGenerationInput(
mpvClient,
streamOpenFilename,
kind,
'stream-open-filename',
isRemoteMediaPath(streamOpenFilename),
isRemoteMediaPath(streamOpenFilename) && streamOpenFilename !== currentVideoPath,
);
logResolvedMediaGenerationInput(options, currentVideoPath, result);
return result;
+11 -2
View File
@@ -377,6 +377,14 @@ test('listSubtitleTracks returns all subtitle streams with delivery urls', async
DeliveryUrl: 'https://cdn.example.com/subs.srt',
IsExternalUrl: true,
},
{
Type: 'Subtitle',
Index: 5,
Codec: 'PGSSUB',
Language: 'eng',
DisplayTitle: 'English PGS',
DeliveryMethod: 'Embed',
},
],
},
],
@@ -395,10 +403,10 @@ test('listSubtitleTracks returns all subtitle streams with delivery urls', async
clientInfo,
'movie-1',
);
assert.equal(tracks.length, 3);
assert.equal(tracks.length, 4);
assert.deepEqual(
tracks.map((track) => track.index),
[2, 3, 4],
[2, 3, 4, 5],
);
assert.equal(
tracks[0]!.deliveryUrl,
@@ -409,6 +417,7 @@ test('listSubtitleTracks returns all subtitle streams with delivery urls', async
'http://jellyfin.local/Videos/movie-1/ms-1/Subtitles/3/Stream.srt?ApiKey=token',
);
assert.equal(tracks[2]!.deliveryUrl, 'https://cdn.example.com/subs.srt');
assert.equal(tracks[3]!.deliveryUrl, null, 'bitmap subtitles cannot be fetched as text');
} finally {
globalThis.fetch = originalFetch;
}
+13
View File
@@ -146,6 +146,17 @@ function setApiKeyParam(url: URL, accessToken: string): void {
url.searchParams.set('ApiKey', accessToken);
}
const IMAGE_SUBTITLE_CODECS = new Set([
'pgssub',
'pgs',
'hdmv_pgs_subtitle',
'dvdsub',
'dvd_subtitle',
'dvbsub',
'dvb_subtitle',
'xsub',
]);
function resolveDeliveryUrl(
session: JellyfinAuthSession,
stream: JellyfinMediaStream,
@@ -163,6 +174,8 @@ function resolveDeliveryUrl(
const streamIndex = asIntegerOrNull(stream.Index);
if (streamIndex === null || !itemId || !mediaSourceId) return null;
const codec = ensureString(stream.Codec).toLowerCase();
// Jellyfin cannot convert bitmap subtitles to text, so requesting them always fails.
if (IMAGE_SUBTITLE_CODECS.has(codec)) return null;
const ext =
codec === 'subrip'
? 'srt'
@@ -363,6 +363,164 @@ test('preload jellyfin subtitles waits for delayed external japanese track inste
]);
});
test('preload jellyfin subtitles selects japanese before slower tracks finish downloading', async () => {
const commands: Array<Array<string | number>> = [];
let releaseSlowTrack!: () => void;
const slowTrackBlocked = new Promise<void>((resolve) => {
releaseSlowTrack = resolve;
});
const mpvTracks: Array<Record<string, unknown>> = [];
const preload = createPreloadJellyfinExternalSubtitlesHandler(
makeDeps({
listJellyfinSubtitleTracks: async () => [
{ index: 0, language: 'eng', title: 'English', deliveryUrl: 'https://sub/eng.ass' },
{ index: 1, language: 'jpn', title: 'Japanese', deliveryUrl: 'https://sub/jpn.srt' },
],
getMpvClient: () => ({ requestProperty: async () => mpvTracks }),
cacheSubtitleTrack: async (track) => {
if (track.index === 0) {
await slowTrackBlocked;
}
return {
path: `/tmp/subminer-jellyfin-subtitles/${track.index}.srt`,
cleanupDir: '/tmp/subminer-jellyfin-subtitles',
};
},
sendMpvCommand: (command) => {
commands.push(command);
if (command[0] === 'sub-add') {
mpvTracks.push({
type: 'sub',
id: mpvTracks.length + 1,
lang: command[4],
title: command[3],
external: true,
'external-filename': command[1],
});
}
},
}),
);
const done = preload({ session, clientInfo, itemId: 'item-1' });
const hasJapanesePrimary = () =>
commands.some(
(command) => command[0] === 'set_property' && command[1] === 'sid' && command[2] === 1,
);
for (let tick = 0; tick < 1000 && !hasJapanesePrimary(); tick += 1) {
await new Promise((resolve) => setImmediate(resolve));
}
const selectedBeforeSlowTrack = setPropertyCommandsExceptTrackAutoSelection(commands);
// Release before asserting so a regression fails here instead of leaving the preload hanging.
releaseSlowTrack();
await done;
assert.deepEqual(selectedBeforeSlowTrack, [['set_property', 'sid', 1]]);
assert.deepEqual(setPropertyCommandsExceptTrackAutoSelection(commands), [
['set_property', 'sid', 1],
['set_property', 'secondary-sid', 2],
]);
});
test('preload jellyfin subtitles does not lock in a fallback japanese track during early selection', async () => {
const commands: Array<Array<string | number>> = [];
let requestCount = 0;
const fallbackJapanese = {
type: 'sub',
id: 5,
lang: 'jpn',
title: 'Japanese SDH',
external: true,
'external-filename': '/tmp/subminer-jellyfin-subtitles/0.srt',
};
const preferredJapanese = {
type: 'sub',
id: 6,
lang: 'jpn',
title: 'Japanese',
external: true,
'external-filename': '/tmp/subminer-jellyfin-subtitles/1.srt',
};
const preload = createPreloadJellyfinExternalSubtitlesHandler(
makeDeps({
listJellyfinSubtitleTracks: async () => [
{ index: 0, language: 'jpn', title: 'Japanese SDH', deliveryUrl: 'https://sub/sdh.srt' },
{
index: 1,
language: 'jpn',
title: 'Japanese',
isDefault: true,
deliveryUrl: 'https://sub/jpn.srt',
},
],
getMpvClient: () => ({
requestProperty: async () => {
requestCount += 1;
// mpv lists the preferred track only after the early selection poll gives up.
return requestCount <= 10 ? [fallbackJapanese] : [fallbackJapanese, preferredJapanese];
},
}),
sendMpvCommand: (command) => commands.push(command),
}),
);
await preload({ session, clientInfo, itemId: 'item-1' });
assert.deepEqual(setPropertyCommandsExceptTrackAutoSelection(commands), [
['set_property', 'sid', 6],
]);
});
test('preload jellyfin subtitles still selects remaining tracks when one download fails', async () => {
const commands: Array<Array<string | number>> = [];
const preload = createPreloadJellyfinExternalSubtitlesHandler(
makeDeps({
listJellyfinSubtitleTracks: async () => [
{ index: 0, language: 'eng', title: 'English', deliveryUrl: 'https://sub/eng.srt' },
{ index: 1, language: 'jpn', title: 'Japanese', deliveryUrl: 'https://sub/jpn.srt' },
{ index: 5, language: 'eng', title: 'English PGS', deliveryUrl: 'https://sub/pgs.srt' },
],
getMpvClient: () => ({
requestProperty: async () => [
{
type: 'sub',
id: 1,
lang: 'eng',
title: 'English',
external: true,
'external-filename': '/tmp/subminer-jellyfin-subtitles/0.srt',
},
{
type: 'sub',
id: 2,
lang: 'jpn',
title: 'Japanese',
external: true,
'external-filename': '/tmp/subminer-jellyfin-subtitles/1.srt',
},
],
}),
cacheSubtitleTrack: async (track) => {
if (track.index === 5) {
throw new Error('Failed to download Jellyfin subtitle (HTTP 400)');
}
return {
path: `/tmp/subminer-jellyfin-subtitles/${track.index}.srt`,
cleanupDir: '/tmp/subminer-jellyfin-subtitles',
};
},
sendMpvCommand: (command) => commands.push(command),
}),
);
await preload({ session, clientInfo, itemId: 'item-1' });
assert.deepEqual(setPropertyCommandsExceptTrackAutoSelection(commands), [
['set_property', 'sid', 2],
['set_property', 'secondary-sid', 1],
]);
});
test('preload jellyfin subtitles clears managed delay when no external tracks are available', async () => {
const commands: Array<Array<string | number>> = [];
const preload = createPreloadJellyfinExternalSubtitlesHandler(
+135 -66
View File
@@ -129,24 +129,48 @@ function pickBestCachedTrackId(
: false,
)
.filter(({ track }) => track.id !== excludeId)
.map(({ track, cached }) => {
const title = cached?.source.title || track.title;
return {
track,
score:
(track.external ? 100 : 0) +
(cached?.source.isDefault ? 35 : 0) +
(cached?.source.isExternal === false ? 25 : 0) +
(cached?.source.isExternal === true ? -10 : 0) +
(cached?.source.isForced ? -25 : 0) +
(isLikelyHearingImpaired(title) ? -10 : 10) +
(/\bdefault\b/i.test(title) ? 3 : 0),
};
})
.flatMap(({ track, cached }) =>
cached
? [
{
track,
score:
(track.external ? 100 : 0) +
scoreJellyfinSource(cached.source, cached.source.title || track.title),
},
]
: [],
)
.sort((a, b) => b.score - a.score);
return ranked[0]?.track.id ?? null;
}
// Ranks Jellyfin subtitle sources by metadata alone, so the preferred track is known before download.
function scoreJellyfinSource(source: JellyfinSubtitleTrack, title: string): number {
return (
(source.isDefault ? 35 : 0) +
(source.isExternal === false ? 25 : 0) +
(source.isExternal === true ? -10 : 0) +
(source.isForced ? -25 : 0) +
(isLikelyHearingImpaired(title) ? -10 : 10) +
(/\bdefault\b/i.test(title) ? 3 : 0)
);
}
function pickPreferredJapaneseSource(
sources: JellyfinSubtitleTrack[],
): JellyfinSubtitleTrack | null {
const ranked = sources
.filter((source) => isJapanese(source.language || '') || isJapanese(source.title || ''))
.map((source) => ({ source, score: scoreJellyfinSource(source, source.title || '') }))
.sort((a, b) => b.score - a.score);
return ranked[0]?.source ?? null;
}
function findMpvTrackIdByPath(tracks: MpvSubtitleTrack[], filePath: string): number | null {
return tracks.find((track) => track.externalFilename === filePath)?.id ?? null;
}
function findCachedTrackForMpvTrackId(
tracks: MpvSubtitleTrack[],
cachedTracks: CachedExternalSubtitleTrack[],
@@ -282,6 +306,22 @@ export function createPreloadJellyfinExternalSubtitlesHandler(deps: {
});
}
function selectJapanesePrimary(
subtitleTracks: MpvSubtitleTrack[],
cachedTracks: CachedExternalSubtitleTrack[],
trackId: number | null,
): void {
if (trackId === null) {
deps.sendMpvCommand(['set_property', 'sid', 'no']);
return;
}
deps.sendMpvCommand(['set_property', 'sid', trackId]);
const selectedCachedTrack = findCachedTrackForMpvTrackId(subtitleTracks, cachedTracks, trackId);
if (selectedCachedTrack) {
startSubtitlePrefetchForCachedTrack(selectedCachedTrack.path);
}
}
function cleanupActiveCache(): void {
const dirs = [...activeCacheDirs];
if (dirs.length === 0) return;
@@ -319,64 +359,93 @@ export function createPreloadJellyfinExternalSubtitlesHandler(deps: {
deps.sendMpvCommand(['set_property', 'secondary-sub-visibility', 'no']);
await deps.wait(300);
const seenUrls = new Set<string>();
const cachedTracks: CachedExternalSubtitleTrack[] = [];
for (const track of externalTracks) {
if (!track.deliveryUrl || seenUrls.has(track.deliveryUrl)) {
continue;
}
const uniqueTracks = externalTracks.filter((track) => {
if (!track.deliveryUrl || seenUrls.has(track.deliveryUrl)) return false;
seenUrls.add(track.deliveryUrl);
const labelBase = (track.title || track.language || '').trim();
const label = labelBase || `Jellyfin Subtitle ${track.index}`;
const cached = await deps.cacheSubtitleTrack(track);
activeCacheDirs.add(cached.cleanupDir);
cachedTracks.push({ ...cached, source: track });
deps.sendMpvCommand(['sub-add', cached.path, 'auto', label, track.language || '']);
}
return true;
});
await deps.wait(TRACK_SELECTION_INITIAL_WAIT_MS);
const shouldWaitForExternalJapanese = externalTracks.some(
(track) => isJapanese(track.language || '') || isJapanese(track.title || ''),
// Download every track at once and add each to mpv as soon as it lands. Jellyfin has
// to extract embedded tracks from the container, so one slow track must not hold up
// the Japanese primary that annotations depend on. A failed track is skipped rather
// than failing the whole preload, so the remaining tracks still get selected.
const cachedTracks: CachedExternalSubtitleTrack[] = [];
const downloads = new Map(
uniqueTracks.map((track) => [
track,
(async (): Promise<CachedExternalSubtitleTrack | null> => {
const labelBase = (track.title || track.language || '').trim();
const label = labelBase || `Jellyfin Subtitle ${track.index}`;
let cached: CachedExternalSubtitleTrack;
try {
cached = { ...(await deps.cacheSubtitleTrack(track)), source: track };
} catch (error) {
deps.logDebug(`Failed to download Jellyfin subtitle track ${track.index}`, error);
return null;
}
activeCacheDirs.add(cached.cleanupDir);
cachedTracks.push(cached);
deps.sendMpvCommand(['sub-add', cached.path, 'auto', label, track.language || '']);
return cached;
})(),
]),
);
const subtitleTracks = await waitForPreferredSubtitleTracks(
deps,
shouldWaitForExternalJapanese,
cachedTracks.map((track) => track.path),
);
if (
shouldWaitForExternalJapanese &&
(!subtitleTracks || !hasExternalJapaneseTrack(subtitleTracks))
) {
deps.logDebug('Timed out waiting for Jellyfin Japanese subtitle track', {
itemId: params.itemId,
});
return;
}
const allDownloads = Promise.all(downloads.values());
const resolvedSubtitleTracks = subtitleTracks ?? [];
const japanesePrimaryId =
pickBestCachedTrackId(resolvedSubtitleTracks, cachedTracks, isJapanese) ??
pickBestTrackId(resolvedSubtitleTracks, isJapanese);
const englishSecondaryId =
pickBestCachedTrackId(resolvedSubtitleTracks, cachedTracks, isEnglish, japanesePrimaryId) ??
pickBestTrackId(resolvedSubtitleTracks, isEnglish, japanesePrimaryId);
if (japanesePrimaryId !== null) {
const selectedCachedTrack = findCachedTrackForMpvTrackId(
resolvedSubtitleTracks,
cachedTracks,
japanesePrimaryId,
);
if (selectedCachedTrack) {
deps.sendMpvCommand(['set_property', 'sid', japanesePrimaryId]);
startSubtitlePrefetchForCachedTrack(selectedCachedTrack.path);
} else {
deps.sendMpvCommand(['set_property', 'sid', japanesePrimaryId]);
try {
let subtitleTracks: MpvSubtitleTrack[] = [];
let japanesePrimaryId: number | null | undefined;
const preferredJapaneseSource = pickPreferredJapaneseSource(uniqueTracks);
const preferredJapanese = preferredJapaneseSource
? await downloads.get(preferredJapaneseSource)
: null;
if (preferredJapanese) {
await deps.wait(TRACK_SELECTION_INITIAL_WAIT_MS);
subtitleTracks =
(await waitForPreferredSubtitleTracks(deps, true, [preferredJapanese.path])) ?? [];
if (!hasExternalJapaneseTrack(subtitleTracks)) {
deps.logDebug('Timed out waiting for Jellyfin Japanese subtitle track', {
itemId: params.itemId,
});
return;
}
// Only commit early to the preferred track. If mpv has not listed it yet, leave the
// choice to the full ranking below instead of locking in a lower-ranked fallback.
const preferredJapaneseTrackId = findMpvTrackIdByPath(
subtitleTracks,
preferredJapanese.path,
);
if (preferredJapaneseTrackId !== null) {
japanesePrimaryId = preferredJapaneseTrackId;
selectJapanesePrimary(subtitleTracks, cachedTracks, japanesePrimaryId);
}
}
} else {
deps.sendMpvCommand(['set_property', 'sid', 'no']);
}
if (englishSecondaryId !== null) {
deps.sendMpvCommand(['set_property', 'secondary-sid', englishSecondaryId]);
await allDownloads;
const cachedPaths = cachedTracks.map((track) => track.path);
if (!hasExpectedExternalSubtitleTracks(subtitleTracks, cachedPaths)) {
await deps.wait(TRACK_SELECTION_INITIAL_WAIT_MS);
subtitleTracks = (await waitForPreferredSubtitleTracks(deps, false, cachedPaths)) ?? [];
}
if (japanesePrimaryId === undefined) {
japanesePrimaryId =
pickBestCachedTrackId(subtitleTracks, cachedTracks, isJapanese) ??
pickBestTrackId(subtitleTracks, isJapanese);
selectJapanesePrimary(subtitleTracks, cachedTracks, japanesePrimaryId);
}
const englishSecondaryId =
pickBestCachedTrackId(subtitleTracks, cachedTracks, isEnglish, japanesePrimaryId) ??
pickBestTrackId(subtitleTracks, isEnglish, japanesePrimaryId);
if (englishSecondaryId !== null) {
deps.sendMpvCommand(['set_property', 'secondary-sid', englishSecondaryId]);
}
} finally {
// Keep this run in the queue until every download has registered its cache dir, so
// the next run's cleanup sees them all.
await allDownloads;
}
} catch (error) {
deps.logDebug('Failed to preload Jellyfin external subtitles', error);