mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-04-03 06:12:07 -07:00
Fix Windows mpv shortcut launch and subtitle dedupe
This commit is contained in:
@@ -184,6 +184,39 @@ test('dispatchMpvProtocolMessage sets secondary subtitle track based on track li
|
||||
assert.deepEqual(state.commands, [{ command: ['set_property', 'secondary-sid', 2] }]);
|
||||
});
|
||||
|
||||
test('dispatchMpvProtocolMessage prefers the already selected matching secondary track', async () => {
|
||||
const { deps, state } = createDeps();
|
||||
|
||||
await dispatchMpvProtocolMessage(
|
||||
{
|
||||
request_id: MPV_REQUEST_ID_TRACK_LIST_SECONDARY,
|
||||
data: [
|
||||
{
|
||||
type: 'sub',
|
||||
id: 2,
|
||||
lang: 'ja',
|
||||
title: 'ja.srt',
|
||||
selected: false,
|
||||
external: true,
|
||||
'external-filename': '/tmp/dupe.srt',
|
||||
},
|
||||
{
|
||||
type: 'sub',
|
||||
id: 3,
|
||||
lang: 'ja',
|
||||
title: 'ja.srt',
|
||||
selected: true,
|
||||
external: true,
|
||||
'external-filename': '/tmp/dupe.srt',
|
||||
},
|
||||
],
|
||||
},
|
||||
deps,
|
||||
);
|
||||
|
||||
assert.deepEqual(state.commands, [{ command: ['set_property', 'secondary-sid', 3] }]);
|
||||
});
|
||||
|
||||
test('dispatchMpvProtocolMessage restores secondary visibility on shutdown', async () => {
|
||||
const { deps, state } = createDeps();
|
||||
|
||||
|
||||
@@ -93,6 +93,97 @@ export interface MpvProtocolHandleMessageDeps {
|
||||
restorePreviousSecondarySubVisibility: () => void;
|
||||
}
|
||||
|
||||
type SubtitleTrackCandidate = {
|
||||
id: number;
|
||||
lang: string;
|
||||
title: string;
|
||||
selected: boolean;
|
||||
external: boolean;
|
||||
externalFilename: string | null;
|
||||
};
|
||||
|
||||
function normalizeSubtitleTrackCandidate(track: Record<string, unknown>): SubtitleTrackCandidate | null {
|
||||
const id =
|
||||
typeof track.id === 'number'
|
||||
? track.id
|
||||
: typeof track.id === 'string'
|
||||
? Number(track.id.trim())
|
||||
: Number.NaN;
|
||||
if (!Number.isInteger(id)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const externalFilename =
|
||||
typeof track['external-filename'] === 'string' && track['external-filename'].trim().length > 0
|
||||
? track['external-filename'].trim()
|
||||
: typeof track.external_filename === 'string' && track.external_filename.trim().length > 0
|
||||
? track.external_filename.trim()
|
||||
: null;
|
||||
|
||||
return {
|
||||
id,
|
||||
lang: String(track.lang || '').trim().toLowerCase(),
|
||||
title: String(track.title || '').trim().toLowerCase(),
|
||||
selected: track.selected === true,
|
||||
external: track.external === true,
|
||||
externalFilename,
|
||||
};
|
||||
}
|
||||
|
||||
function getSubtitleTrackIdentity(track: SubtitleTrackCandidate): string {
|
||||
if (track.externalFilename) {
|
||||
return `external:${track.externalFilename.toLowerCase()}`;
|
||||
}
|
||||
if (track.title.length > 0) {
|
||||
return `title:${track.title}`;
|
||||
}
|
||||
return `id:${track.id}`;
|
||||
}
|
||||
|
||||
function pickSecondarySubtitleTrackId(
|
||||
tracks: Array<Record<string, unknown>>,
|
||||
preferredLanguages: string[],
|
||||
): number | null {
|
||||
const normalizedLanguages = preferredLanguages
|
||||
.map((language) => language.trim().toLowerCase())
|
||||
.filter((language) => language.length > 0);
|
||||
if (normalizedLanguages.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const subtitleTracks = tracks
|
||||
.filter((track) => track.type === 'sub')
|
||||
.map(normalizeSubtitleTrackCandidate)
|
||||
.filter((track): track is SubtitleTrackCandidate => track !== null);
|
||||
|
||||
const dedupedTracks = new Map<string, SubtitleTrackCandidate>();
|
||||
for (const track of subtitleTracks) {
|
||||
const identity = getSubtitleTrackIdentity(track);
|
||||
const existing = dedupedTracks.get(identity);
|
||||
if (!existing || (track.selected && !existing.selected)) {
|
||||
dedupedTracks.set(identity, track);
|
||||
}
|
||||
}
|
||||
|
||||
const uniqueTracks = [...dedupedTracks.values()];
|
||||
|
||||
for (const language of normalizedLanguages) {
|
||||
const selectedMatch = uniqueTracks.find(
|
||||
(track) => track.selected && track.lang === language,
|
||||
);
|
||||
if (selectedMatch) {
|
||||
return selectedMatch.id;
|
||||
}
|
||||
|
||||
const match = uniqueTracks.find((track) => track.lang === language);
|
||||
if (match) {
|
||||
return match.id;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function splitMpvMessagesFromBuffer(
|
||||
buffer: string,
|
||||
onMessage?: MpvMessageParser,
|
||||
@@ -283,15 +374,11 @@ export async function dispatchMpvProtocolMessage(
|
||||
if (Array.isArray(tracks)) {
|
||||
const config = deps.getResolvedConfig();
|
||||
const languages = config.secondarySub?.secondarySubLanguages || [];
|
||||
const subTracks = tracks.filter((track) => track.type === 'sub');
|
||||
for (const language of languages) {
|
||||
const match = subTracks.find((track) => track.lang === language);
|
||||
if (match) {
|
||||
deps.sendCommand({
|
||||
command: ['set_property', 'secondary-sid', match.id],
|
||||
});
|
||||
break;
|
||||
}
|
||||
const secondaryTrackId = pickSecondarySubtitleTrackId(tracks, languages);
|
||||
if (secondaryTrackId !== null) {
|
||||
deps.sendCommand({
|
||||
command: ['set_property', 'secondary-sid', secondaryTrackId],
|
||||
});
|
||||
}
|
||||
}
|
||||
} else if (msg.request_id === MPV_REQUEST_ID_TRACK_LIST_AUDIO) {
|
||||
|
||||
@@ -92,6 +92,52 @@ test('triggerSubsyncFromConfig opens manual picker in manual mode', async () =>
|
||||
assert.equal(inProgressState, false);
|
||||
});
|
||||
|
||||
test('triggerSubsyncFromConfig dedupes repeated subtitle source tracks', async () => {
|
||||
let payloadTrackCount = 0;
|
||||
|
||||
await triggerSubsyncFromConfig(
|
||||
makeDeps({
|
||||
getMpvClient: () => ({
|
||||
connected: true,
|
||||
currentAudioStreamIndex: null,
|
||||
send: () => {},
|
||||
requestProperty: async (name: string) => {
|
||||
if (name === 'path') return '/tmp/video.mkv';
|
||||
if (name === 'sid') return 1;
|
||||
if (name === 'secondary-sid') return 2;
|
||||
if (name === 'track-list') {
|
||||
return [
|
||||
{ id: 1, type: 'sub', selected: true, lang: 'jpn' },
|
||||
{
|
||||
id: 2,
|
||||
type: 'sub',
|
||||
selected: true,
|
||||
external: true,
|
||||
lang: 'eng',
|
||||
'external-filename': '/tmp/ref.srt',
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
type: 'sub',
|
||||
selected: false,
|
||||
external: true,
|
||||
lang: 'eng',
|
||||
'external-filename': '/tmp/ref.srt',
|
||||
},
|
||||
];
|
||||
}
|
||||
return null;
|
||||
},
|
||||
}),
|
||||
openManualPicker: (payload) => {
|
||||
payloadTrackCount = payload.sourceTracks.length;
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
assert.equal(payloadTrackCount, 1);
|
||||
});
|
||||
|
||||
test('triggerSubsyncFromConfig reports failures to OSD', async () => {
|
||||
const osd: string[] = [];
|
||||
await triggerSubsyncFromConfig(
|
||||
|
||||
@@ -76,6 +76,31 @@ function normalizeTrackIds(tracks: unknown[]): MpvTrack[] {
|
||||
});
|
||||
}
|
||||
|
||||
function getSourceTrackIdentity(track: MpvTrack): string {
|
||||
if (track.external && typeof track['external-filename'] === 'string' && track['external-filename'].length > 0) {
|
||||
return `external:${track['external-filename'].toLowerCase()}`;
|
||||
}
|
||||
if (typeof track.id === 'number') {
|
||||
return `id:${track.id}`;
|
||||
}
|
||||
if (typeof track.title === 'string' && track.title.length > 0) {
|
||||
return `title:${track.title.toLowerCase()}`;
|
||||
}
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
function dedupeSourceTracks(tracks: MpvTrack[]): MpvTrack[] {
|
||||
const deduped = new Map<string, MpvTrack>();
|
||||
for (const track of tracks) {
|
||||
const identity = getSourceTrackIdentity(track);
|
||||
const existing = deduped.get(identity);
|
||||
if (!existing || (track.selected && !existing.selected)) {
|
||||
deduped.set(identity, track);
|
||||
}
|
||||
}
|
||||
return [...deduped.values()];
|
||||
}
|
||||
|
||||
export interface TriggerSubsyncFromConfigDeps extends SubsyncCoreDeps {
|
||||
isSubsyncInProgress: () => boolean;
|
||||
setSubsyncInProgress: (inProgress: boolean) => void;
|
||||
@@ -123,12 +148,13 @@ async function gatherSubsyncContext(client: MpvClientLike): Promise<SubsyncConte
|
||||
const filename = track['external-filename'];
|
||||
return typeof filename === 'string' && filename.length > 0;
|
||||
});
|
||||
const uniqueSourceTracks = dedupeSourceTracks(sourceTracks);
|
||||
|
||||
return {
|
||||
videoPath,
|
||||
primaryTrack,
|
||||
secondaryTrack,
|
||||
sourceTracks,
|
||||
sourceTracks: uniqueSourceTracks,
|
||||
audioStreamIndex: client.currentAudioStreamIndex,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user