fix(jellyfin): keep episode titles from leaking across playlist entries

- Pass force-media-title as a per-file loadfile option for every Jellyfin play instead of setting it globally first
- Skip mpv media-title updates for Jellyfin videos in the immersion tracker so a stale title can't replace the canonical one
This commit is contained in:
sudacode committed 2026-11-23 03:07:32 -08:00
1 parent 24cd8cae88
commit 935af26cca
4 files changed
+46 -23

No files matched your search

@@ -2949,6 +2949,38 @@ test('Jellyfin metadata refresh preserves a manual episode assignment', async ()
} }
}); });
test('mpv media-title updates do not overwrite a Jellyfin video title', async () => {
const dbPath = makeDbPath();
let tracker: ImmersionTrackerService | null = null;
try {
const Ctor = await loadTrackerCtor();
tracker = new Ctor({ dbPath });
const streamUrl = 'http://jellyfin.local/Videos/item-3/stream?static=true&api_key=token';
tracker.recordJellyfinPlaybackMetadata({
mediaPath: streamUrl,
displayTitle: 'Show S01E03 Third',
itemTitle: 'Third',
seriesTitle: 'Show',
seasonNumber: 1,
episodeNumber: 3,
itemId: 'item-3',
});
tracker.handleMediaChange(streamUrl, 'Show S01E03 Third');
// mpv restores the previous episode's title as it leaves this playlist entry.
tracker.handleMediaTitleUpdate('Show S01E02 Second');
tracker.handleMediaChange(streamUrl, 'Show S01E02 Second');
const row = (tracker as unknown as { db: DatabaseSync }).db
.prepare('SELECT canonical_title AS title FROM imm_videos')
.get() as { title: string };
assert.equal(row.title, 'Show S01E03 Third');
} finally {
tracker?.destroy();
cleanupDbPath(dbPath);
}
});
test('startup repairs existing Jellyfin stream video links to metadata rows', async () => { test('startup repairs existing Jellyfin stream video links to metadata rows', async () => {
const dbPath = makeDbPath(); const dbPath = makeDbPath();
let tracker: ImmersionTrackerService | null = null; let tracker: ImmersionTrackerService | null = null;
@@ -2464,6 +2464,9 @@ export class ImmersionTrackerService {
private updateVideoTitleForActiveSession(canonicalTitle: string): void { private updateVideoTitleForActiveSession(canonicalTitle: string): void {
if (!this.sessionState) return; if (!this.sessionState) return;
// Jellyfin's title is authoritative. mpv's media-title can still name the previous playlist
// entry while it switches files, so it must not overwrite a Jellyfin video's title.
if (this.hasJellyfinMetadata(this.sessionState.videoId)) return;
updateVideoTitleRecord(this.db, this.sessionState.videoId, canonicalTitle); updateVideoTitleRecord(this.db, this.sessionState.videoId, canonicalTitle);
} }
} }
@@ -159,13 +159,12 @@ test('playback handler disables mpv subtitle selection, then loads media and rep
['set_property', 'sub-visibility', 'no'], ['set_property', 'sub-visibility', 'no'],
['set_property', 'secondary-sub-visibility', 'no'], ['set_property', 'secondary-sub-visibility', 'no'],
['script-message', 'subminer-managed-subtitles-loading'], ['script-message', 'subminer-managed-subtitles-loading'],
['set_property', 'force-media-title', 'Episode 1'],
[ [
'loadfile', 'loadfile',
'https://stream.example/video.m3u8', 'https://stream.example/video.m3u8',
'replace', 'replace',
-1, -1,
`${SUPPRESSED_SUBTITLE_OPTIONS},start=1.2`, `${SUPPRESSED_SUBTITLE_OPTIONS},start=1.2,force-media-title=%9%Episode 1`,
], ],
]); ]);
assert.equal(harness.activeStates.length, 1); assert.equal(harness.activeStates.length, 1);
@@ -314,9 +313,11 @@ test('playback handler publishes Jellyfin title before loading tokenized stream
const loadIndex = harness.events.indexOf('cmd:loadfile'); const loadIndex = harness.events.indexOf('cmd:loadfile');
assert.ok(titleIndex >= 0); assert.ok(titleIndex >= 0);
assert.ok(titleIndex < loadIndex); assert.ok(titleIndex < loadIndex);
const mpvTitleIndex = harness.commands.findIndex((command) => command[1] === 'force-media-title'); // mpv gets the title as a per-file option, so it never falls back to the URL.
assert.ok( assert.ok(
mpvTitleIndex >= 0 && mpvTitleIndex < harness.commands.findIndex((c) => c[0] === 'loadfile'), String(harness.loadfile()?.[4]).endsWith(
',force-media-title=%30%Galaxy Quest S02E07 A New Hope',
),
); );
}); });
@@ -373,7 +374,7 @@ for (const c of HOOK_FAILURE_CASES) {
'https://stream.example/video.m3u8', 'https://stream.example/video.m3u8',
'replace', 'replace',
-1, -1,
SUPPRESSED_SUBTITLE_OPTIONS, `${SUPPRESSED_SUBTITLE_OPTIONS},force-media-title=%9%Episode 1`,
]); ]);
}); });
} }
+5 -18
View File
@@ -135,18 +135,14 @@ function quoteMpvOptionValue(value: string): string {
return `%${Buffer.byteLength(value, 'utf8')}%${value}`; return `%${Buffer.byteLength(value, 'utf8')}%${value}`;
} }
function buildJellyfinLoadfileOptions( // The title is a per-file option, so mpv never shows the tokenized stream URL as the title, and
plan: JellyfinPlaybackPlan, // no global title is left behind for the next playlist entry to fall back to.
startSeconds: number, function buildJellyfinLoadfileOptions(plan: JellyfinPlaybackPlan, startSeconds: number): string {
mediaTitle?: string,
): string {
const options = [...JELLYFIN_LOADFILE_SUBTITLE_SUPPRESSION_OPTIONS]; const options = [...JELLYFIN_LOADFILE_SUBTITLE_SUPPRESSION_OPTIONS];
if (plan.mode === 'direct' && startSeconds > 0) { if (plan.mode === 'direct' && startSeconds > 0) {
options.push(`start=${startSeconds}`); options.push(`start=${startSeconds}`);
} }
if (mediaTitle) { options.push(`force-media-title=${quoteMpvOptionValue(plan.title)}`);
options.push(`force-media-title=${quoteMpvOptionValue(mediaTitle)}`);
}
return options.join(','); return options.join(',');
} }
@@ -157,8 +153,6 @@ function prepareJellyfinPlayback(
options: { options: {
startTimeTicksOverride?: number; startTimeTicksOverride?: number;
fallbackToPlanStartTimeOnZeroOverride?: boolean; fallbackToPlanStartTimeOnZeroOverride?: boolean;
// Playlist entries carry their title per file; an explicit play sets it before loadfile.
perFileMediaTitle?: boolean;
} = {}, } = {},
): JellyfinPreparedPlayback { ): JellyfinPreparedPlayback {
const startTimeTicks = resolveEffectiveStartTimeTicks( const startTimeTicks = resolveEffectiveStartTimeTicks(
@@ -177,11 +171,7 @@ function prepareJellyfinPlayback(
title: plan.title, title: plan.title,
plan, plan,
playbackUrl: stripManagedSubtitleStreamFromPlaybackUrl(playbackUrlBase), playbackUrl: stripManagedSubtitleStreamFromPlaybackUrl(playbackUrlBase),
loadfileOptions: buildJellyfinLoadfileOptions( loadfileOptions: buildJellyfinLoadfileOptions(plan, startSeconds),
plan,
startSeconds,
options.perFileMediaTitle ? plan.title : undefined,
),
startTimeTicks, startTimeTicks,
startSeconds, startSeconds,
playMethod: plan.mode === 'direct' ? 'DirectPlay' : 'Transcode', playMethod: plan.mode === 'direct' ? 'DirectPlay' : 'Transcode',
@@ -281,7 +271,6 @@ export function createPlayJellyfinItemInMpvHandler(deps: {
itemId, itemId,
await deps.resolvePlaybackPlan({ ...context, itemId }), await deps.resolvePlaybackPlan({ ...context, itemId }),
deps.convertTicksToSeconds, deps.convertTicksToSeconds,
{ perFileMediaTitle: true },
), ),
readMpvPlaylist: () => deps.readMpvPlaylist(), readMpvPlaylist: () => deps.readMpvPlaylist(),
sendMpvCommand: (command) => deps.sendMpvCommand(command), sendMpvCommand: (command) => deps.sendMpvCommand(command),
@@ -418,8 +407,6 @@ export function createPlayJellyfinItemInMpvHandler(deps: {
pendingNeighborQueue = null; pendingNeighborQueue = null;
const reporter = beginPlayback(context, prepared, null); const reporter = beginPlayback(context, prepared, null);
deps.sendMpvCommand(['script-message', 'subminer-managed-subtitles-loading']); deps.sendMpvCommand(['script-message', 'subminer-managed-subtitles-loading']);
// Set mpv's title before loadfile can emit a URL-derived media-title event.
deps.sendMpvCommand(['set_property', 'force-media-title', plan.title]);
deps.sendMpvCommand([ deps.sendMpvCommand([
'loadfile', 'loadfile',
prepared.playbackUrl, prepared.playbackUrl,