// The salvage decode (release 30, lane Q): a damaged file decoded for what it // still holds, ON THE SOURCE'S CLOCK — every burst after the damage at its // source time, the damage silence of its own length, the gap recorded. Real // ffmpeg over a synthetic tone-burst recording with a deterministic run of // bytes overwritten (testing/audio/corruptAudio.mjs). // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/salvageAudio-server.test.ts import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { frameAccumulator, gapSeconds, mergeGaps, salvageDecode, scanDecodable, } from "./salvageAudio-server"; import { burstTimes, corruptBytes, hasFfmpeg, makeToneFile, readWavMono16, toneOnsets, } from "../testing/audio/corruptAudio.mjs"; const tools = hasFfmpeg(); const deps = { ffmpegBin: "ffmpeg", ffprobeBin: "ffprobe" }; let ROOT = ""; let CLEAN = ""; let DAMAGED = ""; before(async () => { ROOT = await mkdtemp(path.join(tmpdir(), "salvage-audio-")); if (!tools) return; CLEAN = makeToneFile(path.join(ROOT, "clean.mp4")); DAMAGED = corruptBytes(CLEAN, path.join(ROOT, "damaged.mp4")); }); after(() => rm(ROOT, { recursive: true, force: true })); const near = (a: number, b: number, tol = 0.05) => Math.abs(a - b) <= tol; test("mergeGaps sorts, merges holes closer than a second, drops the short ones", () => { assert.deepEqual( mergeGaps([ [10, 10.2], [3, 4], [4.5, 6], [20, 20.3], [30, 29], ]), [[3, 6]], ); assert.deepEqual(mergeGaps([[1, 2], [5, 7.5]]), [[1, 2], [5, 7.5]]); assert.equal(gapSeconds([[1, 2], [5, 7.5]]), 3.5); }); test("frameAccumulator reads framecrc: a jump in pts is a hole, the tail to the container's end is one", () => { const acc = frameAccumulator(); for (const line of [ "#tb 0: 1/16000", "#stream#, dts, pts, duration, size, hash", "0, 0, 0, 16000, 32000, 0x00000000", "0, 16000, 16000, 16000, 32000, 0x00000000", // 3 s missing (48000 ticks), then two more seconds "0, 80000, 80000, 32000, 64000, 0x00000000", ]) { acc.feed(line); } const done = acc.finish(10); assert.equal(done.decodedSeconds, 4); assert.equal(done.end, 10); assert.deepEqual(done.gaps, [[2, 5], [7, 10]]); }); test("a clean file decodes whole, with no gaps", { skip: !tools && "ffmpeg/ffprobe not installed" }, async () => { const scan = await scanDecodable(CLEAN, deps); assert.ok(scan); assert.ok(near(scan.totalSeconds, 30), `total ${scan.totalSeconds}`); assert.ok(near(scan.decodedSeconds, 30, 0.1), `decoded ${scan.decodedSeconds}`); assert.deepEqual(scan.gaps, []); }); test("a damaged file: the scan finds the hole and counts only what decodes", { skip: !tools && "ffmpeg/ffprobe not installed" }, async () => { const scan = await scanDecodable(DAMAGED, deps); assert.ok(scan); assert.ok(near(scan.totalSeconds, 30), `total ${scan.totalSeconds}`); assert.equal(scan.gaps.length, 1, JSON.stringify(scan.gaps)); const [from, to] = scan.gaps[0]; assert.ok(to - from > 1, `gap ${from}-${to}`); // What decodes is the runtime less the hole (within one AAC frame or so). assert.ok(near(scan.decodedSeconds, 30 - (to - from), 0.1), `decoded ${scan.decodedSeconds}`); }); test("salvageDecode keeps the source's clock: bursts after the damage are at their source times", { skip: !tools && "ffmpeg/ffprobe not installed" }, async () => { const wav = path.join(ROOT, "salvage.wav"); const r = await salvageDecode(DAMAGED, wav, deps); assert.equal(r.gaps.length, 1); const [from, to] = r.gaps[0]; const { samples, rate, seconds } = readWavMono16(wav); assert.equal(rate, 16000); // Padded out to the container's runtime. assert.ok(near(seconds, 30), `wav ${seconds}s`); const onsets = toneOnsets(samples, rate); // Every burst wholly outside the damage is where the source has it. 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( onsets.some((o) => near(o, t)), `burst at ${t}s missing from the salvage (onsets ${onsets.join(", ")})`, ); } // No burst inside the damage: it is silence, not audio from later on. for (const o of onsets) { assert.ok(!(o > from + 0.05 && o < to - 0.05), `onset ${o} inside the gap ${from}-${to}`); } }); test("the control: a plain decode closes the damage up, and every later burst lands early", { skip: !tools && "ffmpeg/ffprobe not installed" }, async () => { // Without this, the test above could pass on a file whose damage cost no // time at all. The plain decode the salvage replaces shifts the clock. const naive = path.join(ROOT, "naive.wav"); execFileSync("ffmpeg", ["-nostdin", "-v", "quiet", "-y", "-err_detect", "ignore_err", "-i", DAMAGED, "-ac", "1", "-ar", "16000", "-c:a", "pcm_s16le", naive]); const scan = await scanDecodable(DAMAGED, deps); assert.ok(scan); const [from, to] = scan.gaps[0]; const { samples, rate, seconds } = readWavMono16(naive); assert.ok(seconds < 30 - (to - from) + 0.1, `naive ${seconds}s`); const late = burstTimes().filter((t) => t > to); const onsets = toneOnsets(samples, rate); for (const t of late) { assert.ok(!onsets.some((o) => near(o, t)), `naive decode kept ${t}s on the clock`); assert.ok(onsets.some((o) => near(o, t - (to - from), 0.1)), `burst ${t}s not shifted by the gap`); } }); test("salvageDecode refuses a file nothing in which decodes", { skip: !tools && "ffmpeg/ffprobe not installed" }, async () => { const junk = path.join(ROOT, "junk.mp4"); corruptBytes(CLEAN, junk, { fromFrac: 0, toFrac: 1 }); await assert.rejects(() => salvageDecode(junk, path.join(ROOT, "junk.wav"), deps), /nothing in junk\.mp4 decodes/); });