// A SALVAGE TRANSCRIPTION KEEPS THE RECORDING'S CLOCK (release 30, lane Q). // // A corrupt-full-source video's kept file is transcribed with salvage (it is // automatic for that status): decoded with error tolerance into a WAV whose // undecodable spans are silence of their own length, then the normal parakeet // path (the real scripts/parakeet-stitch.mjs, in overlapping windows) over a // FAKE parakeet-cli that "transcribes" tone bursts — one word per burst, at // the burst's time in its window. So the cues it produces are where the // bursts are in the WAV, and the test checks they are where the bursts are in // the SOURCE. No real ASR; real ffmpeg over a synthetic damaged recording. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/transcribeSalvage.test.ts import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { existsSync } from "node:fs"; import { chmod, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { pathToFileURL } from "node:url"; import { burstTimes, corruptBytes, hasFfmpeg, makeToneFile, } from "../testing/audio/corruptAudio.mjs"; const tools = hasFfmpeg(); let ROOT = ""; let VIDEO_DIR = ""; let DAMAGED = ""; let CLEAN = ""; const SLUG = "tones"; const ID = "vid1"; before(async () => { ROOT = await mkdtemp(path.join(tmpdir(), "transcribe-salvage-")); // Before anything reads getPaths(): a scratch corpus, and the fake CLI. process.env.TRANSCRIPTS_DIR = path.join(ROOT, "transcripts"); const cli = path.join(ROOT, "fake-parakeet-cli.mjs"); const kit = pathToFileURL(path.resolve(import.meta.dirname, "../testing/audio/corruptAudio.mjs")).href; await writeFile( cli, `#!/usr/bin/env node import { readWavMono16, toneOnsets } from ${JSON.stringify(kit)}; const argv = process.argv.slice(2); const wav = argv[argv.indexOf("--input") + 1]; const { samples, rate } = readWavMono16(wav); const words = toneOnsets(samples, rate).map((t) => ({ w: "beep", start: t, end: t + 0.3 })); process.stdout.write(JSON.stringify({ words })); `, ); await chmod(cli, 0o755); process.env.PARAKEET_CLI = cli; if (!tools) return; VIDEO_DIR = path.join(process.env.TRANSCRIPTS_DIR, "channels", SLUG, "data", ID); await mkdir(VIDEO_DIR, { recursive: true }); CLEAN = makeToneFile(path.join(ROOT, "clean.mp4")); DAMAGED = corruptBytes(CLEAN, path.join(VIDEO_DIR, "audio.mp4")); await writeFile( path.join(VIDEO_DIR, "metadata.info.json"), JSON.stringify({ id: ID, title: "Tones", duration: 30, upload_date: "20260105", webpage_url: "https://example.invalid/v/vid1" }), ); await writeFile( path.join(VIDEO_DIR, "download-outcome.json"), JSON.stringify({ videoId: ID, status: "corrupt-full-source", startedAt: "2026-10-01T00:00:00.000Z", finishedAt: "2026-10-01T00:01:00.000Z", attempts: [], }), ); }); after(() => rm(ROOT, { recursive: true, force: true })); const near = (a: number, b: number, tol = 0.06) => Math.abs(a - b) <= tol; // A model file must exist; the fake CLI never reads it. 20 s windows with the // default 6 s overlap: the 30 s recording is two windows, so the stitch's // offsets are exercised too. const worker = () => ({ id: "w1", name: "fake parakeet", kind: "local" as const, enabled: true, priority: 0, appId: "parakeet", config: { model: CLEAN, chunkSize: 20 }, }); test( "a corrupt full source is salvaged: cues on the recording's clock, the gap recorded, the WAV gone", { skip: !tools && "ffmpeg/ffprobe not installed" }, async () => { const { transcribeOneVideo } = await import("./transcribeOne"); const { getPaths } = await import("../lib/paths"); const paths = getPaths(); const lines: string[] = []; const outcome = await transcribeOneVideo({ paths, videoDir: VIDEO_DIR, videoId: ID, audioFilename: "audio.mp4", worker: worker(), onLog: (l) => lines.push(l), skipInlineDiarization: true, }); assert.equal(outcome, "transcribed", lines.join("\n")); assert.match(lines.join("\n"), /Salvage: decoding audio\.mp4 with error tolerance/); const record = JSON.parse(await readFile(path.join(VIDEO_DIR, "transcribe-outcome.json"), "utf8")); assert.ok(record.salvaged, "transcribe-outcome.json records the salvage"); assert.equal(record.salvaged.source, "audio.mp4"); assert.ok(near(record.salvaged.totalSeconds, 30), `total ${record.salvaged.totalSeconds}`); assert.equal(record.salvaged.gaps.length, 1, JSON.stringify(record.salvaged.gaps)); const [from, to] = record.salvaged.gaps[0]; assert.ok(to - from > 1); assert.ok(record.salvaged.decodedSeconds < 30 - 1); // The cues: one per surviving burst, each at its SOURCE time. const transcript = JSON.parse(await readFile(path.join(VIDEO_DIR, "transcript.json"), "utf8")); const starts: number[] = transcript.chunk_data.map((c: { start_time: number }) => c.start_time); const expected = burstTimes().filter((t) => t + 0.3 < from || t > to); assert.ok(expected.some((t) => t > to), "a burst after the damage is checked"); for (const t of expected) { assert.ok(starts.some((s) => near(s, t)), `no cue at ${t}s (cues ${starts.join(", ")})`); } for (const s of starts) { assert.ok(!(s > from + 0.05 && s < to - 0.05), `cue at ${s}s inside the gap ${from}-${to}`); } // Normalized, as every transcription is. assert.ok(existsSync(path.join(VIDEO_DIR, "transcript.cues.json"))); // The salvage WAV and parakeet's window cache are gone. assert.equal(existsSync(path.join(VIDEO_DIR, "audio.salvage.wav")), false); assert.equal(existsSync(path.join(VIDEO_DIR, ".audio.salvage.wav.parakeet")), false); // The kept file is untouched. assert.ok(existsSync(path.join(VIDEO_DIR, "audio.mp4"))); }, ); test( "a file that decodes whole makes a clean transcript: nothing recorded as salvaged", { skip: !tools && "ffmpeg/ffprobe not installed" }, async () => { // The kept file was replaced by a clean one outside the managed download; // the sidecar still says corrupt-full-source, so salvage runs — and finds // nothing missing. const { transcribeOneVideo } = await import("./transcribeOne"); const { getPaths } = await import("../lib/paths"); const dir = path.join(path.dirname(VIDEO_DIR), "vid2"); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "audio.mp4"), await readFile(CLEAN)); await writeFile(path.join(dir, "metadata.info.json"), JSON.stringify({ id: "vid2", title: "Tones", duration: 30 })); await writeFile( path.join(dir, "download-outcome.json"), JSON.stringify({ videoId: "vid2", status: "corrupt-full-source", startedAt: "x", finishedAt: "x", attempts: [] }), ); const lines: string[] = []; const outcome = await transcribeOneVideo({ paths: getPaths(), videoDir: dir, videoId: "vid2", audioFilename: "audio.mp4", worker: worker(), onLog: (l) => lines.push(l), skipInlineDiarization: true, }); assert.equal(outcome, "transcribed", lines.join("\n")); assert.match(lines.join("\n"), /decodes whole; the transcript is a clean one/); const record = JSON.parse(await readFile(path.join(dir, "transcribe-outcome.json"), "utf8")); assert.equal(record.salvaged, undefined); const transcript = JSON.parse(await readFile(path.join(dir, "transcript.json"), "utf8")); assert.equal(transcript.chunk_data.length, burstTimes().length); }, );