commit 0041242cf6f057bc309342b73dd24a4eb8abcb96
parent e6134f32398b9fbcd186cdfffe63e5627e80f9a5
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Tue, 27 Oct 2026 00:06:53 -0400
chunked encode: two spans that key alike are encoded once (no two jobs on one file)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
2 files changed, 33 insertions(+), 2 deletions(-)
diff --git a/umtool/report-to-video/build-video.mjs b/umtool/report-to-video/build-video.mjs
@@ -4835,7 +4835,10 @@ async function chunkedXfadeConcat({ segments, render, outPath, chrome = null, jo
};
let done = 0;
- const jobs = todo.map((p) => async () => {
+ // Two spans that key alike are one file: encoded once.
+ const byKey = new Map();
+ for (const p of todo) if (!byKey.has(p.rec.key)) byKey.set(p.rec.key, p);
+ const jobs = [...byKey.values()].map((p) => async () => {
const tc = Date.now();
const tmp = p.file.replace(/\.mp4$/, ".partial.mp4");
await rm(tmp, { force: true });
@@ -4848,7 +4851,7 @@ async function chunkedXfadeConcat({ segments, render, outPath, chrome = null, jo
await rename(tmp, p.file);
done += 1;
EMIT("note", {
- message: `final encode: chunk ${done}/${todo.length} (${(p.span.a / fps).toFixed(1)}–${(p.span.b / fps).toFixed(1)}s) ` +
+ message: `final encode: chunk ${done}/${byKey.size} (${(p.span.a / fps).toFixed(1)}–${(p.span.b / fps).toFixed(1)}s) ` +
`in ${((Date.now() - tc) / 1000).toFixed(1)}s`,
});
});
diff --git a/umtool/report-to-video/chunked-encode.test.mjs b/umtool/report-to-video/chunked-encode.test.mjs
@@ -307,3 +307,31 @@ test("at 30 fps the chunks hold exactly the cut's frames (offsets to the microse
rmSync(dir, { recursive: true, force: true });
}
});
+
+test("two spans that key alike are encoded once and joined twice", { skip: !have }, async () => {
+ const fps = 10;
+ const render = { width: 64, height: 36, fps, transition: 0.5, audioRate: 48000, audioChannels: 2, preset: "ultrafast", crf: 0 };
+ const dir = mkdtempSync(path.join(tmpdir(), "chunked-dup-"));
+ const events = [];
+ setProgressMode((e) => events.push(e));
+ try {
+ // The same segment four times: the middle spans are the same pixels.
+ const one = segment(dir, 1, 30, fps, "same");
+ const segments = [0, 1, 2, 3].map((k) => {
+ const f = path.join(dir, `s${k}.mp4`);
+ writeFileSync(f, readFileSync(one));
+ return f;
+ });
+ const out = path.join(dir, "cut.mp4");
+ await concatWithXfade(segments, render, out, null, null, null, { workDir: dir });
+ const rec = JSON.parse(readFileSync(path.join(dir, CHUNKS_DIR, CHUNKS_RECORD), "utf8"));
+ const keys = rec.chunks.map((c) => c.key);
+ assert.ok(new Set(keys).size < keys.length, `some spans key alike (${keys.join(" ")})`);
+ const encoded = events.filter((e) => /^final encode: chunk \d+\/\d+/.test(e.message ?? "")).length;
+ assert.equal(encoded, new Set(keys).size, "each key encoded once");
+ assert.equal(probeFrames(out), Math.round(scheduleFrom([3, 3, 3, 3], 0.5).total * fps));
+ } finally {
+ setProgressMode("human");
+ rmSync(dir, { recursive: true, force: true });
+ }
+});