// `umtool scenes` (2026-10-25): scene cuts and black spans in a clip's fetched // window, in SOURCE seconds. The parsers on ffmpeg's own log lines; then one // real scan of a four-second window made here (red, black, blue), found the // way the build finds it (the editor's window cache, channels//data//clips/). // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-scenes-cli-")); process.env.REPORTS_DIR = path.join(ROOT, "reports"); const { parseSceneLog, scanArgs, scenesCommand, toSourceSeconds } = await import("./scenesCli.mjs"); const hasFfmpeg = (() => { try { execFileSync(process.env.FFMPEG_BIN ?? "ffmpeg", ["-version"], { stdio: "ignore" }); return true; } catch { return false; } })(); test.after(() => rm(ROOT, { recursive: true, force: true })); const LOG = [ "[Parsed_scdet_2 @ 0x7f5f580103c0] lavfi.scd.score: 29.058, lavfi.scd.time: 5.866667", "frame= 189 fps=0.0 q=-0.0 size=N/A time=00:00:06.30 bitrate=N/A speed=12.6x", "[Parsed_scdet_2 @ 0x7f5f580103c0] lavfi.scd.score: 26.976, lavfi.scd.time: 23.133333", "[Parsed_blackdetect_1 @ 0x7f5f58010140] black_start:23.133333 black_end:23.733333 black_duration:0.6", "[Parsed_scdet_2 @ 0x7f5f580103c0] lavfi.scd.score: 29.097, lavfi.scd.time: 23.733333", ].join("\n"); test("parseSceneLog + toSourceSeconds: eps-footage's window, as found by hand (black 250.49–251.09, cut 251.09)", () => { const ev = parseSceneLog(LOG); assert.equal(ev.cuts.length, 3); assert.deepEqual(ev.black, [{ from: 23.133333, to: 23.733333 }]); // The window 227.36–299.00, scanned from its start: t = 0 is source 227.36. const s = toSourceSeconds(ev, 227.36, { start: 228.36, end: 260.03 }); assert.deepEqual(s.black, [{ from: 250.49, to: 251.09, dur: 0.6, inClip: true }]); assert.deepEqual(s.cuts.map((c) => c.at), [233.23, 250.49, 251.09]); assert.equal(s.cuts[2].score, 29.097); assert.equal(toSourceSeconds(ev, 227.36, { start: 260, end: 270 }).black[0].inClip, false); }); test("scanArgs: one pass, seek and length, both filters with their knobs", () => { const a = scanArgs("/w.mp4", { seek: 1.5, dur: 30, blackMin: 0.05, threshold: 8, pixTh: 0.1 }); assert.deepEqual(a.slice(a.indexOf("-ss"), a.indexOf("-ss") + 4), ["-ss", "1.500", "-t", "30.000"]); assert.match(a[a.indexOf("-vf") + 1], /blackdetect=d=0\.05:pix_th=0\.1,scdet=threshold=8$/); }); test("scenes: the clip's window on disk, scanned; source seconds out; a clip with no window refused", { skip: !hasFfmpeg && "no ffmpeg" }, async () => { const channels = path.join(ROOT, "channels"); const clips = path.join(channels, "chan", "data", "vid1", "clips"); await mkdir(clips, { recursive: true }); // 1.5 s red, 0.6 s black, 1.9 s blue: the window 10.00–14.00. execFileSync(process.env.FFMPEG_BIN ?? "ffmpeg", [ "-v", "error", "-y", "-f", "lavfi", "-i", "color=c=red:s=160x90:r=25:d=1.5", "-f", "lavfi", "-i", "color=c=black:s=160x90:r=25:d=0.6", "-f", "lavfi", "-i", "color=c=blue:s=160x90:r=25:d=1.9", "-filter_complex", "[0:v][1:v][2:v]concat=n=3:v=1:a=0[v]", "-map", "[v]", "-c:v", "libx264", "-pix_fmt", "yuv420p", path.join(clips, "10.00-14.00.mp4"), ]); const dir = path.join(ROOT, "reports", "scan"); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: "scan", provenance: { siteOrigin: "https://example.test", channelSlug: "chan", channelsDir: channels }, render: { fetchPad: 0.5 }, timeline: [ { type: "clip", id: "c01", video: "vid1", start: 10.5, end: 13.5 }, { type: "clip", id: "gone", video: "vid2", start: 1, end: 3 }, { type: "card", id: "k", heading: "x" }, ], })); const lines = []; const run = (...args) => scenesCommand(args, { root: path.join(ROOT, "reports"), out: (s) => lines.push(s), err: (s) => lines.push(`ERR ${s}`) }); assert.equal(await run("scan", "c01", "--json"), 0); const s = JSON.parse(lines.at(-1)); assert.equal(s.window.kind, "corpus-window"); assert.deepEqual([s.window.from, s.window.to], [10, 14]); assert.deepEqual(s.scanned, { from: 10, to: 14 }); assert.equal(s.black.length, 1); assert.ok(Math.abs(s.black[0].from - 11.5) <= 0.05, `black from ${s.black[0].from}`); assert.ok(Math.abs(s.black[0].to - 12.1) <= 0.05, `black to ${s.black[0].to}`); assert.ok(s.cuts.some((c) => Math.abs(c.at - 12.1) <= 0.05), JSON.stringify(s.cuts)); // --from/--to narrow the scan: the seek lands on the same source clock. assert.equal(await run("scan", "c01", "--from", "11", "--to", "13", "--json"), 0); const n = JSON.parse(lines.at(-1)); assert.deepEqual(n.scanned, { from: 11, to: 13 }); assert.ok(Math.abs(n.black[0].from - 11.5) <= 0.05 && Math.abs(n.black[0].to - 12.1) <= 0.05, JSON.stringify(n.black)); assert.equal(await run("scan", "gone"), 1); assert.match(lines.at(-1), /has no window on disk — `umtool fetch scan`/); assert.equal(await run("scan", "k"), 2); assert.match(lines.at(-1), /not a clip/); assert.equal(await run("scan"), 2); });