Skip to content

One-click character swap for videos: an open-source, local Higgsfield Genjutsu alternative built on ComfyUI and MiniMax H3, with FLUX.3 Klein relighting.

Estimate clips with the pack's clip_plan and label the estimate (L5) (#14)

mixbag6c68fd7486d9
diff --git a/components/openjutsu.tsx b/components/openjutsu.tsx
index 173f084..6b9a60c 100644
--- a/components/openjutsu.tsx
+++ b/components/openjutsu.tsx
@@ -1078,10 +1078,10 @@ export function Openjutsu() {
<small>{colorFix
? !video ? "Add a video." : ready ? `${(part.length || video.duration).toFixed(1)} s of video` : "Turn on detail contrast, the tone mapper or both."
: clips && ready
- ? mode === "advanced" && montage ? `${clips} clip${clips > 1 ? "s" : ""} or more at ${canvas ? `${canvas[0]}×${canvas[1]}` : resolution}; Find people (about 3 minutes per 18 s) lets you check the picks first.`
+ ? mode === "advanced" && montage ? `${clips > 1 ? "About " : ""}${clips} clip${clips > 1 ? "s" : ""} or more at ${canvas ? `${canvas[0]}×${canvas[1]}` : resolution}; Find people (about 3 minutes per 18 s) lets you check the picks first.`
: faceFix ? `Up to ${clips} chunk${clips > 1 ? "s" : ""} of face`
: controlNet ? `One clip at ${canvas ? `${canvas[0]}×${canvas[1]}` : resolution}${(part.length || video!.duration) > CONTROL_MAX_SECONDS ? `: only the first ${CONTROL_MAX_SECONDS} s of this part are used` : ""}`
- : `${clips} clip${clips > 1 ? "s" : ""} at ${canvas ? `${canvas[0]}×${canvas[1]}` : resolution}`
+ : `${clips > 1 ? "About " : ""}${clips} clip${clips > 1 ? "s" : ""} at ${canvas ? `${canvas[0]}×${canvas[1]}` : resolution}`
: mode === "object" && editMode === "add" && video && !placement.length ? "Draw where the new object should go."
: `Add a video and ${mode === "object" ? editMode === "add" ? "describe the object to add" : "say what it becomes" : faceFix ? "the photo of the person" : mode === "simple" ? "a character photo" : "character photos"}.`}</small>
</div>
@@ -1187,7 +1187,7 @@ function RunView({ run, job, progress, preview, streamError, now, before, onCanc
<div className="row">
<h2>{status === "queued" ? `Queued${job?.queuePosition ? ` (#${job.queuePosition})` : ""}` : people ? "Finding people" : mask ? "Checking the mask" : color ? "Fixing the colours"
: face ? (sampling ? `Chunk ${chunk} of up to ${run.clips}` : chunk ? "Pasting the face back" : "Tracking the face")
- : progress?.clip ? `Clip ${progress.clip} of ${clips}` : "Preparing"}</h2>
+ : progress?.clip ? `Clip ${progress.clip} of ${clips > 1 || run.kind === "montage" ? "about " : ""}${clips}` : "Preparing"}</h2>
<span className="time">{elapsed(now - run.startedAt)}</span>
</div>
<div className="bar"><div style={{ width: `${Math.round(fraction * 100)}%` }} /></div>
@@ -1315,13 +1315,13 @@ function MaskCheck({ url, clipSeconds, checked, margin, onMargin, onRecheck, onG
<button className="secondary" onClick={toggle}>{playing ? "Pause" : "Play"}</button>
<button className="secondary" onClick={() => seek(frame + 1)} aria-label="Next frame">▶</button>
</div>
- <small>Frame {frame + 1} / {frames} · {(frame / 24).toFixed(2)} s · clip {starts.filter((start) => start <= frame).length} of {starts.length}</small>
+ <small>Frame {frame + 1} / {frames} · {(frame / 24).toFixed(2)} s · clip {starts.filter((start) => start <= frame).length} of {starts.length > 1 ? "about " : ""}{starts.length}</small>
</div>
<p className="hint legend">
<i className="swatch cyan" /> regenerated <i className="swatch orange" /> tracked person · right: the grey mannequin H3 copies the motion from.
Anything outside the cyan area stays as it is, so the new character should fit inside it.
</p>
- <p className="hint">The check tracks the whole video at once and the render tracks each clip, so they can differ slightly.</p>
+ <p className="hint">The check tracks the whole video at once and the render tracks each clip, so they can differ slightly. Clip numbers here are an estimate: the render also ends clips on hard cuts.</p>
<div className="setting">
<span>Mask margin <small>{margin === checked ? `Showing ${checked}.` : `Showing ${checked}; check again to see ${margin}.`} Tight keeps more background, loose gives a bigger character more room.</small></span>
<MarginPicker value={margin} onChange={onMargin} />
diff --git a/lib/clips.ts b/lib/clips.ts
index 517d574..1c10804 100644
--- a/lib/clips.ts
+++ b/lib/clips.ts
@@ -1,33 +1,41 @@
-// Mirrors clip_lengths() in the H3OneClickSwapPlan node, so the page can show "clip k of N".
+// Estimates the clip plan of the H3OneClickSwapPlan node (clip_plan() in comfyui-h3-oneclick-swap), so the page can
+// show about how many clips a run takes and where they start. It is exact for a video without hard cuts. The pack also
+// ends a clip on a nearby hard cut and starts the next one without overlap; the page cannot see the cuts before a
+// run, so for a video with cuts the real count and boundaries can differ.
const FPS = 24;
const OVERLAP = 39;
const gridFloor = (frames: number) => Math.floor((frames - 5) / 17) * 17 + 5;
-const gridCeil = (frames: number) => Math.ceil((frames - 5) / 17) * 17 + 5;
-function clipLengths(durationSeconds: number, clipSeconds: number) {
+// [length, context] per clip: each clip is the longest that fits and repeats the previous clip's last frames.
+function clipPlan(durationSeconds: number, clipSeconds: number) {
const total = Math.floor(durationSeconds * FPS);
- const clipFrames = Math.floor(clipSeconds * FPS);
- if (!Number.isFinite(total) || total < 5 || !Number.isFinite(clipFrames) || gridFloor(clipFrames) <= OVERLAP) return [];
- const lengths = [Math.min(gridFloor(clipFrames), gridFloor(total))];
- let rem = total - lengths[0];
- const count = rem >= 17 ? Math.ceil(rem / (gridFloor(clipFrames) - OVERLAP)) : 0;
- for (let j = 0; j < count; j++) {
- const length = Math.min(gridCeil(Math.ceil(rem / (count - j)) + OVERLAP), gridFloor(rem + OVERLAP));
- if (length - OVERLAP < 17) break;
- lengths.push(length);
- rem -= length - OVERLAP;
+ const longest = gridFloor(Math.floor(clipSeconds * FPS));
+ if (!Number.isFinite(total) || total < 5 || !Number.isFinite(longest) || longest <= OVERLAP) return [];
+ const clips: [number, number][] = [];
+ let end = 0;
+ let context = 0;
+ while (true) {
+ const start = end - context;
+ const length = gridFloor(Math.min(longest, total - start));
+ if (clips.length && length - context < 17) return clips;
+ clips.push([length, context]);
+ end = start + length;
+ context = Math.min(OVERLAP, gridFloor(length - context));
}
- return lengths;
}
export function clipCount(durationSeconds: number, clipSeconds: number) {
- return clipLengths(durationSeconds, clipSeconds).length;
+ return clipPlan(durationSeconds, clipSeconds).length;
}
-// First source frame of each clip (clips after the first start OVERLAP frames early).
+// First source frame of each clip (clips after the first start on the frames they repeat).
export function clipStarts(durationSeconds: number, clipSeconds: number) {
- const starts = [0];
- for (const length of clipLengths(durationSeconds, clipSeconds).slice(0, -1)) starts.push(starts.at(-1)! + length - OVERLAP);
- return starts;
+ const starts: number[] = [];
+ let end = 0;
+ for (const [length, context] of clipPlan(durationSeconds, clipSeconds)) {
+ starts.push(end - context);
+ end = starts.at(-1)! + length;
+ }
+ return starts.length ? starts : [0];
}
diff --git a/test/simple.test.ts b/test/simple.test.ts
index 304153f..822e1c1 100644
--- a/test/simple.test.ts
+++ b/test/simple.test.ts
@@ -132,3 +132,10 @@ test("the mask check drops the hat nodes without hat fix", () => {
test("clip starts follow the Swap Plan's 39-frame overlap", () => {
assert.deepEqual(clipStarts(339 / 24, 6), [0, 102, 204]);
});
+
+test("clips match clip_plan for a video without cuts: each is the longest that fits", () => {
+ assert.equal(clipCount(107 / 24, 3), 3);
+ // the last clip repeats only five frames: the clip before it had just 17 new frames
+ assert.deepEqual(clipStarts(107 / 24, 3), [0, 17, 68]);
+ assert.deepEqual(clipStarts(16, 6), [0, 102, 204, 306]);
+});