Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit 2c4152940a0d1ae82cbe384c71a63afcf410f82e
parent c63260c093e0f5a894dc167c37f6574ef5d7e3ca
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Mon, 26 Oct 2026 12:21:31 -0400

umtool: a crossfade batch keys by its segments' names, so a take started --from another reuses its batches

The batch key named each input segment by its full path, so two takes'
hard links of the same segments never shared a batch. It now names them
by file name, size and mtime. A batch made under the old key -- by this
take, or by the --from take of the very same inodes (xfade-batches/
.from.json names its segments) -- is adopted under the new name.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Diffstat:
Mumtool/docs/report-video.md | 5++++-
Mumtool/lib/report/takeFrom.mjs | 30++++++++++++++++++++++++------
Mumtool/lib/report/takeFrom.test.mjs | 11++++++++++-
Mumtool/report-to-video/build-video.mjs | 71++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Mumtool/report-to-video/xfade-batch.test.mjs | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++--
5 files changed, 154 insertions(+), 19 deletions(-)

diff --git a/umtool/docs/report-video.md b/umtool/docs/report-video.md @@ -906,7 +906,10 @@ hard-links `takes/final-blurred/out/sourced/`'s clip segments (the files `SEGMEN names), each keyed chrome render (`chrome/<layer>-frames/` with its `.key`; a layer kept aside in `takes/<other>/out/.keep-frames/<layer>-frames` is linked in as `chrome/<layer>-frames`) and its finished crossfade batches into this take's `out/sourced/`, then renders as usual: whatever still -matches is reused, the rest is rebuilt. Links, never copies or symlinks, and every one of these +matches is reused, the rest is rebuilt. A batch keys by its segments' file names, sizes and mtimes +(no longer their full paths), so the same batch keys alike in both takes; a batch the other take +made under the old path key is adopted when this take's segments are those very files (the same +inodes; `xfade-batches/.from.json` names the other take's segments for it). Links, never copies or symlinks, and every one of these files the build replaces rather than writes, so the other take's files never change. `--from` refuses a take that does not exist, this take itself, or a take with no `out/sourced/`, before anything is written. A chrome layer reuses its frames only when they are on disk: a build prunes diff --git a/umtool/lib/report/takeFrom.mjs b/umtool/lib/report/takeFrom.mjs @@ -12,7 +12,9 @@ // key (the segments' size and mtime) is // out/.keep-frames/<layer>-frames a layer kept aside from a prune (the // other take's chrome/ lost it after its -// mux), linked in as chrome/<layer>-frames +// mux), linked in as chrome/<layer>-frames; +// when both are there, the one whose .key +// was written last // // Links, never copies, and never a symlink: every one of these the build // REPLACES rather than writes -- a segment through a rename @@ -24,9 +26,9 @@ // // Plain node; the CLI runs it with bare node. -import { link, lstat, mkdir, readdir, rename, rm } from "node:fs/promises"; +import { link, lstat, mkdir, readdir, rename, rm, writeFile } from "node:fs/promises"; import path from "node:path"; -import { DEFAULT_VARIANT, SEGMENT_FILE_SUFFIXES } from "umtool-report-to-video/build-video"; +import { DEFAULT_VARIANT, SEGMENT_FILE_SUFFIXES, XFADE_FROM_FILE } from "umtool-report-to-video/build-video"; import { takesDirOf } from "./takes.mjs"; /** Where `--from` keeps a layer aside: the other take's `out/.keep-frames/<layer>-frames`. */ @@ -97,11 +99,19 @@ export async function linkFromTake(projectDir, id, from, takeOut) { // The keyed renders: chrome/<layer>-frames, with its .key, else the one kept aside. const chrome = path.join(src, "chrome"); const kept = path.join(path.dirname(src), KEEP_FRAMES_DIR); + // Both there: the one whose .key was written last (the other is an older cut's). const layers = new Map(); - for (const f of await names(kept)) if (f.endsWith("-frames") && (await isFile(path.join(kept, f, ".key")))) layers.set(f, path.join(kept, f)); - for (const f of await names(chrome)) if (f.endsWith("-frames") && (await isFile(path.join(chrome, f, ".key")))) layers.set(f, path.join(chrome, f)); + for (const dir of [chrome, kept]) { + for (const f of await names(dir)) { + if (!f.endsWith("-frames")) continue; + const key = await lstat(path.join(dir, f, ".key")).catch(() => null); + if (!key?.isFile()) continue; + const had = layers.get(f); + if (!had || key.mtimeMs > had.at) layers.set(f, { dir: path.join(dir, f), at: key.mtimeMs }); + } + } if (layers.size) await mkdir(path.join(dst, "chrome"), { recursive: true }); - for (const [name, dir] of [...layers].sort()) { + for (const [name, { dir }] of [...layers].sort((x, y) => x[0].localeCompare(y[0]))) { await linkFrames(dir, path.join(dst, "chrome", name)); out.frames.push(name); } @@ -113,5 +123,13 @@ export async function linkFromTake(projectDir, id, from, takeOut) { await mkdir(batchOut, { recursive: true }); if (await linkFile(path.join(batches, f), path.join(batchOut, f))) out.batches += 1; } + // A batch the other take keyed by its own segments' paths (before keys + // named files) is found through this: build-video adopts it when this + // take's segments are those very files (xfadeBatchKeyV1). + if (await isDir(batchOut)) { + const tmp = path.join(batchOut, `${XFADE_FROM_FILE}.${process.pid}.tmp`); + await writeFile(tmp, JSON.stringify({ take: from, segments: path.resolve(segs) }) + "\n", "utf8"); + await rename(tmp, path.join(batchOut, XFADE_FROM_FILE)); + } return out; } diff --git a/umtool/lib/report/takeFrom.test.mjs b/umtool/lib/report/takeFrom.test.mjs @@ -6,7 +6,7 @@ // Run with: node --test umtool/lib/report/takeFrom.test.mjs import assert from "node:assert/strict"; import { existsSync, statSync } from "node:fs"; -import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { mkdir, mkdtemp, readFile, rm, utimes, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; @@ -74,14 +74,23 @@ test("linkFromTake: segments, keyed chrome frames (kept-aside ones too) and fini assert.ok(sameFile(path.join(p.src, "chrome", "deck-frames", ".key"), path.join(d, "chrome", "deck-frames", ".key"))); assert.equal(await readFile(path.join(d, "chrome", "stamp-frames", ".key"), "utf8"), "k2\n"); assert.ok(!existsSync(path.join(d, "chrome", "deck")), "a composition is not linked"); + // A layer in both places: the newer .key wins (chrome/ holds an older cut's here). + const stale = path.join(p.src, "chrome", "stamp-frames"); + await mkdir(stale, { recursive: true }); + await writeFile(path.join(stale, ".key"), "old\n"); + await utimes(path.join(stale, ".key"), new Date(0), new Date(0)); + await linkFromTake(p.dir, "twin", "blurred", out); + assert.equal(await readFile(path.join(d, "chrome", "stamp-frames", ".key"), "utf8"), "k2\n"); assert.ok(!existsSync(path.join(d, "chrome", "feed-frames-nokey")), "a sequence with no key is not linked"); assert.equal(got.batches, 1); assert.ok(sameFile(path.join(p.src, "xfade-batches", "batch-0123abcd.mov"), path.join(d, "xfade-batches", "batch-0123abcd.mov"))); assert.ok(!existsSync(path.join(d, "xfade-batches", "batch-0123abcd.partial.mov"))); + assert.deepEqual(JSON.parse(await readFile(path.join(d, "xfade-batches", ".from.json"), "utf8")), { take: "blurred", segments: path.join(p.src, "segments") }); assert.equal(await readFile(path.join(p.src, "segments", "s02.mp4"), "utf8"), "blurred s02.mp4", "the other take's file is as it was"); // Again: nothing new to link, nothing broken. const again = await linkFromTake(p.dir, "twin", "blurred", out); assert.equal(again.segments, 0); + assert.deepEqual(again.frames, ["deck-frames", "stamp-frames"]); assert.equal(again.batches, 0); } finally { await rm(p.root, { recursive: true, force: true }); diff --git a/umtool/report-to-video/build-video.mjs b/umtool/report-to-video/build-video.mjs @@ -4354,6 +4354,39 @@ export function xfadeBatchArgs({ segments, durs, batch, render, joins = null, ou ]; } +/** In a take's `xfade-batches/`: the segments dir of the take it was started from (`umtool take --from`). */ +export const XFADE_FROM_FILE = ".from.json"; + +/** + * A batch's key: its argv (graph, joins, encode) with each input segment named + * by its file name, and each input's size and mtime -- so the same batch of + * the same segment files keys alike in two takes (a hard link has its file's + * size and mtime). + */ +export function xfadeBatchKey(argv, ins, sts) { + const name = new Map(ins.map((s) => [s, `<seg:${path.basename(s)}>`])); + return createHash("sha256") + .update(JSON.stringify({ + v: 2, + argv: argv.map((a) => name.get(a) ?? a), + stats: ins.map((s, k) => [path.basename(s), sts[k].size, sts[k].mtimeMs]), + })) + .digest("hex") + .slice(0, 16); +} + +/** The first key (full paths; `names` stands for `ins`), for finding a batch made before xfadeBatchKey. */ +export function xfadeBatchKeyV1(argv, ins, names, sts) { + const as = new Map(ins.map((s, k) => [s, path.resolve(names[k])])); + return createHash("sha256") + .update(JSON.stringify({ + argv: argv.map((a) => as.get(a) ?? a), + stats: ins.map((_, k) => [path.resolve(names[k]), sts[k].size, sts[k].mtimeMs]), + })) + .digest("hex") + .slice(0, 16); +} + /** * Crossfade each batch into `dir/batch-<key>.mov`, reusing a file whose key * matches: the key is the batch's whole argv (graph, joins, encode) and each @@ -4368,19 +4401,39 @@ export async function buildXfadeBatches({ segments, durs, batches, render, joins const D = render.transition ?? 0.5; const lens = xfadeBatchLengths(durs, D, batches); await mkdir(dir, { recursive: true }); + // A take started `--from` another (takeFrom.mjs) names that take's + // segments dir here, so a batch it made under the first key (below) is found. + const from = await readFile(path.join(dir, XFADE_FROM_FILE), "utf8").then((t) => JSON.parse(t)?.segments ?? null, () => null); const planned = []; for (const batch of batches) { const argv = xfadeBatchArgs({ segments, durs, batch, render, joins, outPath: "" }).slice(0, -1); const ins = segments.slice(batch.from, batch.to); - const stats = await Promise.all(ins.map(async (s) => { - const st = await stat(s); - return [path.resolve(s), st.size, st.mtimeMs]; - })); - const key = createHash("sha256") - .update(JSON.stringify({ argv: argv.map((a) => (ins.includes(a) ? path.resolve(a) : a)), stats })) - .digest("hex") - .slice(0, 16); - planned.push(path.join(dir, `batch-${key}.mov`)); + const sts = await Promise.all(ins.map((s) => stat(s))); + const key = xfadeBatchKey(argv, ins, sts); + const file = path.join(dir, `batch-${key}.mov`); + planned.push(file); + if (await exists(file)) continue; + // The first key named each input by its full path, so no other take's + // batch could match it. A batch made under it -- by this take, or by the + // take it was started from, of the very same files (the same inodes) -- + // is the same batch: adopted under the key of today. + const legacy = [ins]; + if (from) { + const theirs = ins.map((s) => path.join(from, path.basename(s))); + const same = await Promise.all(theirs.map(async (t, k) => { + const st = await stat(t).catch(() => null); + return st && st.ino === sts[k].ino && st.dev === sts[k].dev; + })); + if (same.every(Boolean)) legacy.push(theirs); + } + for (const names of legacy) { + const old = path.join(dir, `batch-${xfadeBatchKeyV1(argv, ins, names, sts)}.mov`); + if (old !== file && (await exists(old))) { + await rename(old, file); + EMIT("note", { message: `crossfade batch ${path.basename(old)} is this cut's ${path.basename(file)} (the same segments) — reused` }); + break; + } + } } const keep = new Set(planned.map((f) => path.basename(f))); for (const name of await readdir(dir)) { diff --git a/umtool/report-to-video/xfade-batch.test.mjs b/umtool/report-to-video/xfade-batch.test.mjs @@ -9,13 +9,14 @@ // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; -import { mkdtempSync, readdirSync, rmSync, statSync } from "node:fs"; +import { linkSync, mkdirSync, mkdtempSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { - XFADE_BATCH_DEFAULT, buildXfadeBatches, concatWithXfade, xfadeBatchArgs, xfadeBatchJoins, + XFADE_BATCH_DEFAULT, XFADE_FROM_FILE, buildXfadeBatches, concatWithXfade, xfadeBatchArgs, xfadeBatchJoins, + xfadeBatchKey, xfadeBatchKeyV1, xfadeBatchLengths, xfadeBatchSize, xfadeBatches, xfadeConcatArgs, } from "./build-video.mjs"; import { scheduleFrom } from "./deck.mjs"; @@ -223,3 +224,54 @@ test("seven segments in batches of three: the same frames as one pass", { skip: rmSync(dir, { recursive: true, force: true }); } }); + +test("a batch keys by its segments' names, sizes and mtimes: two takes' hard links key alike; a batch under the first key is adopted", { skip: !have && "no ffmpeg" }, async () => { + const root = mkdtempSync(path.join(tmpdir(), "xfade-batch-from-")); + try { + const a = path.join(root, "a"); + const b = path.join(root, "b"); + mkdirSync(a, { recursive: true }); + mkdirSync(b, { recursive: true }); + const segsA = [0, 1, 2, 3].map((i) => segment(a, i)); + const segsB = segsA.map((s) => { + const t = path.join(b, path.basename(s)); + linkSync(s, t); + return t; + }); + const durs = segsA.map(() => 2); + const render = { ...RENDER, xfadeBatch: 2 }; + const plan = xfadeBatches(4, 2); + const argvOf = (segments, batch) => xfadeBatchArgs({ segments, durs, batch, render, outPath: "" }).slice(0, -1); + const sts = segsA.slice(0, 2).map((s) => statSync(s)); + assert.equal(xfadeBatchKey(argvOf(segsA, plan[0]), segsA.slice(0, 2), sts), xfadeBatchKey(argvOf(segsB, plan[0]), segsB.slice(0, 2), sts)); + assert.notEqual( + xfadeBatchKeyV1(argvOf(segsA, plan[0]), segsA.slice(0, 2), segsA.slice(0, 2), sts), + xfadeBatchKeyV1(argvOf(segsB, plan[0]), segsB.slice(0, 2), segsB.slice(0, 2), sts), + "the first key named the paths", + ); + + // Take A's batches, renamed to the first key's names (as a build before this left them). + const dirA = path.join(a, "xfade-batches"); + await buildXfadeBatches({ segments: segsA, durs, batches: plan, render, dir: dirA }); + for (const batch of plan) { + const ins = segsA.slice(batch.from, batch.to); + const st = ins.map((s) => statSync(s)); + renameSync(path.join(dirA, `batch-${xfadeBatchKey(argvOf(segsA, batch), ins, st)}.mov`), path.join(dirA, `batch-${xfadeBatchKeyV1(argvOf(segsA, batch), ins, ins, st)}.mov`)); + } + const inodes = readdirSync(dirA).map((f) => statSync(path.join(dirA, f)).ino).sort(); + // Take B, started from A: its batches linked in, and A's segments dir named. + const dirB = path.join(b, "xfade-batches"); + mkdirSync(dirB); + for (const f of readdirSync(dirA)) linkSync(path.join(dirA, f), path.join(dirB, f)); + writeFileSync(path.join(dirB, XFADE_FROM_FILE), JSON.stringify({ segments: a })); + const r = await buildXfadeBatches({ segments: segsB, durs, batches: plan, render, dir: dirB }); + assert.deepEqual(r.files.map((f) => statSync(f).ino).sort(), inodes, "every batch adopted, none encoded"); + assert.equal(readdirSync(dirB).length, 2, "the from record is gone with the old names"); + + // The same in A itself: its own batches under the first key are adopted too. + const again = await buildXfadeBatches({ segments: segsA, durs, batches: plan, render, dir: dirA }); + assert.deepEqual(again.files.map((f) => statSync(f).ino).sort(), inodes); + } finally { + rmSync(root, { recursive: true, force: true }); + } +});