mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-28 04:46:07 -08:00
fix(subsync): guard embedded track cache against replaced media files
- Capture the file signature before an individual embedded retry and skip caching if the file changed mid-extraction - Keep cached embedded tracks when the combined single-pass rip fails on a broken track - Expose getCachedEmbeddedSubtitles and getMediaFileSignature helpers
This commit is contained in:
4 files changed
+364
-11
No files matched your search
@@ -10,6 +10,8 @@ import {
|
||||
extractAudioSpeechSpans,
|
||||
extractSinglePassRipMedia,
|
||||
getCachedAudioSpeechSpans,
|
||||
getCachedEmbeddedSubtitles,
|
||||
getMediaFileSignature,
|
||||
parseRawSileroVadSegments,
|
||||
} from './audio-speech';
|
||||
import { parseSubtitleFileContent } from './subtitle-lines';
|
||||
@@ -144,12 +146,14 @@ exit 0
|
||||
}
|
||||
});
|
||||
|
||||
test('cacheEmbeddedSubtitleTrack populates single-pass cache and ignores zero-event parses', async () => {
|
||||
test('cacheEmbeddedSubtitleTrack populates single-pass cache, ignores empty parses, and guards file signature', async () => {
|
||||
clearAudioSpeechCache();
|
||||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-embedded-cache-'));
|
||||
try {
|
||||
const videoPath = path.join(tmpDir, 'episode.mkv');
|
||||
fs.writeFileSync(videoPath, 'video');
|
||||
fs.writeFileSync(videoPath, 'video-v1');
|
||||
const sigV1 = getMediaFileSignature(videoPath);
|
||||
assert.ok(sigV1 !== null);
|
||||
|
||||
const validParsed = parseSubtitleFileContent(
|
||||
'1\n00:00:01,000 --> 00:00:02,500\nHello world\n',
|
||||
@@ -157,8 +161,11 @@ test('cacheEmbeddedSubtitleTrack populates single-pass cache and ignores zero-ev
|
||||
);
|
||||
const emptyParsed = parseSubtitleFileContent('', 'track_3.srt');
|
||||
|
||||
cacheEmbeddedSubtitleTrack(videoPath, 3, validParsed);
|
||||
cacheEmbeddedSubtitleTrack(videoPath, 3, emptyParsed);
|
||||
cacheEmbeddedSubtitleTrack(videoPath, 3, validParsed, sigV1);
|
||||
cacheEmbeddedSubtitleTrack(videoPath, 3, emptyParsed, sigV1);
|
||||
|
||||
const cachedV1 = getCachedEmbeddedSubtitles(videoPath);
|
||||
assert.equal(cachedV1?.get(3)?.events.length, 1);
|
||||
|
||||
const rip = await extractSinglePassRipMedia({
|
||||
ffmpegPath: '/nonexistent/ffmpeg',
|
||||
@@ -170,6 +177,14 @@ test('cacheEmbeddedSubtitleTrack populates single-pass cache and ignores zero-ev
|
||||
|
||||
assert.ok(rip !== null);
|
||||
assert.equal(rip.embeddedSubtitles.get(3)?.events.length, 1);
|
||||
|
||||
// Replacing the file invalidates sigV1; publishing under sigV1 must not poison v2.
|
||||
fs.writeFileSync(videoPath, 'video-v2-remuxed');
|
||||
const sigV2 = getMediaFileSignature(videoPath);
|
||||
assert.notEqual(sigV2, sigV1);
|
||||
|
||||
cacheEmbeddedSubtitleTrack(videoPath, 4, validParsed, sigV1);
|
||||
assert.equal(getCachedEmbeddedSubtitles(videoPath), null);
|
||||
} finally {
|
||||
clearAudioSpeechCache();
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
|
||||
@@ -122,14 +122,31 @@ function rememberAudioSpeech(key: string | null, result: AudioSpeechResult): voi
|
||||
rememberLru(audioSpeechCache, key, result);
|
||||
}
|
||||
|
||||
export function getMediaFileSignature(videoPath: string): string | null {
|
||||
return buildFileSignatureKey(videoPath);
|
||||
}
|
||||
|
||||
export function getCachedEmbeddedSubtitles(
|
||||
videoPath: string,
|
||||
): Map<number, ParsedSubtitleFile> | null {
|
||||
const fileKey = buildFileSignatureKey(videoPath);
|
||||
if (!fileKey) return null;
|
||||
const cached = embeddedSubtitlesCache.get(fileKey);
|
||||
return cached && cached.size > 0 ? new Map(cached) : null;
|
||||
}
|
||||
|
||||
export function cacheEmbeddedSubtitleTrack(
|
||||
videoPath: string,
|
||||
ffIndex: number,
|
||||
parsed: ParsedSubtitleFile,
|
||||
expectedFileSignature?: string | null,
|
||||
): void {
|
||||
if (parsed.events.length === 0) return;
|
||||
const fileKey = buildFileSignatureKey(videoPath);
|
||||
if (!fileKey) return;
|
||||
if (expectedFileSignature !== undefined && fileKey !== expectedFileSignature) {
|
||||
return;
|
||||
}
|
||||
const existing = embeddedSubtitlesCache.get(fileKey);
|
||||
const updated = new Map<number, ParsedSubtitleFile>(existing);
|
||||
updated.set(ffIndex, parsed);
|
||||
|
||||
Reference in new issue
Block a user