Archilyzer · Source

archilyzer

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

commit 0e791449b0417f1e305eb5282decc70e7b140c08
parent 77b7a4eb4dde4e4708a2ae3cf4a75630b9f96d16
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Wed,  4 Nov 2026 14:23:21 -0500

umtool: a hard-cut cut's sound keys by each segment's audio packets, not its file -- a post whose picture changed and whose words did not keeps the cut's sound (93 s on mid-6)

soundIdentity: ffmpeg's framehash of the stream-copied audio (packets and
their timestamps, ~0.1 s a segment), memoised by inode in the content-hash
memo (HashMemo.derived); one memo per work dir per process, so the early
sound and the chunked encode share it.

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

Diffstat:
Mumtool/docs/report-video.md | 3++-
Mumtool/report-to-video/README.md | 5+++--
Mumtool/report-to-video/build-video.mjs | 43+++++++++++++++++++++++++++++++++++++------
Mumtool/report-to-video/chunked-encode.mjs | 22++++++++++++++++++++++
Mumtool/report-to-video/chunked-encode.test.mjs | 12++++++++++++
5 files changed, 76 insertions(+), 9 deletions(-)

diff --git a/umtool/docs/report-video.md b/umtool/docs/report-video.md @@ -1692,7 +1692,8 @@ changed. A post that grew or shrank moves every later chunk without changing it after it are the same frames, later -- so `--from` the take before re-encodes only the touched post's chunks. The deck's frames themselves are keyed by what they draw, so a twin of a pruned take captures them again (about 90 s for a 31-minute cut) and still reuses every chunk. The cut's sound -is one pass (about 90 s, begun beside the capture), reused when no segment changed. +is one pass (about 90 s, begun beside the capture), reused when no segment's sound changed (a +caption or a knob that moves no word keeps it). A layer whose page changed reuses its frames FRAME BY FRAME (report-to-video `frame-keys.mjs`, each module's `frameKeys`: the deck, the stamps, the feed, the finale, the thread rail, the flips, the diff --git a/umtool/report-to-video/README.md b/umtool/report-to-video/README.md @@ -1966,8 +1966,9 @@ parsing — wrapping the expression in single quotes is what protects them. segment at the longer of its picture and its sound (0.30 s, 9 frames, by the end of mid-6, its last 9 frames cut off); the sound is pinned the same way (`hardCutSoundArgs`: each segment's sound as PCM exactly as long as its - picture, end to end, encoded once -- begun beside the deck's capture, reused - when no segment changed). + picture, end to end, encoded once -- begun beside the deck's capture, and + keyed by each segment's SOUND, its audio packets: a fix to a post's picture + alone keeps the cut's sound). - **A chunk's chrome layers key by their frame keys** when a sequence carries `.frame-keys` (every keyed layer does): what its frames DRAW, not their bytes. A layer captured again after a prune draws "a hair differently" per renderer diff --git a/umtool/report-to-video/build-video.mjs b/umtool/report-to-video/build-video.mjs @@ -5027,6 +5027,36 @@ export function hardCutSoundArgs({ durs, joins = null, render }) { /** The hard-cut sound passes in flight, by their file: the early start and the encode share one. */ const SOUND_IN_FLIGHT = new Map(); +/** The content-hash memo of a work dir, one per process (the early sound and the encode share it). */ +const MEMOS = new Map(); +function memoAt(workDir) { + const file = path.join(workDir, HASH_MEMO_FILE); + if (!MEMOS.has(file)) MEMOS.set(file, HashMemo.open(file)); + return MEMOS.get(file); +} + +/** + * A segment's SOUND, as an identity: the sha1 of its audio packets with + * their timestamps (ffmpeg's framehash, stream-copied: no decode, ~0.1 s). + * A post whose picture changed and whose words did not (a caption, a knob) + * has the same sound, and the cut's sound is not encoded again for it. + */ +export async function soundIdentity(file) { + const { stdout } = await execFileP(FFMPEG, ["-nostdin", "-v", "error", "-i", file, "-map", "0:a:0", "-c", "copy", "-f", "framehash", "-hash", "sha256", "-"], { maxBuffer: 1 << 28 }); + const body = String(stdout).split("\n").filter((l) => !l.startsWith("#software")).join("\n"); + return createHash("sha1").update(body).digest("hex"); +} + +/** Each segment's sound identity (soundIdentity), memoised by inode in `memo`, 8 at a time. */ +async function soundIdentities(memo, segments) { + const out = new Array(segments.length); + await runPool(segments.map((s, i) => async () => { + out[i] = await memo.derived(s, "sound", soundIdentity); + if (!out[i]) throw new Error(`the cut's sound: segment ${s} is not a file`); + }), 8); + return out; +} + /** * Make (or find) a hard-cut cut's sound: `<dir>/audio-<key>.m4a`, keyed by * the segments' contents and every argv that makes it (hardCutSoundArgs). @@ -5034,17 +5064,18 @@ const SOUND_IN_FLIGHT = new Map(); * the deck's capture -- and awaited by the chunked encode, which calls it * again with the same arguments (one pass, shared). Resolves to the file. * - * @param {{ segments: string[], durs: number[], joins?: Array<object|null>|null, render: object, dir: string, workDir: string, hashes?: string[]|null, ffmpeg?: string|null }} job + * @param {{ segments: string[], durs: number[], joins?: Array<object|null>|null, render: object, dir: string, workDir: string, ffmpeg?: string|null }} job */ export function hardCutSound(job) { const run = (async () => { const { segments, durs, joins = null, render, dir, workDir } = job; - const hashes = job.hashes ?? (await new HashMemo(null).hashAll(segments)); + // Keyed by each segment's SOUND, not its file: a fix to a post's picture + // alone leaves the cut's sound as it was. + const sounds = await soundIdentities(await memoAt(workDir), segments); const ffmpeg = job.ffmpeg ?? (await ffmpegIdentity(FFMPEG)); const a = hardCutSoundArgs({ durs, joins, render }); const keyArgv = [...segments.map((_, i) => a.pin(i, `<in:${i}>`, "<pin>")), a.encode("<list>", "<out>")]; - const audio = path.join(dir, `audio-${chunkKey({ keyArgv: keyArgv.flat(), inputs: hashes.map((h) => ({ seg: h })), ffmpeg })}.m4a`); - return audio; + return path.join(dir, `audio-${chunkKey({ keyArgv: keyArgv.flat(), inputs: sounds.map((h) => ({ sound: h })), ffmpeg })}.m4a`); })(); return run.then((audio) => { if (!SOUND_IN_FLIGHT.has(audio)) { @@ -5182,7 +5213,7 @@ export async function chunkedConcat({ segments, render, outPath, chrome = null, }); const dir = path.join(workDir, CHUNKS_DIR); await mkdir(dir, { recursive: true }); - const memo = await HashMemo.open(path.join(workDir, HASH_MEMO_FILE)); + const memo = await memoAt(workDir); const ffmpeg = await ffmpegIdentity(FFMPEG); const prev = await readFile(path.join(dir, CHUNKS_RECORD), "utf8").then((t) => JSON.parse(t), () => null); const prevChunks = Array.isArray(prev?.chunks) ? prev.chunks : []; @@ -5258,7 +5289,7 @@ export async function chunkedConcat({ segments, render, outPath, chrome = null, const audioProto = D === 0 ? null : cutAudioArgs({ segments: segments.map((_, i) => `<in:${i}>`), durs, D, render, joins, outPath: "" }).slice(0, -1); let audio = D === 0 ? null : path.join(dir, `audio-${chunkKey({ keyArgv: audioProto, inputs: segHash.map((h) => ({ seg: h })), ffmpeg })}.m4a`); const audioJob = D === 0 ? async () => { - audio = (await hardCutSound({ segments, durs, joins, render, dir, workDir, hashes: segHash, ffmpeg })).audio; + audio = (await hardCutSound({ segments, durs, joins, render, dir, workDir, ffmpeg })).audio; } : async () => { if (await exists(audio)) { EMIT("note", { message: "final encode: the cut's sound unchanged — reused" }); diff --git a/umtool/report-to-video/chunked-encode.mjs b/umtool/report-to-video/chunked-encode.mjs @@ -184,6 +184,28 @@ export class HashMemo { return h; } + /** + * Something computed from a file's contents (`compute(file)`, a string), + * memoised beside its hash by the same identity under `tag` -- a segment's + * SOUND, say, which a build keys apart from its picture. null when it is + * not a regular file. + */ + async derived(file, tag, compute) { + const st = await stat(file).catch(() => null); + if (!st?.isFile()) return null; + const id = `${tag}:${st.dev}:${st.ino}:${st.size}:${st.mtimeMs}`; + this.seen.add(id); + const had = this.map.get(id); + if (had) return had; + const h = await compute(file); + const after = await stat(file).catch(() => null); + if (after && `${tag}:${after.dev}:${after.ino}:${after.size}:${after.mtimeMs}` === id) { + this.map.set(id, h); + this.dirty = true; + } + return h; + } + /** Hash many files, `width` at a time; the hashes in order. */ async hashAll(files, width = 8) { const out = new Array(files.length); diff --git a/umtool/report-to-video/chunked-encode.test.mjs b/umtool/report-to-video/chunked-encode.test.mjs @@ -421,6 +421,18 @@ test("a chunked hard-cut deck build is the hard-cut concat and its overlay pass, await chunkedConcat({ segments, render: R10, outPath: chunked, chrome, workDir: dir, hardCut: true }); assert.ok(events.some((e) => /0 to encode/.test(e.message ?? "")), "nothing encoded the second time"); assert.ok(events.some((e) => /sound unchanged/.test(e.message ?? ""))); + + // Segment 2's picture made again, its sound the same packets (a caption + // fixed): its chunks are encoded, the cut's sound is not. + const other = longSoundSegment(dir, 5, 33, fps, 0.15); + const remade = path.join(dir, "remade.mp4"); + run(["-v", "error", "-y", "-i", other, "-i", segments[2], "-map", "0:v", "-map", "1:a", "-c", "copy", remade]); + rmSync(segments[2]); + linkSync(remade, segments[2]); + events.length = 0; + await chunkedConcat({ segments, render: R10, outPath: chunked, chrome, workDir: dir, hardCut: true }); + assert.ok(events.some((e) => /5\/7 chunk\(s\) unchanged/.test(e.message ?? "")), events.map((e) => e.message).join("\n")); + assert.ok(events.some((e) => /sound unchanged/.test(e.message ?? "")), "a new picture over the same sound keeps the cut's sound"); } finally { setProgressMode("human"); delete process.env.REPORT_VIDEO_CHUNK_FRAMES;