// `umtool voice-match` (release 34 N2): the plan (which seconds of which file), // the reference (keeps, else solo recordings on disk), the CLI end to end over a // synthetic corpus with the test kit's fake embedder, and the app's job step. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; import { encodeWav16 } from "yt-dlp-transcript-common/lib/montage/voiceMatch.mjs"; import { VOICE_A, VOICE_B, syntheticVoice, withSilence } from "yt-dlp-transcript-common/testing/audio/syntheticVoice.mjs"; import { PROJECT_KINDS } from "../projects/kinds.mjs"; import { keepRulings, planClips, referenceRecordings, voiceMatchCommand, voiceMatchSteps } from "./voiceMatch.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const FAKE = path.resolve(HERE, "..", "..", "..", "common", "testing", "audio", "fakeSpeakerEmbed.mjs"); const ENGINE = { python: process.execPath, script: FAKE, model: "fake.onnx" }; const KIND = PROJECT_KINDS.find((k) => k.makesTakes).id; const hasFfmpeg = (() => { try { execFileSync("ffmpeg", ["-version"], { stdio: "ignore" }); return true; } catch { return false; } })(); /** A wav of `x`, encoded by ffmpeg to an audio-only AAC file at `out`. */ async function encode(x, out) { const wav = `${out}.wav`; await writeFile(wav, encodeWav16(x)); execFileSync("ffmpeg", ["-v", "error", "-y", "-i", wav, "-c:a", "aac", "-b:a", "96k", out]); await rm(wav); } const cuesDoc = (id, cues, extra = {}) => ({ id, title: `Upload ${id}`, uploadDate: "20200101", duration: 200, source: "vtt", isLivestream: false, platform: "youtube", cues, ...extra }); /** * A corpus and a project: three solo uploads of VOICE_A with their sound on * disk (the reference), and clips whose windows say "slow news day" in * VOICE_A (s01, s03, s04) or VOICE_B (s02, the guest). */ async function fixture() { const root = await mkdtemp(path.join(tmpdir(), "voice-match-")); const chans = path.join(root, "channels"); const data = (id) => path.join(chans, "chan", "data", id); for (const [i, date] of ["20190101", "20210101", "20230101"].entries()) { const id = `solo${i}`; await mkdir(data(id), { recursive: true }); await writeFile(path.join(data(id), "transcript.cues.json"), JSON.stringify(cuesDoc(id, [{ start: 2, end: 5, text: "what is going on everyone" }], { uploadDate: date }))); await encode(withSilence(syntheticVoice({ ...VOICE_A, seconds: 66, phase: i * 0.9 }), { before: 2 }), path.join(data(id), "audio.m4a")); } // An upload that is NOT a reference: a ">>" speaker change. await mkdir(data("duo"), { recursive: true }); await writeFile(path.join(data("duo"), "transcript.cues.json"), JSON.stringify(cuesDoc("duo", [{ start: 2, end: 5, text: ">> and you" }]))); await encode(syntheticVoice({ ...VOICE_B, seconds: 70 }), path.join(data("duo"), "audio.m4a")); const clips = [ ["s01", "host1", VOICE_A, 1.7], ["s02", "guest1", VOICE_B, 0.4], ["s03", "host2", VOICE_A, 3.1], ["s04", "host3", VOICE_A, 4.6], ]; const timeline = []; for (const [id, video, voice, phase] of clips) { // The window is source seconds 100–108; the phrase is said at 102.5–104. await mkdir(path.join(data(video), "clips"), { recursive: true }); await writeFile(path.join(data(video), "transcript.cues.json"), JSON.stringify(cuesDoc(video, [{ start: 102.5, end: 104, text: "slow news day" }]))); const sound = withSilence(syntheticVoice({ ...voice, seconds: 2.5, phase }), { before: 2, after: 3.5 }); await encode(sound, path.join(data(video), "clips", "100.00-108.00.mp4")); timeline.push({ type: "clip", id, channel: "chan", video, start: 100, end: 108, cutStart: 102.5, cutEnd: 104, cite: 102.5, montage: { match: "slow news day" } }); } const reportsRoot = path.join(root, "reports"); const dir = path.join(reportsRoot, "montage"); await mkdir(dir, { recursive: true }); const manifest = { schemaVersion: 1, slug: "montage", title: "Montage", kind: KIND, provenance: { channelsDir: "../../channels", channelSlug: "chan" }, render: {}, timeline }; await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(manifest, null, 1)); return { root, chans, dir, reportsRoot, manifest }; } const quiet = () => { const out = []; return { out, log: (s) => out.push(s), err: (s) => out.push(s) }; }; test("voice-match: the plan, the reference recordings, and the CLI over a synthetic corpus", { skip: !hasFfmpeg && "needs ffmpeg" }, async () => { const f = await fixture(); try { // The plan: the phrase found in the cues, ± 1 s, on the window's clock. const plan = await planClips(f.dir, f.manifest, { channelsDir: f.chans }); assert.equal(plan.length, 4); const s01 = plan.find((c) => c.id === "s01"); assert.equal(s01.key, "chan/host1@102.50"); assert.equal(s01.via, "cues"); assert.ok(Math.abs(s01.start - 1.5) < 0.05 && Math.abs(s01.dur - 3.5) < 0.05, `+${s01.start} ${s01.dur}s`); assert.ok(s01.fallback, "the cut ± the pad is the fallback"); // The reference: the three solo uploads, not the one with a ">>". const ref = await referenceRecordings({ channelsDir: f.chans, channels: ["chan"] }); assert.deepEqual([...new Set(ref.items.map((i) => i.video))], ["solo0", "solo1", "solo2"]); assert.equal(ref.items.length, 18, "six 10 s pieces each"); assert.ok(Math.abs(ref.items[0].start - 2) < 0.05, "from the first cue"); // The CLI end to end. const q = quiet(); const code = await voiceMatchCommand(["montage", "--json"], { reportsRoot: f.reportsRoot, ...q, engine: ENGINE }); assert.equal(code, 0, q.out.join("\n")); const doc = JSON.parse(await readFile(path.join(f.dir, "voice-match.json"), "utf8")); assert.equal(doc.version, 1); assert.equal(doc.model, "titanet-small"); assert.equal(doc.reference.kind, "recordings"); assert.equal(doc.reference.recordings.length, 3); assert.equal(doc.clips["chan/host1@102.50"].label, "likely"); assert.equal(doc.clips["chan/guest1@102.50"].label, "unlikely"); assert.equal(doc.clips["chan/host1@102.50"].id, "s01"); assert.ok(doc.clips["chan/host1@102.50"].speechSeconds <= 2.6, "the pad's silence is not speech"); // Three keeps: the reference is the ruled clips, each scored against the others. await writeFile( path.join(f.dir, "montage-rulings.json"), JSON.stringify({ version: 1, rulings: Object.fromEntries(["host1", "host2", "host3"].map((v) => [`chan/${v}@102.50`, { verdict: "keep", reason: "", at: "2026-10-18T00:00:00Z" }])) }), ); assert.deepEqual(await keepRulings(f.dir), ["chan/host1@102.50", "chan/host2@102.50", "chan/host3@102.50"]); assert.equal(await voiceMatchCommand(["montage"], { reportsRoot: f.reportsRoot, ...quiet(), engine: ENGINE }), 0); const ruled = JSON.parse(await readFile(path.join(f.dir, "voice-match.json"), "utf8")); assert.equal(ruled.reference.kind, "rulings"); assert.equal(ruled.clips["chan/host2@102.50"].inReference, true); assert.equal(ruled.clips["chan/guest1@102.50"].label, "unlikely"); // --dry plans and writes nothing new. const dry = quiet(); assert.equal(await voiceMatchCommand(["montage", "--dry", "--reference", "recordings"], { reportsRoot: f.reportsRoot, ...dry, engine: ENGINE }), 0); assert.match(dry.out.join("\n"), /s02 {2}chan\/guest1@102\.50 {2}cues {2}100\.00-108\.00\.mp4/); } finally { await rm(f.root, { recursive: true, force: true }); } }); test("voice-match: refusals in words", async () => { const q = quiet(); assert.equal(await voiceMatchCommand(["no-such-project"], { reportsRoot: path.join(tmpdir(), "nowhere-voice"), ...q }), 2); assert.match(q.out.join("\n"), /no project matches/); const help = quiet(); assert.equal(await voiceMatchCommand(["--help"], { ...help }), 0); assert.match(help.out.join("\n"), /usage: umtool voice-match/); }); test("voiceMatchSteps: one nice'd job step running the CLI, NDJSON", () => { const plan = voiceMatchSteps({ id: "montage" }, { reference: "recordings" }); assert.equal(plan.kind, "voice-match montage"); assert.equal(plan.steps.length, 1); const [s] = plan.steps; assert.deepEqual(s.argv.slice(0, 4), ["nice", "-n", "10", "node"]); assert.match(s.argv[4], /umtool[\\/]bin[\\/]umtool\.mjs$/); assert.deepEqual(s.argv.slice(5), ["voice-match", "montage", "--reference", "recordings", "--progress", "ndjson"]); assert.equal(s.ndjson, true); assert.equal(voiceMatchSteps({ id: "m" }, { reference: "bogus" }).steps[0].argv.includes("auto"), true); });