// The sync report finds each clip's own sound in the finished cut. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { xfadeGraph } from "./build-video.mjs"; import { bestMatch, bestMatchFast, measureWindow, SYNC_FILE, writeSyncReport } from "./sync-report.mjs"; const have = spawnSync("ffmpeg", ["-version"]).status === 0; const RENDER = { width: 160, height: 90, fps: 30, audioRate: 48000, audioChannels: 2 }; test("measureWindow: past the joins, up to six seconds from three in; none under a second", () => { assert.deepEqual(measureWindow(30, 0.5), { from: 3, len: 6 }); assert.deepEqual(measureWindow(8, 0.5), { from: 1, len: 6 }); const w = measureWindow(4, 0.5); assert.equal(w.from, 0.6); assert.ok(Math.abs(w.len - 2.8) < 1e-9); assert.equal(measureWindow(1.5, 0.5), null); }); test("bestMatch / bestMatchFast: the offset a copy sits at", () => { let seed = 7; const rnd = () => ((seed = (seed * 1103515245 + 12345) % 2 ** 31) / 2 ** 31) - 0.5; // Low-passed noise, as speech is to a 4 kHz decode. const b = new Float32Array(4000); let y = 0; for (let i = 0; i < b.length; i += 1) b[i] = y = 0.7 * y + rnd(); const a = b.slice(1234, 1234 + 1000); assert.equal(bestMatch(a, b).offset, 1234); assert.equal(bestMatchFast(a, b).offset, 1234); assert.ok(bestMatch(a, b).peak > 0.95); }); function segment(dir, name, seed) { const out = path.join(dir, `${name}.mp4`); const r = spawnSync("ffmpeg", [ "-v", "error", "-y", "-f", "lavfi", "-i", "color=c=gray:s=160x90:r=30:d=8", "-f", "lavfi", "-i", `anoisesrc=d=8:c=pink:r=48000:a=0.3:seed=${seed}`, "-map", "0:v", "-map", "1:a", "-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-c:a", "aac", "-ac", "2", out, ]); assert.equal(r.status, 0, String(r.stderr)); return out; } test("writeSyncReport: in sync it reads 0; a cut whose sound is 100 ms late reads +100", { skip: !have && "no ffmpeg" }, async () => { const dir = mkdtempSync(path.join(tmpdir(), "sync-report-")); try { const segs = [segment(dir, "a", 1), segment(dir, "b", 2), segment(dir, "c", 3)]; const D = 0.5; const g = xfadeGraph([8, 8, 8], D, null, RENDER); const final = path.join(dir, "final.mp4"); let r = spawnSync("ffmpeg", ["-v", "error", "-y", ...segs.flatMap((s) => ["-i", s]), "-filter_complex", g.parts.join(";"), "-map", g.vlab, "-map", g.alab, "-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-c:a", "aac", final]); assert.equal(r.status, 0, String(r.stderr)); const items = segs.map((s, i) => ({ id: `c${i}`, segment: s, start: i * (8 - D), seconds: 8 })); const logs = []; const doc = await writeSyncReport({ outDir: dir, final, items, fps: 30, D, onLog: (l) => logs.push(l) }); for (const it of doc.items) assert.ok(Math.abs(it.lagMs) <= 2, `${it.id} lag ${it.lagMs}`); assert.deepEqual(logs, []); assert.deepEqual(JSON.parse(readFileSync(path.join(dir, SYNC_FILE), "utf8")).items.map((it) => it.id), ["c0", "c1", "c2"]); const late = path.join(dir, "late.mp4"); r = spawnSync("ffmpeg", ["-v", "error", "-y", "-i", final, "-af", "adelay=100:all=1", "-c:v", "copy", "-c:a", "aac", late]); assert.equal(r.status, 0, String(r.stderr)); const bad = await writeSyncReport({ outDir: dir, final: late, items, fps: 30, D, onLog: (l) => logs.push(l) }); for (const it of bad.items) assert.ok(Math.abs(it.lagMs - 100) <= 2, `${it.id} lag ${it.lagMs}`); assert.equal(logs.length, 3); assert.match(logs[0], /^sync: c0 sound late by 10\d ms/); } finally { rmSync(dir, { recursive: true, force: true }); } });