// transcriptionContainer (release 23 F2): before the page's Transcribe // downloads audio, a container already here — a source-media. in the // video dir, else the saved-video store's — is transcribed instead; a pointer // whose container cannot be read is refused with the media guard's sentence; // a container with no sound, or none at all, takes the download path. // // Real ffprobe over tiny lavfi containers (a second at 160×90). // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/transcriptionContainer.test.ts import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { copyFile, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import type { Paths } from "../lib/paths"; import { transcriptionContainer } from "./transcribeOne"; const SLUG = "demo-channel"; let ROOT = ""; let WITH_SOUND = ""; let SILENT = ""; const ff = (args: string[]) => execFileSync("ffmpeg", ["-nostdin", "-v", "error", "-y", ...args]); before(async () => { ROOT = await mkdtemp(path.join(tmpdir(), "transcription-container-")); WITH_SOUND = path.join(ROOT, "with-sound.mp4"); SILENT = path.join(ROOT, "silent.mp4"); ff([ "-f", "lavfi", "-i", "testsrc=size=160x90:rate=10:duration=1", "-f", "lavfi", "-i", "sine=frequency=440:duration=1", "-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-c:a", "aac", "-shortest", WITH_SOUND, ]); ff([ "-f", "lavfi", "-i", "testsrc=size=160x90:rate=10:duration=1", "-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", SILENT, ]); }); after(() => rm(ROOT, { recursive: true, force: true })); async function video(): Promise<{ paths: Paths; videoDir: string; store: string }> { const root = await mkdtemp(path.join(ROOT, "t-")); const paths = { transcriptsDir: path.join(root, "transcripts"), savedVideosDir: path.join(root, "transcripts", "saved-videos"), ffprobeBin: "ffprobe", } as Paths; const videoDir = path.join(paths.transcriptsDir, "channels", SLUG, "data", "abc123"); const store = path.join(paths.savedVideosDir, SLUG, "abc123"); await mkdir(videoDir, { recursive: true }); await mkdir(store, { recursive: true }); return { paths, videoDir, store }; } async function point(videoDir: string, store: string, file: string | null): Promise { if (file) await copyFile(file, path.join(store, "source-media.mp4")); await writeFile( path.join(videoDir, "saved-video.json"), JSON.stringify({ storedAt: "", dir: store, file: "source-media.mp4", bytes: 1 }), ); return path.join(store, "source-media.mp4"); } const ask = (paths: Paths, videoDir: string, entries: string[]) => transcriptionContainer({ paths, slug: SLUG, videoDir, entries }); test("a saved container with sound is transcribed from, not downloaded around", async () => { const { paths, videoDir, store } = await video(); const file = await point(videoDir, store, WITH_SOUND); assert.deepEqual(await ask(paths, videoDir, ["saved-video.json"]), { kind: "container", file, from: "saved-video" }); }); test("a pointer whose container cannot be read is refused with the media guard's sentence", async () => { const { paths, videoDir, store } = await video(); const file = await point(videoDir, store, null); const r = await ask(paths, videoDir, ["saved-video.json"]); assert.equal(r.kind, "unreadable"); assert.equal( r.kind === "unreadable" ? r.error : "", `Channel "${SLUG}": media is not reachable — its saved video ${file} is not there (drive not mounted?)`, ); }); test("no container at all: the download path", async () => { const { paths, videoDir } = await video(); assert.deepEqual(await ask(paths, videoDir, ["metadata.info.json"]), { kind: "none" }); }); test("a container with no sound: the download path", async () => { const { paths, videoDir, store } = await video(); const file = await point(videoDir, store, SILENT); assert.deepEqual(await ask(paths, videoDir, ["saved-video.json"]), { kind: "no-audio", file }); }); test("a source-media container in the video dir comes first, as resolveAudioFile reads it", async () => { const { paths, videoDir, store } = await video(); await point(videoDir, store, SILENT); await copyFile(WITH_SOUND, path.join(videoDir, "source-media.mp4")); assert.deepEqual(await ask(paths, videoDir, ["saved-video.json", "source-media.mp4"]), { kind: "container", file: path.join(videoDir, "source-media.mp4"), from: "video-dir", }); // A silent one there is the download path (the worker would read it first). await copyFile(SILENT, path.join(videoDir, "source-media.mp4")); assert.deepEqual(await ask(paths, videoDir, ["saved-video.json", "source-media.mp4"]), { kind: "no-audio", file: path.join(videoDir, "source-media.mp4"), }); // One ffprobe cannot read is passed over for the pointer. await writeFile(path.join(videoDir, "source-media.mp4"), "not a container"); await point(videoDir, store, WITH_SOUND); assert.equal((await ask(paths, videoDir, ["saved-video.json", "source-media.mp4"])).kind, "container"); });