// A clip's picture blurred at its segment encode (clip-blur.mjs): the knob's // checks, the cut's switch (render.blur "off": the uncensored variant), the // filters it puts on the picture and ONLY the picture (the header, the pad and // everything laid over the frame later stay sharp), a manifest without it the // graph it always was, --chrome-only refusing segments blurred another way -- // and one tiny real render: the picture's region smoothed, the header's not. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { BLUR_CAPTION_DEFAULT, BLUR_STRENGTH, blurCaptionLayout, blurPictureFilters, blurProblems, blurRecordOf, blurSpan, clipBlurOf, fittedSize, validateClipBlur, validateClipBlurEntries, } from "./clip-blur.mjs"; import { clipBaseFilters, deckFraming, deckFramingFilter } from "./build-video.mjs"; import { validatorErrors } from "./lint.mjs"; import { validateLook } from "./look.mjs"; import { MONO_BOLD_FONT_FILE, MONO_FONT_FILE } from "./brand.mjs"; const clip = (extra = {}) => ({ type: "clip", id: "dog", video: "v1", start: 228.36, end: 298, ...extra }); const PAL = { bg: "#0b0d10", fg: "#e8eaed", muted: "#9aa0a6", accent: "#f5a623" }; const RENDER = { width: 640, height: 360, fps: 30, palette: PAL, fontRegular: MONO_FONT_FILE, fontBold: MONO_BOLD_FONT_FILE }; test("validateClipBlur: the good shapes pass, every bad one is refused in a sentence naming the key", () => { assert.deepEqual(validateClipBlur(undefined), []); assert.deepEqual(validateClipBlur(null), []); assert.deepEqual(validateClipBlur({ on: true }), []); assert.deepEqual(validateClipBlur({ on: true, caption: "Blurred for TOS", strength: 8, from: 230, to: 260 }, "blur", clip()), []); assert.deepEqual(validateClipBlur({ on: true, from: 240 }, "blur", clip()), [], "from alone runs to the clip's end"); const bad = (b, re, entry = null) => { const errs = validateClipBlur(b, "blur", entry); assert.ok(errs.some((e) => re.test(e)), `${JSON.stringify(b)} → ${JSON.stringify(errs)}`); }; bad("yes", /must be \{ on: true/); bad({}, /on must be true/); bad({ on: false }, /on must be true/); bad({ on: true, sigma: 3 }, /blur\.sigma is not a blur setting/); bad({ on: true, caption: "" }, /caption must be a text of 1 to 120/); bad({ on: true, caption: "x".repeat(121) }, /caption must be a text of 1 to 120/); bad({ on: true, strength: 1 }, /strength must be a number from 2 to 15/); bad({ on: true, strength: 16 }, /strength must be a number from 2 to 15/); bad({ on: true, strength: "5" }, /strength must be a number/); bad({ on: true, from: -1 }, /from must be a source second/); bad({ on: true, from: 250, to: 250.1 }, /at least 0\.2 s before/); bad({ on: true, from: 200 }, /from 200 must lie inside the clip's window 228\.36–298/, clip()); bad({ on: true, to: 300 }, /to 300 must lie inside the clip's window/, clip()); }); test("validateClipBlurEntries: only a clip, not under a montage; render.blur is the look's", () => { const m = { render: {}, timeline: [clip({ blur: { on: true } }), { type: "card", id: "k", blur: { on: true } }] }; assert.deepEqual(validateClipBlurEntries(m), ["timeline[1] (k).blur: only a clip's picture can be blurred"]); const mont = { render: { montage: { style: "x" } }, timeline: [clip({ blur: { on: true } })] }; assert.match(validateClipBlurEntries(mont)[0], /does not combine with render\.montage/); assert.deepEqual(validateLook({ render: { blur: "off" } }), []); assert.deepEqual(validateLook({ render: { blur: "on" } }), []); assert.match(validateLook({ render: { blur: "no" } })[0], /render\.blur must be "on".*or "off"/); // The lint (and the build: validatorErrors is its list) say the same. const lint = validatorErrors({ render: { blur: false }, timeline: [clip({ blur: { on: true, strength: 99 } })] }); assert.ok(lint.some((e) => /render\.blur must be/.test(e)), JSON.stringify(lint)); assert.ok(lint.some((e) => /timeline\[0\] \(dog\)\.blur\.strength/.test(e)), JSON.stringify(lint)); }); test("clipBlurOf: the defaults filled; none without the key; render.blur off is none for every clip", () => { assert.equal(clipBlurOf(clip(), {}), null); assert.deepEqual(clipBlurOf(clip({ blur: { on: true } }), {}), { strength: BLUR_STRENGTH.default, caption: BLUR_CAPTION_DEFAULT, from: null, to: null }); assert.deepEqual(clipBlurOf(clip({ blur: { on: true, caption: " Blurred ", strength: 9, from: 240 } }), { blur: "on" }), { strength: 9, caption: "Blurred", from: 240, to: null }); assert.equal(clipBlurOf(clip({ blur: { on: true } }), { blur: "off" }), null, "the uncensored variant"); assert.equal(clipBlurOf(clip({ blur: { on: true, strength: 99 } }), {}), null, "an invalid blur is the validator's to refuse, never half-drawn"); }); test("blurSpan: whole, a span in the segment's own seconds, or none when it misses", () => { const b = (from, to) => ({ strength: 5, caption: "c", from, to }); assert.equal(blurSpan(null, 100, 10), null); assert.deepEqual(blurSpan(b(null, null), 100, 10), { whole: true }); assert.deepEqual(blurSpan(b(102, 105), 100, 10), { a: 2, b: 5 }); assert.deepEqual(blurSpan(b(102, null), 100, 10), { a: 2, b: 10 }); assert.deepEqual(blurSpan(b(null, 104.5), 100, 10), { a: 0, b: 4.5 }); assert.deepEqual(blurSpan(b(95, 120), 100, 10), { whole: true }, "covering the whole segment"); assert.equal(blurSpan(b(111, 115), 100, 10), null); }); test("the filters: gblur scaled to the picture's height, one drawtext a caption line, enable over a partial span", () => { const blur = { strength: 5, caption: BLUR_CAPTION_DEFAULT, from: null, to: null }; const layout = blurCaptionLayout(blur.caption, 1600, 900); assert.equal(layout.fontSize, 38); assert.ok(layout.lines.length >= 1 && layout.lines.every((l) => l.length <= Math.floor((1600 * 0.84) / (38 * 0.6)))); assert.equal(layout.lines.join(" "), BLUR_CAPTION_DEFAULT); const files = layout.lines.map((_, k) => `/seg/dog.blur-${k}.txt`); const whole = blurPictureFilters({ blur, span: { whole: true }, h: 900, layout, lineFiles: files, render: RENDER }); assert.equal(whole[0], "gblur=sigma=45:steps=3"); assert.equal(whole.length, 1 + layout.lines.length); assert.match(whole[1], /^drawtext=textfile='\/seg\/dog\.blur-0\.txt':fontfile='[^']*IBMPlexMono-Bold\.ttf':fontsize=38:fontcolor=#e8eaed:box=1:boxcolor=#0b0d10@0\.78/); assert.ok(!whole.some((f) => f.includes("enable"))); const part = blurPictureFilters({ blur, span: { a: 1.5, b: 4.25 }, h: 900, layout, lineFiles: files, render: RENDER }); assert.ok(part.every((f) => f.endsWith(":enable='between(t,1.5,4.25)'")), JSON.stringify(part)); assert.deepEqual(blurPictureFilters({ blur, span: null, h: 900, layout, lineFiles: files, render: RENDER }), []); assert.deepEqual(fittedSize(1280, 720, 1600, 900), { w: 1600, h: 900 }); assert.deepEqual(fittedSize(720, 1280, 1600, 900), { w: 506, h: 900 }); }); test("only the picture: the blur sits between the scale and the pad; without it the chain is the bytes it was", () => { const render = { ...RENDER, width: 1920, height: 1080, chrome: { engine: "hyperframes", layout: "deck" } }; // The deck: the picture into the footage box; the deck itself is laid over the concat later. const plain = deckFramingFilter(render, {}); assert.equal(deckFramingFilter(render, { picture: [] }), plain, "no blur: the same string"); const { fit, place } = deckFraming(render); assert.equal(plain, [...fit, ...place].join(",")); const blurred = deckFraming(render, { picture: ["gblur=sigma=40:steps=3"] }); assert.deepEqual(blurred.fit, [fit[0], "gblur=sigma=40:steps=3", fit[1]], "scaled, blurred, THEN padded"); assert.deepEqual(blurred.place, place); // No deck: the header is drawn AFTER the blur, on the padded frame. const args = { render: { ...RENDER, width: 640, height: 360 }, VW: 640, VH: 304, HH: 56, hasHeader: true, attribPath: "/seg/a.txt" }; const before = [ "scale=640:304:force_original_aspect_ratio=decrease", `pad=640:304:(ow-iw)/2:(oh-ih)/2:color=${PAL.bg}`, `pad=640:304:0:0:color=${PAL.bg}`, `pad=640:360:0:56:color=${PAL.bg}`, "setsar=1", "fps=30", ]; const base = clipBaseFilters(args); assert.deepEqual(base.slice(0, 6), before, "without a blur: the chain as it always was"); assert.match(base[base.length - 1], /^drawtext=textfile='\/seg\/a\.txt'/); const withBlur = clipBaseFilters({ ...args, picture: ["gblur=sigma=15:steps=3", "drawtext=textfile='/seg/dog.blur-0.txt'"] }); const g = withBlur.findIndex((f) => f.startsWith("gblur")); const firstPad = withBlur.findIndex((f) => f.startsWith("pad=")); const header = withBlur.findIndex((f) => f.startsWith("drawtext=textfile='/seg/a.txt'")); assert.equal(g, 1); assert.ok(g < firstPad && firstPad < header, JSON.stringify(withBlur)); assert.equal(withBlur.filter((f) => f.startsWith("gblur")).length, 1); }); test("--chrome-only: a segment blurred another way than the cut asks is refused by name", () => { const e = clip({ blur: { on: true } }); const want = blurRecordOf(clipBlurOf(e, {})); assert.deepEqual(blurProblems({ entries: [e], records: [{ blur: want }], render: {} }), []); assert.deepEqual(blurProblems({ entries: [clip()], records: [{}], render: {} }), [], "neither: nothing to say"); assert.deepEqual(blurProblems({ entries: [clip()], records: [null], render: {} }), []); assert.match(blurProblems({ entries: [e], records: [{ blur: want }], render: { blur: "off" } })[0], /dog \(blurred on disk, clear in this cut\)/); assert.match(blurProblems({ entries: [e], records: [{}], render: {} })[0], /dog \(clear on disk, blurred in this cut\)/); const moved = clip({ blur: { on: true, from: 240 } }); assert.match(blurProblems({ entries: [moved], records: [{ blur: want }], render: {} })[0], /blurred another way/); }); // ---- one tiny real render -------------------------------------------------- const have = spawnSync("ffmpeg", ["-version"]).status === 0; /** The gray frame `file` at `t` as raw bytes (w × h). */ function grayFrame(file, w, h) { const r = spawnSync("ffmpeg", ["-v", "error", "-i", file, "-frames:v", "1", "-f", "rawvideo", "-pix_fmt", "gray", "-"], { maxBuffer: 1 << 24 }); assert.equal(r.status, 0, String(r.stderr)); assert.equal(r.stdout.length, w * h); return r.stdout; } /** * The detail in [x, y, w, h] of a gray frame `W` wide: the mean squared step * between neighbouring pixels. A blur takes the edges out of a picture, so it * drops by an order of magnitude; the large areas of colour stay. */ function detail(buf, W, [x, y, w, h]) { let n = 0; let sq = 0; for (let j = y; j < y + h; j += 1) { for (let i = x; i < x + w - 1; i += 1) { const d = buf[j * W + i + 1] - buf[j * W + i]; const e = buf[(j + 1) * W + i] - buf[j * W + i]; sq += d * d + e * e; n += 1; } } return sq / n; } test("a tiny real render: the picture's region is smoothed, the header's is the same pixels", { skip: !have && "no ffmpeg" }, () => { const dir = mkdtempSync(path.join(tmpdir(), "clip-blur-")); try { const W = 320; const H = 180; const HH = 28; const VH = H - HH; const render = { ...RENDER, width: W, height: H, fps: 10 }; const attrib = path.join(dir, "attrib.txt"); writeFileSync(attrib, "Channel · a title · 2026-10-23"); const blur = clipBlurOf(clip({ blur: { on: true } }), render); const fit = fittedSize(W * 2, VH * 2, W, VH); const layout = blurCaptionLayout(blur.caption, fit.w, fit.h); const lineFiles = layout.lines.map((line, k) => { const f = path.join(dir, `blur-${k}.txt`); writeFileSync(f, line); return f; }); const picture = blurPictureFilters({ blur, span: { whole: true }, h: fit.h, layout, lineFiles, render }); const encode = (chain, out) => { const r = spawnSync("ffmpeg", [ "-v", "error", "-y", "-f", "lavfi", "-i", `testsrc2=s=${W * 2}x${VH * 2}:r=10:d=0.3,format=yuv420p`, // The segment's own output format (encodeArgs: -pix_fmt yuv420p), so both // chains negotiate it from the source on, as a build's do. "-vf", [...chain, "format=yuv420p"].join(","), "-frames:v", "1", "-c:v", "png", out, ]); assert.equal(r.status, 0, String(r.stderr)); return grayFrame(out, W, H); }; const base = { render, VW: W, VH, HH, hasHeader: true, attribPath: attrib }; const sharp = encode(clipBaseFilters(base), path.join(dir, "sharp.png")); const soft = encode(clipBaseFilters({ ...base, picture }), path.join(dir, "soft.png")); // Below the caption, in the picture: the test pattern's edges are gone. const region = [20, HH + Math.round(VH * 0.55), W - 40, Math.round(VH * 0.35)]; const ds = detail(sharp, W, region); const db = detail(soft, W, region); assert.ok(db < ds * 0.1, `the picture's detail ${db.toFixed(1)} should be a tenth of ${ds.toFixed(1)} or less`); // The header band: drawn after the blur, byte for byte the same. const header = sharp.subarray(0, W * HH); assert.ok(header.some((v) => v !== header[0]), "the header has its words drawn"); assert.deepEqual(soft.subarray(0, W * HH), header); } finally { rmSync(dir, { recursive: true, force: true }); } });