commit 6fdc691ff50d735e29f5aed0e9e5d5cf1c52e2a8
parent 94fb2437eb6fdf1a00e092e0239b54041bdfbe4a
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Mon, 26 Oct 2026 15:56:23 -0400
umtool trailer: read each run through a movie source in the graph, so a run is decoded when the chain reaches it
Separate -i inputs decode ahead of the crossfade chain: on the Huck trailer
(14 runs) the frames waiting in the graph peaked at 5.6 GB decode-only and
4.75 GB with the encode. A movie source is pulled by the filter after it, so
the same trailer peaks at 3.4 GB with the x264 slow encode, and its frames are
identical (PSNR inf against the -i build). Trimmed by the master's own
timestamps, half a frame either side of the frame boundaries. The declared
estimate is 4000 MB.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
2 files changed, 79 insertions(+), 64 deletions(-)
diff --git a/umtool/report-to-video/trailer.mjs b/umtool/report-to-video/trailer.mjs
@@ -32,13 +32,15 @@
// start D − x into its segment and the master's blend is under the trailer's.
// Pulled-in edges are said, never silent (`notes`).
//
-// The spans are cut frame-accurately (decoded, never stream-copied): each is
-// one input of the master, seeked half a frame before a frame boundary a
-// second ahead and trimmed BY FRAME COUNT (the picture) and by time (the
-// sound, sample-exact), so no float rounding can drop or add a frame. They
-// are joined with the master's own encode settings (build-video's encodeArgs
-// over the take's `render`): crossfaded (xfade + acrossfade, the sound pinned
-// to the picture's length as build-video's join pins it), or hard-cut through
+// The spans are cut frame-accurately (decoded, never stream-copied) in ONE
+// ffmpeg pass: each run of the master is a `movie` source inside the
+// filtergraph, decoded only when the chain reaches it (separate `-i` inputs
+// decode ahead and the waiting frames took 5.6 GB), trimmed by the master's
+// own timestamps half a frame either side of its frame boundaries (the
+// picture) and at its first frame (the sound, sample-exact). They are joined
+// with the master's own encode settings (build-video's encodeArgs over the
+// take's `render`): crossfaded (xfade + acrossfade, the sound pinned to the
+// picture's length as build-video's join pins it), or hard-cut through
// `concat` when the crossfade is 0.
//
// The run takes the machine's heavy slot (heavy-gate.mjs) with a declared
@@ -59,13 +61,17 @@ const FFPROBE = () => process.env.FFPROBE_BIN ?? "ffprobe";
/** The trailer's crossfade, in seconds, when none is given. */
export const TRAILER_XFADE_DEFAULT = 0.3;
-/** The memory the one ffmpeg pass is declared to need in the heavy slot (MB). */
-export const TRAILER_ESTIMATE_MB = 2000;
+/**
+ * The memory the one ffmpeg pass is declared to need in the heavy slot (MB):
+ * measured 3.4 GB peak on the Huck trailer (14 runs of a 1080p master, x264
+ * `slow`), with room above it.
+ */
+export const TRAILER_ESTIMATE_MB = 4000;
-/** Each input's decoder threads: every span is open at once, so each is kept small. */
+/** Each run's decoder threads: every source is open at once, so each is kept small. */
export const TRAILER_DECODE_THREADS = 2;
-/** How far before a span its input is seeked: the decoder settles there, the trim cuts on the frame. */
+/** How far before a run its source seeks (to the keyframe at or before it): the trim cuts on the frame. */
const PRE_ROLL = 1;
/** A source second this close to a cut's edge is on it (the records keep three decimals). */
@@ -305,16 +311,6 @@ function pieceName(p, i) {
}
/**
- * The ffmpeg argv for a plan: one input per piece (the master, seeked),
- * one filtergraph, the master's encode settings.
- *
- * Each input is seeked to half a frame before the frame PRE_ROLL seconds
- * ahead of its piece (or not at all, for a piece that starts inside the
- * master's first second), so the first decoded frame is that frame exactly;
- * `trim=start_frame` then skips the pre-roll by count. Both streams' clocks
- * start at the seek point, so the sound is trimmed by the same second.
- */
-/**
* The runs of the master a plan reads: its pieces, a seamless one folded into
* the run before it (its frames follow on). `{ start, count }` in frames.
*/
@@ -328,59 +324,74 @@ export function trailerRuns(plan) {
return runs;
}
+/**
+ * A file name as a `movie` filter's option inside a filtergraph: escaped for
+ * the option (`\ ' :`), then for the graph (`\ ' [ ] , ;`), as ffmpeg's
+ * filtergraph docs say.
+ */
+export function filterPath(file) {
+ const opt = String(file).replace(/[\\':]/g, (c) => `\\${c}`);
+ return opt.replace(/[\\'[\],;]/g, (c) => `\\${c}`);
+}
+
+/**
+ * The ffmpeg argv for a plan: the whole cut in one filtergraph, one `movie`
+ * source of the master per run, the master's encode settings.
+ *
+ * WHY `movie` AND NOT ONE `-i` PER RUN. ffmpeg reads its `-i` inputs on their
+ * own threads, as fast as they decode, and the crossfade chain does not need
+ * the eleventh run for a minute and a half: measured on the Huck trailer
+ * (14 runs), the frames waiting in the graph took 5.6 GB. A `movie` source
+ * inside the graph is decoded only when the filter after it asks for a frame,
+ * so a run is read when the chain reaches it and memory stays flat.
+ *
+ * FRAME-ACCURATE BY TIMESTAMP. `movie` seeks to the keyframe at or before
+ * PRE_ROLL seconds ahead of the run and keeps the master's own timestamps,
+ * so the picture is trimmed half a frame either side of its frame boundaries
+ * (exactly the run's frames, whatever the float rounding) and the sound at
+ * the run's first frame, sample-exact; then both are pinned to the run's
+ * length, as build-video's join pins each segment's.
+ */
export function trailerArgs({ plan, master, render, outPath, threads = TRAILER_DECODE_THREADS }) {
const { fps, xfade: x } = plan;
- const pieces = trailerRuns(plan).map((r) => ({ frames: r }));
+ const runs = trailerRuns(plan);
const half = 0.5 / fps;
- const preFrames = Math.round(PRE_ROLL * fps);
- const inputs = [];
+ const src = filterPath(master);
const parts = [];
- pieces.forEach((p, i) => {
- const fa = p.frames.start;
- const n = p.frames.count;
+ runs.forEach((r, i) => {
+ const a = r.start / fps;
+ const n = r.count;
const d = (n / fps).toFixed(6);
- let skip;
- let audioFrom;
- if (fa >= preFrames + 1) {
- skip = preFrames;
- const ss = (fa - preFrames) / fps - half;
- audioFrom = PRE_ROLL + half;
- inputs.push("-threads", String(threads), "-ss", ss.toFixed(6), "-t", (PRE_ROLL + half + n / fps + 0.5).toFixed(6), "-i", master);
- } else {
- skip = fa;
- audioFrom = fa / fps;
- inputs.push("-threads", String(threads), "-t", (fa / fps + n / fps + 0.5).toFixed(6), "-i", master);
- }
- parts.push(`[${i}:v]trim=start_frame=${skip}:end_frame=${skip + n},setpts=PTS-STARTPTS[v${i}]`);
- // Pinned to the picture's length, as build-video's join pins each segment's.
+ const seek = Math.max(0, a - PRE_ROLL);
+ parts.push(`movie=filename=${src}:seek_point=${seek.toFixed(6)}:streams=dv+da:dec_threads=${threads}[m${i}v][m${i}a]`);
+ parts.push(`[m${i}v]trim=start=${Math.max(0, a - half).toFixed(6)}:end=${(a + n / fps - half).toFixed(6)},setpts=PTS-STARTPTS[v${i}]`);
parts.push(
- `[${i}:a]atrim=start=${audioFrom.toFixed(6)}:duration=${d},asetpts=PTS-STARTPTS,` +
+ `[m${i}a]atrim=start=${a.toFixed(6)}:end=${(a + n / fps).toFixed(6)},asetpts=PTS-STARTPTS,` +
`apad=whole_dur=${d},atrim=end=${d}[a${i}]`,
);
});
let vlab = "[v0]";
let alab = "[a0]";
- if (pieces.length > 1 && x > 0) {
- let acc = pieces[0].frames.count / fps;
- for (let i = 1; i < pieces.length; i += 1) {
+ if (runs.length > 1 && x > 0) {
+ let acc = runs[0].count / fps;
+ for (let i = 1; i < runs.length; i += 1) {
const off = acc - x;
parts.push(`${vlab}[v${i}]xfade=transition=fade:duration=${x.toFixed(6)}:offset=${off.toFixed(6)}[xv${i}]`);
parts.push(`${alab}[a${i}]acrossfade=d=${x.toFixed(6)}:c1=tri:c2=tri[xa${i}]`);
vlab = `[xv${i}]`;
alab = `[xa${i}]`;
- acc = acc + pieces[i].frames.count / fps - x;
+ acc = acc + runs[i].count / fps - x;
}
- } else if (pieces.length > 1) {
- parts.push(`${pieces.map((_, i) => `[v${i}][a${i}]`).join("")}concat=n=${pieces.length}:v=1:a=1[cv][ca]`);
+ } else if (runs.length > 1) {
+ parts.push(`${runs.map((_, i) => `[v${i}][a${i}]`).join("")}concat=n=${runs.length}:v=1:a=1[cv][ca]`);
vlab = "[cv]";
alab = "[ca]";
}
return [
"-nostdin", "-v", "error", "-y",
- ...inputs,
"-filter_complex", parts.join(";"),
"-map", vlab, "-map", alab,
- "-map_metadata", "-1", "-map_chapters", "-1",
+ "-map_metadata", "-1",
...encodeArgs(render),
outPath,
];
diff --git a/umtool/report-to-video/trailer.test.mjs b/umtool/report-to-video/trailer.test.mjs
@@ -10,6 +10,7 @@ import test from "node:test";
import {
TrailerError,
+ filterPath,
loadTakeBuild,
makeTrailer,
parseBeats,
@@ -174,46 +175,49 @@ test("scheduleSpans: a rail schedule (starts only) ends each segment where the n
assert.equal(spans.get("b").end, 20);
});
-test("the ffmpeg graph: one seeked input per run, trimmed by frame count, crossfaded, the master's encode", () => {
+test("the ffmpeg graph: one movie source per run, trimmed by the master's timestamps, crossfaded, the master's encode", () => {
const p = plan([{ id: "c1", start: 104.2, end: 108.2 }, { id: "c3", start: 2001, end: 2004 }], { intro: false, finale: 2 });
const render = { fps: 30, crf: 21, preset: "slow", audioBitrate: "160k", audioRate: 48000, audioChannels: 2 };
const argv = trailerArgs({ plan: p, master: "/m/master.mp4", render, outPath: "/o/t.mp4" });
- const inputs = argv.reduce((n, a, i) => n + (a === "-i" && argv[i + 1] === "/m/master.mp4" ? 1 : 0), 0);
- assert.equal(inputs, 3);
- // The first: frame 230 at 7.667 s, seeked half a frame before the frame a second ahead.
- const ss = argv.indexOf("-ss");
- assert.equal(argv[ss + 1], (200 / 30 - 1 / 60).toFixed(6));
+ assert.ok(!argv.includes("-i"), "no -i inputs: the sources are in the graph, read when the chain asks");
const graph = argv[argv.indexOf("-filter_complex") + 1];
- assert.match(graph, /\[0:v\]trim=start_frame=30:end_frame=150,setpts=PTS-STARTPTS\[v0\]/);
- assert.match(graph, /\[0:a\]atrim=start=1\.016667:duration=4\.000000,asetpts=PTS-STARTPTS,apad=whole_dur=4\.000000,atrim=end=4\.000000\[a0\]/);
+ assert.equal(graph.match(/movie=filename=\/m\/master\.mp4:/g).length, 3);
+ // The first run: frame 230 (7.667 s) for 120 frames, the source seeked a second ahead.
+ assert.match(graph, /movie=filename=\/m\/master\.mp4:seek_point=6\.666667:streams=dv\+da:dec_threads=2\[m0v\]\[m0a\]/);
+ assert.match(graph, /\[m0v\]trim=start=7\.650000:end=11\.650000,setpts=PTS-STARTPTS\[v0\]/);
+ assert.match(graph, /\[m0a\]atrim=start=7\.666667:end=11\.666667,asetpts=PTS-STARTPTS,apad=whole_dur=4\.000000,atrim=end=4\.000000\[a0\]/);
assert.match(graph, /\[v0\]\[v1\]xfade=transition=fade:duration=0\.300000:offset=3\.700000\[xv1\]/);
assert.match(graph, /\[a0\]\[a1\]acrossfade=d=0\.300000:c1=tri:c2=tri\[xa1\]/);
assert.match(graph, /\[xv1\]\[v2\]xfade=.*offset=6\.400000\[xv2\]/);
assert.deepEqual(argv.slice(argv.indexOf("-map"), argv.indexOf("-map") + 4), ["-map", "[xv2]", "-map", "[xa2]"]);
- assert.ok(argv.includes("-map_chapters"));
assert.equal(argv[argv.indexOf("-crf") + 1], "21");
assert.equal(argv[argv.indexOf("-preset") + 1], "slow");
assert.equal(argv[argv.indexOf("-b:a") + 1], "160k");
assert.equal(argv.at(-1), "/o/t.mp4");
});
-test("the ffmpeg graph: --xfade 0 is a concat; a run inside the master's first second is not seeked", () => {
+test("the ffmpeg graph: --xfade 0 is a concat; a run at the master's start seeks to 0", () => {
const p = plan([{ id: "c3", start: 2001, end: 2004 }], { intro: true, xfade: 0 });
const argv = trailerArgs({ plan: p, master: "/m.mp4", render: { fps: 30 }, outPath: "/o.mp4" });
const graph = argv[argv.indexOf("-filter_complex") + 1];
assert.match(graph, /\[v0\]\[a0\]\[v1\]\[a1\]concat=n=2:v=1:a=1\[cv\]\[ca\]/);
assert.doesNotMatch(graph, /xfade/);
- assert.match(graph, /\[0:v\]trim=start_frame=0:end_frame=/);
- assert.equal(argv.filter((a) => a === "-ss").length, 1, "the intro's input reads from 0");
+ assert.match(graph, /seek_point=0\.000000:.*\[m0v\]trim=start=0\.000000:/);
});
-test("the ffmpeg graph: a seamless pair is one input, one run", () => {
+test("the ffmpeg graph: a seamless pair is one source, one run", () => {
const p = plan([{ id: "c1", start: 100.3, end: 104 }], { intro: true });
const argv = trailerArgs({ plan: p, master: "/m.mp4", render: { fps: 30 }, outPath: "/o.mp4" });
- assert.equal(argv.filter((a) => a === "-i").length, 1);
+ const graph = argv[argv.indexOf("-filter_complex") + 1];
+ assert.equal(graph.match(/movie=/g).length, 1);
assert.deepEqual(argv.slice(argv.indexOf("-map"), argv.indexOf("-map") + 2), ["-map", "[v0]"]);
});
+test("filterPath: a file name escaped for the movie option, then for the graph", () => {
+ assert.equal(filterPath("/a/b.mp4"), "/a/b.mp4");
+ assert.equal(filterPath("/x/it's:a,[b];c.mp4"), "/x/it\\\\\\'s\\\\:a\\,\\[b\\]\\;c.mp4");
+});
+
test("output paths: the default, a bare --out name, a path; the record beside", () => {
assert.equal(trailerOutPath("/r/takes/t", "slug"), "/r/takes/t/out/slug.trailer.mp4");
assert.equal(trailerOutPath("/r/takes/t", "slug", "teaser"), "/r/takes/t/out/teaser.mp4");