mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-28 04:46:07 -08:00
refactor(subsync): share the auto-sync candidate attempt loop
The align, apply-if-confident, else record-best-attempt loop was repeated six times in runAutoSubsync and the audio-verdict grouping twice. Both now go through tryReference and groupCandidatesByAudioStatus; behavior is unchanged.
This commit is contained in:
1 file changed
+91
-161
@@ -1,12 +1,13 @@
|
|||||||
import { redactUrlCredentials } from './subtitle-generation-process';
|
import { redactUrlCredentials } from './subtitle-generation-process';
|
||||||
import { buildJellyfinEmbeddedSubtitleUrl } from '../../subsync/utils';
|
import { buildJellyfinEmbeddedSubtitleUrl } from '../../subsync/utils';
|
||||||
import type { AlignmentResult } from '../../subsync/alass-align';
|
import type { AlignmentResult, AlignSubtitleOptions } from '../../subsync/alass-align';
|
||||||
import { extractSinglePassRipMedia, getMediaFileSignature } from '../../subsync/audio-speech';
|
import { extractSinglePassRipMedia, getMediaFileSignature } from '../../subsync/audio-speech';
|
||||||
import {
|
import {
|
||||||
decodeSubtitleBytesWithStatus,
|
decodeSubtitleBytesWithStatus,
|
||||||
extractSpansFromParsedSubtitle,
|
extractSpansFromParsedSubtitle,
|
||||||
parseSubtitleFileContent,
|
parseSubtitleFileContent,
|
||||||
type ParsedSubtitleFile,
|
type ParsedSubtitleFile,
|
||||||
|
type SubtitleTimeSpan,
|
||||||
} from '../../subsync/subtitle-lines';
|
} from '../../subsync/subtitle-lines';
|
||||||
import { isRemoteMediaPath } from '../../jimaku/utils';
|
import { isRemoteMediaPath } from '../../jimaku/utils';
|
||||||
import {
|
import {
|
||||||
@@ -45,9 +46,31 @@ import {
|
|||||||
getMpvClientForSubsync,
|
getMpvClientForSubsync,
|
||||||
isAlreadyInSync,
|
isAlreadyInSync,
|
||||||
isExecutableAvailable,
|
isExecutableAvailable,
|
||||||
|
type CandidateWithSpans,
|
||||||
} from './subsync-shared';
|
} from './subsync-shared';
|
||||||
import { alignSubtitleOffThread } from '../../subsync/align-worker';
|
import { alignSubtitleOffThread } from '../../subsync/align-worker';
|
||||||
|
|
||||||
|
type BestAttempt = { alignment: AlignmentResult; sourceLabel: string; referenceKey: string };
|
||||||
|
|
||||||
|
/** Split evaluated references by audio verdict; aligned/unverified keep only usable dialogue tracks. */
|
||||||
|
function groupCandidatesByAudioStatus(candidates: readonly CandidateWithSpans[]): {
|
||||||
|
misaligned: CandidateWithSpans[];
|
||||||
|
aligned: CandidateWithSpans[];
|
||||||
|
unverified: CandidateWithSpans[];
|
||||||
|
} {
|
||||||
|
const isUsableDialogue = (c: CandidateWithSpans): boolean =>
|
||||||
|
!c.evaluated.isSignsOnly && c.dialogueSpans.length >= MIN_REFERENCE_DIALOGUE_LINES;
|
||||||
|
return {
|
||||||
|
misaligned: candidates.filter(
|
||||||
|
(c) => c.evaluated.audioStatus === 'misaligned' && !c.evaluated.isSignsOnly,
|
||||||
|
),
|
||||||
|
aligned: candidates.filter((c) => c.evaluated.audioStatus === 'aligned' && isUsableDialogue(c)),
|
||||||
|
unverified: candidates.filter(
|
||||||
|
(c) => c.evaluated.audioStatus === 'unverified' && isUsableDialogue(c),
|
||||||
|
),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promise<void> {
|
export async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promise<void> {
|
||||||
const client = getMpvClientForSubsync(deps);
|
const client = getMpvClientForSubsync(deps);
|
||||||
const context = await gatherSubsyncContext(client);
|
const context = await gatherSubsyncContext(client);
|
||||||
@@ -168,33 +191,50 @@ export async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promis
|
|||||||
deps.showMpvOsd(formatAlignmentSummary(sourceLabel, alignment));
|
deps.showMpvOsd(formatAlignmentSummary(sourceLabel, alignment));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Only assigned inside tryReference; the cast keeps TS from narrowing it to null.
|
||||||
|
let bestAttempt = null as BestAttempt | null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Align the target to one reference. A confident result is applied (returns true);
|
||||||
|
* otherwise it is remembered as the best attempt for the guided modal unless
|
||||||
|
* `trackBest` is false.
|
||||||
|
*/
|
||||||
|
const tryReference = async (
|
||||||
|
referenceSpans: readonly SubtitleTimeSpan[],
|
||||||
|
source: { label: string; key: string },
|
||||||
|
options: Pick<AlignSubtitleOptions, 'calibration' | 'confidenceThreshold' | 'leadSec'>,
|
||||||
|
trackBest = true,
|
||||||
|
): Promise<boolean> => {
|
||||||
|
const alignment = await alignSubtitleOffThread(deps.signal, parsedTarget, referenceSpans, {
|
||||||
|
stripSigns: true,
|
||||||
|
allowSplits: true,
|
||||||
|
lengthSec,
|
||||||
|
...options,
|
||||||
|
});
|
||||||
|
if (alignment.confident) {
|
||||||
|
await applyAlignment(alignment, source.label);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (trackBest && (!bestAttempt || alignment.confidence > bestAttempt.alignment.confidence)) {
|
||||||
|
bestAttempt = { alignment, sourceLabel: source.label, referenceKey: source.key };
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
const clean = (confidenceThreshold: number) =>
|
||||||
|
({ calibration: 'clean', confidenceThreshold }) as const;
|
||||||
|
|
||||||
// Fast path: if sampled audio already verified a clean aligned subtitle reference
|
// Fast path: if sampled audio already verified a clean aligned subtitle reference
|
||||||
// and target alignment succeeds with high confidence, finish immediately without
|
// and target alignment succeeds with high confidence, finish immediately without
|
||||||
// reading the full video audio or embedded container tracks!
|
// reading the full video audio or embedded container tracks!
|
||||||
if (!audioSpeech && prepared.sampledAudioWindows) {
|
if (!audioSpeech && prepared.sampledAudioWindows) {
|
||||||
const fastAligned = evaluatedCandidates.filter(
|
for (const candidate of groupCandidatesByAudioStatus(evaluatedCandidates).aligned) {
|
||||||
(c) =>
|
const applied = await tryReference(
|
||||||
c.evaluated.audioStatus === 'aligned' &&
|
|
||||||
!c.evaluated.isSignsOnly &&
|
|
||||||
c.dialogueSpans.length >= MIN_REFERENCE_DIALOGUE_LINES,
|
|
||||||
);
|
|
||||||
for (const candidate of fastAligned) {
|
|
||||||
const alignment = await alignSubtitleOffThread(
|
|
||||||
deps.signal,
|
|
||||||
parsedTarget,
|
|
||||||
candidate.dialogueSpans,
|
candidate.dialogueSpans,
|
||||||
{
|
candidate.evaluated,
|
||||||
stripSigns: true,
|
clean(AUTO_SUBTITLE_CONFIDENCE_THRESHOLD),
|
||||||
allowSplits: true,
|
false,
|
||||||
calibration: 'clean',
|
|
||||||
lengthSec,
|
|
||||||
confidenceThreshold: AUTO_SUBTITLE_CONFIDENCE_THRESHOLD,
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
if (alignment.confident) {
|
if (applied) return;
|
||||||
await applyAlignment(alignment, candidate.evaluated.label);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sampled verification did not yield a confident aligned subtitle sync (e.g. all
|
// Sampled verification did not yield a confident aligned subtitle sync (e.g. all
|
||||||
@@ -221,112 +261,48 @@ export async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promis
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let misalignedCandidates = evaluatedCandidates.filter(
|
let {
|
||||||
(c) => c.evaluated.audioStatus === 'misaligned' && !c.evaluated.isSignsOnly,
|
misaligned: misalignedCandidates,
|
||||||
);
|
aligned: alignedCandidates,
|
||||||
let alignedCandidates = evaluatedCandidates.filter(
|
unverified: unverifiedDialogueCandidates,
|
||||||
(c) =>
|
} = groupCandidatesByAudioStatus(evaluatedCandidates);
|
||||||
c.evaluated.audioStatus === 'aligned' &&
|
|
||||||
!c.evaluated.isSignsOnly &&
|
|
||||||
c.dialogueSpans.length >= MIN_REFERENCE_DIALOGUE_LINES,
|
|
||||||
);
|
|
||||||
let unverifiedDialogueCandidates = evaluatedCandidates.filter(
|
|
||||||
(c) =>
|
|
||||||
c.evaluated.audioStatus === 'unverified' &&
|
|
||||||
!c.evaluated.isSignsOnly &&
|
|
||||||
c.dialogueSpans.length >= MIN_REFERENCE_DIALOGUE_LINES,
|
|
||||||
);
|
|
||||||
|
|
||||||
let bestAttempt: {
|
|
||||||
alignment: AlignmentResult;
|
|
||||||
sourceLabel: string;
|
|
||||||
referenceKey: string;
|
|
||||||
} | null = null;
|
|
||||||
|
|
||||||
// 1. Best path: an audio-verified aligned dialogue subtitle reference (loaded or unloaded!).
|
// 1. Best path: an audio-verified aligned dialogue subtitle reference (loaded or unloaded!).
|
||||||
for (const candidate of alignedCandidates) {
|
for (const candidate of alignedCandidates) {
|
||||||
const alignment = await alignSubtitleOffThread(
|
const applied = await tryReference(
|
||||||
deps.signal,
|
|
||||||
parsedTarget,
|
|
||||||
candidate.dialogueSpans,
|
candidate.dialogueSpans,
|
||||||
{
|
candidate.evaluated,
|
||||||
stripSigns: true,
|
clean(AUTO_SUBTITLE_CONFIDENCE_THRESHOLD),
|
||||||
allowSplits: true,
|
|
||||||
calibration: 'clean',
|
|
||||||
lengthSec,
|
|
||||||
confidenceThreshold: AUTO_SUBTITLE_CONFIDENCE_THRESHOLD,
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
if (alignment.confident) {
|
if (applied) return;
|
||||||
await applyAlignment(alignment, candidate.evaluated.label);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!bestAttempt || alignment.confidence > bestAttempt.alignment.confidence) {
|
|
||||||
bestAttempt = {
|
|
||||||
alignment,
|
|
||||||
sourceLabel: candidate.evaluated.label,
|
|
||||||
referenceKey: candidate.evaluated.key,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Fallback when no subtitle track is audio-aligned (none exist, only signs/songs, or all
|
// 2. Fallback when no subtitle track is audio-aligned (none exist, only signs/songs, or all
|
||||||
// reference subtitles are shifted off the audio): sync directly to video audio speech!
|
// reference subtitles are shifted off the audio): sync directly to video audio speech!
|
||||||
if (audioSpeech && audioSpeech.spans.length >= MIN_REFERENCE_DIALOGUE_LINES) {
|
if (audioSpeech && audioSpeech.spans.length >= MIN_REFERENCE_DIALOGUE_LINES) {
|
||||||
const audioAlignment = await alignSubtitleOffThread(
|
const audioLabel = audioSpeech.source === 'silero-vad' ? 'audio (Silero VAD)' : 'video audio';
|
||||||
deps.signal,
|
const applied = await tryReference(
|
||||||
parsedTarget,
|
|
||||||
audioSpeech.spans,
|
audioSpeech.spans,
|
||||||
|
{ label: audioLabel, key: 'video' },
|
||||||
{
|
{
|
||||||
stripSigns: true,
|
|
||||||
allowSplits: true,
|
|
||||||
calibration: audioSpeech.calibration,
|
calibration: audioSpeech.calibration,
|
||||||
leadSec: AUDIO_SPEECH_LEAD_SEC,
|
leadSec: AUDIO_SPEECH_LEAD_SEC,
|
||||||
lengthSec,
|
|
||||||
confidenceThreshold: AUTO_AUDIO_CONFIDENCE_THRESHOLD,
|
confidenceThreshold: AUTO_AUDIO_CONFIDENCE_THRESHOLD,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
const audioLabel = audioSpeech.source === 'silero-vad' ? 'audio (Silero VAD)' : 'video audio';
|
if (applied) return;
|
||||||
if (audioAlignment.confident) {
|
|
||||||
await applyAlignment(audioAlignment, audioLabel);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!bestAttempt || audioAlignment.confidence > bestAttempt.alignment.confidence) {
|
|
||||||
bestAttempt = {
|
|
||||||
alignment: audioAlignment,
|
|
||||||
sourceLabel: audioLabel,
|
|
||||||
referenceKey: 'video',
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. When audio verification wasn't available (e.g. remote stream or no ffmpeg):
|
// 3. When audio verification wasn't available (e.g. remote stream or no ffmpeg):
|
||||||
// try already-loaded external dialogue tracks first with a stricter confidence bar.
|
// try already-loaded external dialogue tracks first with a stricter confidence bar.
|
||||||
if (!audioSpeech) {
|
if (!audioSpeech) {
|
||||||
for (const candidate of unverifiedDialogueCandidates) {
|
for (const candidate of unverifiedDialogueCandidates) {
|
||||||
const alignment = await alignSubtitleOffThread(
|
const applied = await tryReference(
|
||||||
deps.signal,
|
|
||||||
parsedTarget,
|
|
||||||
candidate.dialogueSpans,
|
candidate.dialogueSpans,
|
||||||
{
|
candidate.evaluated,
|
||||||
stripSigns: true,
|
clean(AUTO_REMOTE_CONFIDENCE_THRESHOLD),
|
||||||
allowSplits: true,
|
|
||||||
calibration: 'clean',
|
|
||||||
lengthSec,
|
|
||||||
confidenceThreshold: AUTO_REMOTE_CONFIDENCE_THRESHOLD,
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
if (alignment.confident) {
|
if (applied) return;
|
||||||
await applyAlignment(alignment, candidate.evaluated.label);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!bestAttempt || alignment.confidence > bestAttempt.alignment.confidence) {
|
|
||||||
bestAttempt = {
|
|
||||||
alignment,
|
|
||||||
sourceLabel: candidate.evaluated.label,
|
|
||||||
referenceKey: candidate.evaluated.key,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3b. On remote streams where no external reference succeeded, fetch embedded tracks
|
// 3b. On remote streams where no external reference succeeded, fetch embedded tracks
|
||||||
@@ -347,30 +323,11 @@ export async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promis
|
|||||||
|
|
||||||
const extractedSpans = extractSpansFromParsedSubtitle(parsed, { stripSigns: true });
|
const extractedSpans = extractSpansFromParsedSubtitle(parsed, { stripSigns: true });
|
||||||
if (extractedSpans.dialogueCount < MIN_REFERENCE_DIALOGUE_LINES) return false;
|
if (extractedSpans.dialogueCount < MIN_REFERENCE_DIALOGUE_LINES) return false;
|
||||||
const alignment = await alignSubtitleOffThread(
|
return tryReference(
|
||||||
deps.signal,
|
|
||||||
parsedTarget,
|
|
||||||
extractedSpans.spans,
|
extractedSpans.spans,
|
||||||
{
|
item,
|
||||||
stripSigns: true,
|
clean(AUTO_REMOTE_CONFIDENCE_THRESHOLD),
|
||||||
allowSplits: true,
|
|
||||||
calibration: 'clean',
|
|
||||||
lengthSec,
|
|
||||||
confidenceThreshold: AUTO_REMOTE_CONFIDENCE_THRESHOLD,
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
if (alignment.confident) {
|
|
||||||
await applyAlignment(alignment, item.label);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (!bestAttempt || alignment.confidence > bestAttempt.alignment.confidence) {
|
|
||||||
bestAttempt = {
|
|
||||||
alignment,
|
|
||||||
sourceLabel: item.label,
|
|
||||||
referenceKey: item.key,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const item of rankedEmbedded) {
|
for (const item of rankedEmbedded) {
|
||||||
@@ -467,21 +424,11 @@ export async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promis
|
|||||||
signal: deps.signal,
|
signal: deps.signal,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
misalignedCandidates = evaluatedCandidates.filter(
|
({
|
||||||
(c) => c.evaluated.audioStatus === 'misaligned' && !c.evaluated.isSignsOnly,
|
misaligned: misalignedCandidates,
|
||||||
);
|
aligned: alignedCandidates,
|
||||||
alignedCandidates = evaluatedCandidates.filter(
|
unverified: unverifiedDialogueCandidates,
|
||||||
(c) =>
|
} = groupCandidatesByAudioStatus(evaluatedCandidates));
|
||||||
c.evaluated.audioStatus === 'aligned' &&
|
|
||||||
!c.evaluated.isSignsOnly &&
|
|
||||||
c.dialogueSpans.length >= MIN_REFERENCE_DIALOGUE_LINES,
|
|
||||||
);
|
|
||||||
unverifiedDialogueCandidates = evaluatedCandidates.filter(
|
|
||||||
(c) =>
|
|
||||||
c.evaluated.audioStatus === 'unverified' &&
|
|
||||||
!c.evaluated.isSignsOnly &&
|
|
||||||
c.dialogueSpans.length >= MIN_REFERENCE_DIALOGUE_LINES,
|
|
||||||
);
|
|
||||||
|
|
||||||
for (const candidate of unverifiedDialogueCandidates) {
|
for (const candidate of unverifiedDialogueCandidates) {
|
||||||
if (!candidate.evaluated.isEmbedded) continue;
|
if (!candidate.evaluated.isEmbedded) continue;
|
||||||
@@ -489,29 +436,12 @@ export async function runAutoSubsync(deps: TriggerSubsyncFromConfigDeps): Promis
|
|||||||
(item) => item.key === candidate.evaluated.key,
|
(item) => item.key === candidate.evaluated.key,
|
||||||
);
|
);
|
||||||
if (matchedItem && triedRemoteEmbedded.has(matchedItem.ffIndex)) continue;
|
if (matchedItem && triedRemoteEmbedded.has(matchedItem.ffIndex)) continue;
|
||||||
const alignment = await alignSubtitleOffThread(
|
const applied = await tryReference(
|
||||||
deps.signal,
|
|
||||||
parsedTarget,
|
|
||||||
candidate.dialogueSpans,
|
candidate.dialogueSpans,
|
||||||
{
|
candidate.evaluated,
|
||||||
stripSigns: true,
|
clean(AUTO_REMOTE_CONFIDENCE_THRESHOLD),
|
||||||
allowSplits: true,
|
|
||||||
calibration: 'clean',
|
|
||||||
lengthSec,
|
|
||||||
confidenceThreshold: AUTO_REMOTE_CONFIDENCE_THRESHOLD,
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
if (alignment.confident) {
|
if (applied) return;
|
||||||
await applyAlignment(alignment, candidate.evaluated.label);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!bestAttempt || alignment.confidence > bestAttempt.alignment.confidence) {
|
|
||||||
bestAttempt = {
|
|
||||||
alignment,
|
|
||||||
sourceLabel: candidate.evaluated.label,
|
|
||||||
referenceKey: candidate.evaluated.key,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user