// render.transitionStyle: the crossfade's look between clips (transition.mjs). // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { xfadeGraph } from "./build-video.mjs"; import { execFileSync } from "node:child_process"; import { createHash } from "node:crypto"; import { SLIDE_DEFAULTS, TRANSITION_STYLES, blurSlideEdges, pushExpr, slideOf, transitionStyleOf, validateSlide, validateTransitionStyle, xfadeTransitionOf } from "./transition.mjs"; const RENDER = { width: 1920, height: 1080, fps: 30, transition: 0.5, audioRate: 48000, audioChannels: 2 }; const xfades = (g) => g.parts.filter((p) => p.includes("xfade=")); test("transitionStyle: absent is the dissolve every cut always had", () => { assert.equal(transitionStyleOf(RENDER), "fade"); assert.equal(transitionStyleOf(null), "fade"); assert.deepEqual(xfades(xfadeGraph([10, 12], 0.5, null, RENDER)), ["[0:v][1:v]xfade=transition=fade:duration=0.5:offset=9.500[v1]"]); }); test("transitionStyle: fadeblack dips every plain join through black, same timing", () => { const g = xfadeGraph([10, 12, 4], 0.5, null, { ...RENDER, transitionStyle: "fadeblack" }); assert.deepEqual(xfades(g), [ "[0:v][1:v]xfade=transition=fadeblack:duration=0.5:offset=9.500[v1]", "[v1][2:v]xfade=transition=fadeblack:duration=0.5:offset=21.000[v2]", ]); // The sound still crossfades as before. assert.ok(g.parts.includes("[p0a][p1a]acrossfade=d=0.5:c1=tri:c2=tri[a1]")); }); test("transitionStyle: a THEN/NOW wipe and a whip keep their own look", () => { const joins = [{ wipe: true }, { whip: true }, null]; const g = xfadeGraph([10, 12, 4], 0.5, joins, { ...RENDER, transitionStyle: "fadeblack" }); const x = xfades(g); assert.match(x[0], /transition=wipeleft/); assert.match(x[1], /transition=slideleft/); }); test("validateTransitionStyle: xfade's names only", () => { assert.deepEqual(validateTransitionStyle({ render: {} }), []); for (const s of TRANSITION_STYLES) assert.deepEqual(validateTransitionStyle({ render: { transitionStyle: s } }), []); assert.equal(validateTransitionStyle({ render: { transitionStyle: "spin" } }).length, 1); assert.equal(validateTransitionStyle({ render: { transitionStyle: 3 } }).length, 1); }); test("transitionStyle: blurslide is a quick eased push (one closed-form custom xfade) with smear and grain on the seam's frames", () => { const R = { ...RENDER, transition: 0.22, transitionStyle: "blurslide" }; const g = xfadeGraph([10, 12, 4], 0.22, null, R); const x = xfades(g); assert.equal(x.length, 2); assert.ok(x[0].startsWith("[p0e][p1e]xfade=transition=custom:expr='if(eq(PLANE,0),a0(abs(X-")); assert.ok(x[0].endsWith("':duration=0.22:offset=9.780[v1]")); assert.ok(x[0].includes(`abs(X-${SLIDE_DEFAULTS.travel}*W*(1-P*P*P))`)); assert.match(x[0], /a1\(abs\(X-/); // every plane read, not just luma // No registers: xfade's slice threads share them (see transition.mjs). assert.doesNotMatch(x[0], /\b(st|ld)\(/); // The smear in bands over the whole join, the grain on the half nearest the seam, each clip in its own time. const edge = (i) => g.parts.find((p) => p.endsWith(`[p${i}e]`)); assert.equal( edge(0), "[0:v]setpts=PTS-STARTPTS," + "dblur=angle=180:radius=13.4:enable='between(t,9.780,9.835)'," + "dblur=angle=180:radius=6.8:enable='between(t,9.835,9.890)'," + "dblur=angle=180:radius=2.5:enable='between(t,9.890,9.945)'," + "noise=alls=2:allf=t+u:enable='gte(t,9.890)'[p0e]", ); assert.match(edge(1), /radius=13.4:enable='between\(t,11.780,11.835\)\+between\(t,0.000,0.055\)'/); assert.match(edge(1), /enable='gte\(t,11.890\)\+lt\(t,0.110\)'/); assert.match(edge(2), /enable='lt\(t,0.110\)'/); assert.ok(g.parts.includes("[p0a][p1a]acrossfade=d=0.22:c1=tri:c2=tri[a1]")); // The knobs: no grain and no smear -> no edge filters; either alone -> only it. const g0 = xfadeGraph([10, 12], 0.22, null, { ...R, slide: { grain: 0, blur: 0 } }); assert.ok(!g0.parts.some((p) => p.includes("noise=") || p.includes("dblur="))); assert.doesNotMatch(blurSlideEdges(10, 0.22, { ...R, slide: { grain: 0 } }, { tail: true }), /noise=/); assert.doesNotMatch(blurSlideEdges(10, 0.22, { ...R, slide: { blur: 0 } }, { tail: true }), /dblur=/); assert.deepEqual(slideOf({ slide: { travel: 0.2 } }), { ...SLIDE_DEFAULTS, travel: 0.2 }); assert.equal(xfadeTransitionOf({ transitionStyle: "fadeblack" }, 0.5), "fadeblack"); }); test("transitionStyle: blurslide keeps a flat picture flat on ffmpeg's default threads (no register race)", () => { // Measured before the fix: white into white had tens of thousands of black // pixels on two seam frames -- slice threads sharing st()/ld() registers. const R = { ...RENDER, width: 640, height: 360, transition: 0.3, transitionStyle: "blurslide", slide: { travel: 0.2, blur: 1, grain: 0 } }; const g = xfadeGraph([1, 1], 0.3, null, R); const src = ["-f", "lavfi", "-i", "color=white:s=640x360:r=30:d=1,format=yuv420p"]; const tone = ["-f", "lavfi", "-i", "anullsrc=r=48000:cl=stereo:d=1"]; const graph = g.parts.map((p) => p.replace("[0:a]", "[2:a]").replace("[1:a]", "[3:a]")).join(";"); const raw = execFileSync("ffmpeg", ["-v", "error", ...src, ...src, ...tone, ...tone, "-filter_complex", `${graph};${g.vlab}format=gray[g]`, "-map", "[g]", "-f", "rawvideo", "pipe:1", "-map", g.alab, "-f", "null", "-"], { maxBuffer: 64 << 20 }); const n = 640 * 360; assert.equal(raw.length, n * Math.round(1.7 * 30)); let low = 0; for (let i = 0; i < raw.length; i += 1) if (raw[i] < 250) low += 1; assert.equal(low, 0); }); test("validateSlide: travel/blur/grain within bounds, nothing else", () => { assert.deepEqual(validateSlide({ render: {} }), []); assert.deepEqual(validateSlide({ render: { slide: { travel: 0.1, blur: 1, grain: 0 } } }), []); assert.equal(validateSlide({ render: { slide: { travel: 0.9 } } }).length, 1); assert.equal(validateSlide({ render: { slide: { speed: 2 } } }).length, 1); assert.equal(validateSlide({ render: { slide: 3 } }).length, 1); assert.equal(validateTransitionStyle({ render: { transitionStyle: "blurslide", slide: { grain: 99 } } }).length, 1); }); test("transitionStyle: blurslide leaves a dip, a wipe and a whip alone, and every other style untouched", () => { const R = { ...RENDER, transitionStyle: "blurslide" }; const g = xfadeGraph([10, 12, 4], 0.5, [{ wipe: true }, null, null], R); assert.match(xfades(g)[0], /transition=wipeleft/); assert.doesNotMatch(g.parts.find((p) => p.endsWith("[p0e]")) ?? "", /gte|dblur/); assert.match(g.parts.find((p) => p.endsWith("[p1e]")), /enable='gte\(t,11.750\)'/); assert.equal(blurSlideEdges(10, 0.5, { transitionStyle: "fadeblack" }, { tail: true }), ""); assert.equal(blurSlideEdges(10, 0, { transitionStyle: "blurslide" }, { tail: true }), ""); for (const s of ["fade", "fadeblack", "fadewhite", "dissolve"]) { assert.ok(!xfadeGraph([10, 12], 0.5, null, { ...RENDER, transitionStyle: s }).parts.some((p) => p.includes("noise=") || p.includes("dblur=") || p.includes("custom"))); } }); test("transitionStyle: blurslide renders in ffmpeg; frames outside the seam are untouched, inside they differ", () => { const run = (style) => { const R = { ...RENDER, width: 320, height: 180, transition: 0.4, transitionStyle: style }; const g = xfadeGraph([1.5, 1.5], 0.4, null, R); const src = (c) => ["-f", "lavfi", "-i", `testsrc2=s=320x180:r=30:d=1.5,hue=h=${c}`]; const tone = ["-f", "lavfi", "-i", "sine=f=440:d=1.5:sample_rate=48000"]; const args = ["-v", "error", ...src(0), ...src(120), ...tone, ...tone, "-filter_complex", [...g.parts.map((p) => p.replace("[0:a]", "[2:a]").replace("[1:a]", "[3:a]"))].join(";"), "-map", g.vlab, "-map", g.alab, "-f", "framemd5", "-"]; return execFileSync("ffmpeg", args, { encoding: "utf8" }).split("\n").filter((l) => /^0,/.test(l)).map((l) => l.split(",").pop().trim()); }; const plain = run("fade"); const blur = run("blurslide"); assert.equal(blur.length, Math.round((1.5 + 1.5 - 0.4) * 30)); // Frame 10 (0.33 s) is before the window and frame 70 after it: untouched by the blur, same as any style. const md5 = (s) => createHash("md5").update(s).digest("hex"); assert.equal(md5(blur[10]), md5(plain[10])); assert.equal(md5(blur[70]), md5(plain[70])); // Frame 40 (1.33 s) is mid-seam: blurred, grained and slid, not a dissolve. assert.notEqual(blur[40], plain[40]); });