One-click character swap for videos: an open-source, local Higgsfield Genjutsu alternative built on ComfyUI and MiniMax H3, with FLUX.3 Klein relighting.
Add custom resolution option
D3voz45ca4ff2c68d
diff --git a/README.md b/README.mdindex a2ca3cc..5942ec5 100644--- a/README.md+++ b/README.md@@ -75,7 +75,7 @@ On Windows, double-click `start_openjutsu.bat` (ComfyUI must be running, or star`.env.local` sets `COMFY_BASE_URL=http://127.0.0.1:8189`. Leave `COMFY_API_KEY` empty for a local proxy. The base model and LoRA pickers read ComfyUI's model lists from `COMFYUI_URL` (default `http://127.0.0.1:8188`); without it they become text fields.-A 6 s clip at 480p takes about 6–13 minutes on a 4060 Ti, most of it in H3 sampling. 736p is the default; 1080p is about 2.5× slower than 736p and needs much more memory.+A 6 s clip at 480p takes about 6–13 minutes on a 4060 Ti, most of it in H3 sampling. 736p is the default; 1080p is about 2.5× slower than 736p and needs much more memory. Custom sets any short edge from 256 to 2048 px (rounded to 32); the other side follows the video's shape. It needs the `short_edge` input of the H3 Swap Plan nodes in comfyui-h3-oneclick-swap.## Tipsdiff --git a/app/api/jobs/route.ts b/app/api/jobs/route.tsindex 4d95a28..90d6dc6 100644--- a/app/api/jobs/route.ts+++ b/app/api/jobs/route.ts@@ -5,7 +5,7 @@ import { CHUNK_SECONDS, FACE_STYLES, type FaceSettings, type FaceStyle } from ".import { MAX_CHARACTERS } from "../../../lib/advanced.ts";import { MAX_LORAS, STEPS, STRENGTH, type ModelSettings } from "../../../lib/model.ts";import { EDIT_MODES, OBJECT_MARGINS, type EditMode, type ObjectMargin, type ObjectSettings } from "../../../lib/object.ts";-import { CLIP_SECONDS, DEFAULT_TARGET, MARGINS, MAX_SEED, MIN_SECTION_SECONDS, MOTION_SIZES, RESOLUTIONS, type Margin, type MotionSize, type Resolution, type Section, type SimpleSettings } from "../../../lib/simple.ts";+import { CLIP_SECONDS, DEFAULT_TARGET, MARGINS, MAX_SEED, MIN_SECTION_SECONDS, MOTION_SIZES, RESOLUTIONS, SHORT_EDGE, type Margin, type MotionSize, type Resolution, type Section, type SimpleSettings } from "../../../lib/simple.ts";export const runtime = "nodejs";@@ -39,8 +39,12 @@ function parsePicture(picture: FormDataEntryValue | null) {}function parseRun(form: FormData) {- const resolution = String(form.get("resolution"));- if (!RESOLUTIONS.includes(resolution as Resolution)) throw new Error("Choose a supported resolution.");+ const entry = String(form.get("resolution"));+ const edge = Number(entry);+ const resolution: Resolution = RESOLUTIONS.includes(entry as (typeof RESOLUTIONS)[number]) ? entry as Resolution : edge;+ if (typeof resolution === "number" && !(Number.isInteger(edge) && edge >= SHORT_EDGE.min && edge <= SHORT_EDGE.max)) {+ throw new Error(`Choose a resolution, or a custom short edge of ${SHORT_EDGE.min}-${SHORT_EDGE.max} px.`);+ }const motion = Number(form.get("motion"));if (!MOTION_SIZES.includes(motion as MotionSize)) throw new Error("Choose a supported motion reference size.");const seed = Number(form.get("seed"));@@ -50,7 +54,7 @@ function parseRun(form: FormData) {throw new Error(`Clip length must be ${CLIP_SECONDS.min}-${CLIP_SECONDS.max} s.`);}- return { resolution: resolution as Resolution, motion: motion as MotionSize, seed, clipSeconds };+ return { resolution, motion: motion as MotionSize, seed, clipSeconds };}function parseObject(form: FormData) {diff --git a/components/openjutsu.tsx b/components/openjutsu.tsxindex 103d476..6123f40 100644--- a/components/openjutsu.tsx+++ b/components/openjutsu.tsx@@ -4,7 +4,7 @@ import { useEffect, useRef, useState, type DragEvent, type ReactNode } from "reaimport type { PublicJob } from "../lib/comfy.ts";import type { NodeInfo } from "../lib/progress.ts";import { clipCount, clipStarts } from "../lib/clips.ts";-import { CLIP_SECONDS, DEFAULT_TARGET, MARGINS, MAX_SEED, MIN_SECTION_SECONDS, MOTION_SIZES, RESOLUTIONS, type Margin, type MotionSize, type Resolution, type Section } from "../lib/simple.ts";+import { CLIP_SECONDS, DEFAULT_TARGET, MARGINS, MAX_SEED, MIN_SECTION_SECONDS, MOTION_SIZES, RESOLUTIONS, SHORT_EDGE, canvasSize, type Margin, type MotionSize, type Resolution, type Section } from "../lib/simple.ts";import { MAX_CHARACTERS } from "../lib/advanced.ts";import { DEFAULT_MODEL, MAX_LORAS, STEPS, STRENGTH, type Lora } from "../lib/model.ts";import type { EditMode } from "../lib/object.ts";@@ -136,12 +136,13 @@ function DropZone({ accept, onFiles, multiple, children, className }: {export function Openjutsu() {const [mode, setMode] = useState<Mode>("simple");- const [video, setVideo] = useState<{ file: File; url: string; duration: number } | null>(null);+ const [video, setVideo] = useState<{ file: File; url: string; duration: number; width: number; height: number } | null>(null);const [part, setPart] = useState<Section>({ start: 0, length: 0 });const player = useRef<HTMLVideoElement>(null);const [characters, setCharacters] = useState<Character[]>([]);const [maxQuality, setMaxQuality] = useState(false);const [resolution, setResolution] = useState<Resolution>("736p");+ const [customEdge, setCustomEdge] = useState(SHORT_EDGE.default);const [hatFix, setHatFix] = useState(false);const [noLipSync, setNoLipSync] = useState(false);const [margin, setMargin] = useState<Margin>("normal");@@ -228,12 +229,12 @@ export function Openjutsu() {if (!file) return;if (video) URL.revokeObjectURL(video.url);const url = URL.createObjectURL(file);- setVideo({ file, url, duration: 0 });+ setVideo({ file, url, duration: 0, width: 0, height: 0 });setPart({ start: 0, length: 0 });const probe = document.createElement("video");probe.preload = "metadata";probe.onloadedmetadata = () => {- setVideo((current) => (current?.url === url ? { ...current, duration: probe.duration } : current));+ setVideo((current) => (current?.url === url ? { ...current, duration: probe.duration, width: probe.videoWidth, height: probe.videoHeight } : current));setPart({ start: 0, length: probe.duration });};probe.src = url;@@ -288,6 +289,15 @@ export function Openjutsu() {setCharacters(characters.map((c, i) => (i === index ? { ...c, role } : c)));}+ const canvas = video?.width ? canvasSize(video.width, video.height, resolution) : null;++ function chooseEdge(value: number) {+ const edge = Math.min(SHORT_EDGE.max, Math.max(SHORT_EDGE.min, Math.round(value / SHORT_EDGE.step) * SHORT_EDGE.step || SHORT_EDGE.default));+ setCustomEdge(edge);+ setResolution(edge);+ return edge;+ }+const clips = !video?.duration ? 0 : faceFix ? faceChunkCount(part.length || video.duration, chunkSeconds) : clipCount(part.length || video.duration, clipSeconds);const edit = EDITS.find((e) => e.id === editMode)!;const fromPicture = mode === "object" && editMode === "picture";@@ -324,7 +334,7 @@ export function Openjutsu() {form.set("role", character.role);}if (kind === "simple") {- form.set("resolution", resolution);+ form.set("resolution", String(resolution));form.set("seed", String(seed));form.set("clip_seconds", String(clipSeconds));}@@ -335,7 +345,7 @@ export function Openjutsu() {form.append("role", c.role);}form.set("max_quality", String(kind === "advanced_maxq"));- form.set("resolution", resolution);+ form.set("resolution", String(resolution));form.set("seed", String(seed));form.set("clip_seconds", String(clipSeconds));}@@ -359,7 +369,7 @@ export function Openjutsu() {if (look && editMode !== "remove") form.set("picture", look.file);form.set("biggest_only", String(biggestOnly));form.set("keep_outline", String(keepOutline));- form.set("resolution", resolution);+ form.set("resolution", String(resolution));form.set("seed", String(seed));form.set("clip_seconds", String(clipSeconds));}@@ -628,12 +638,20 @@ export function Openjutsu() {<section className="panel settings"><h2><span>3</span> Settings</h2>{mode !== "post" && <div className="setting">- <span>Resolution</span>+ <span>Resolution <small>{canvas ? `${canvas[0]} × ${canvas[1]} for this video.` : "Short edge of the video."} The shape follows the source.</small></span><div className="segmented">{RESOLUTIONS.map((r) => (<button key={r} className={r === resolution ? "on" : ""} onClick={() => setResolution(r)}>{r}</button>))}+ <button className={typeof resolution === "number" ? "on" : ""} onClick={() => setResolution(customEdge)}>Custom</button></div>+ {typeof resolution === "number" && (+ <input+ className="steps-input" type="number" aria-label="Custom short edge in pixels" min={SHORT_EDGE.min} max={SHORT_EDGE.max} step={SHORT_EDGE.step}+ defaultValue={resolution} onBlur={(event) => { event.target.value = String(chooseEdge(Number(event.target.value))); }}+ onKeyDown={(event) => { if (event.key === "Enter") event.currentTarget.blur(); }}+ />+ )}</div>}{mode === "object" && editMode !== "background" && !fromPicture && (<label className="setting toggle">@@ -750,7 +768,7 @@ 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- ? faceFix ? `Up to ${clips} chunk${clips > 1 ? "s" : ""} of face` : `${clips} clip${clips > 1 ? "s" : ""} at ${resolution}`+ ? faceFix ? `Up to ${clips} chunk${clips > 1 ? "s" : ""} of face` : `${clips} clip${clips > 1 ? "s" : ""} at ${canvas ? `${canvas[0]}×${canvas[1]}` : resolution}`: `Add a video and ${mode === "object" ? "say what it becomes" : faceFix ? "the photo of the person" : mode === "simple" ? "a character photo" : "character photos"}.`}</small></div></>diff --git a/lib/advanced.ts b/lib/advanced.tsindex 2db35b7..469efa0 100644--- a/lib/advanced.ts+++ b/lib/advanced.ts@@ -2,7 +2,7 @@// workflows/advanced.api.json (one pass) and advanced_maxq.api.json (one pass per person, 2 characters) are exported// with every picture, the hat fix and No lip-sync enabled; each request prunes what it does not use. Nodes are found by title, as// the builder (ComfyUI user/default/oneclick_advanced_build.js) names them.-import { MARGINS, dropNoLipSync, isLink, setMotionSize, type ApiGraph, type Margin, type MotionSize, type Resolution } from "./simple.ts";+import { MARGINS, dropNoLipSync, isLink, setMotionSize, setResolution, type ApiGraph, type Margin, type MotionSize, type Resolution } from "./simple.ts";export const MAX_CHARACTERS = 4;@@ -75,7 +75,7 @@ export function pruneAdvanced(source: ApiGraph, pictures: number, settings: Adva}const plan = graph[nodeByTitle(graph, PLAN)].inputs;const roles = settings.roles.slice(0, pictures).map((role) => role.replace(/\s+/g, " ").trim());- plan.resolution = settings.resolution;+ setResolution(plan, settings.resolution);plan.seed = settings.seed;plan.clip_seconds = settings.clipSeconds;plan.roles = roles.some(Boolean) ? roles.map((role, i) => role || ordinal(i, pictures)).join("\n") : "";diff --git a/lib/object.ts b/lib/object.tsindex 8784b44..7747ef7 100644--- a/lib/object.ts+++ b/lib/object.ts@@ -2,7 +2,7 @@// workflows/object.api.json is exported (ComfyUI user/default/oneclick_object_build.js) in change mode with a picture;// each request prunes it. Nodes are found by title.import { nodeByTitle } from "./advanced.ts";-import { setMotionSize, type ApiGraph, type MotionSize, type Resolution } from "./simple.ts";+import { setMotionSize, setResolution, type ApiGraph, type MotionSize, type Resolution } from "./simple.ts";export const EDIT_MODES = ["change", "remove", "background"] as const;export type EditMode = (typeof EDIT_MODES)[number];@@ -82,9 +82,9 @@ export function pruneObject(source: ApiGraph, picture: boolean, settings: Objecttarget: settings.target,change_to: settings.changeTo,keep_outline: settings.keepOutline,- resolution: settings.resolution,seed: settings.seed,clip_seconds: settings.clipSeconds,});+ setResolution(plan, settings.resolution);return graph;}diff --git a/lib/simple.ts b/lib/simple.tsindex 96fdf58..89d42c4 100644--- a/lib/simple.ts+++ b/lib/simple.ts@@ -6,7 +6,20 @@ export type ApiNode = { class_type: string; inputs: Record<string, unknown>; _meexport type ApiGraph = Record<string, ApiNode>;export const RESOLUTIONS = ["480p", "576p", "736p", "768p", "1080p"] as const;-export type Resolution = (typeof RESOLUTIONS)[number];+// A number is a custom short edge in pixels; the Swap Plan rounds the canvas to 32 and keeps the source shape either way.+export type Resolution = (typeof RESOLUTIONS)[number] | number;+export const SHORT_EDGE = { min: 256, max: 2048, step: 32, default: 960 };++// Canvas the Swap Plan makes for a source of this size, as its plan_clips does.+export function canvasSize(width: number, height: number, resolution: Resolution) {+ const scale = (typeof resolution === "number" ? resolution : parseInt(resolution)) / Math.min(width, height);+ return [width, height].map((side) => Math.max(32, Math.round((side * scale) / 32) * 32));+}++export function setResolution(plan: Record<string, unknown>, resolution: Resolution) {+ if (typeof resolution === "number") plan.short_edge = resolution;+ else plan.resolution = resolution;+}export const CLIP_SECONDS = { min: 2.5, max: 15, default: 6 };export const MAX_SEED = 0xffffffff;@@ -129,7 +142,7 @@ export function pruneSimple(source: ApiGraph, settings: SimpleSettings): ApiGrapif (!settings.noLipSync) dropNoLipSync(graph);setMotionSize(graph, settings.motion);const plan = graph[NODES.plan].inputs;- plan.resolution = settings.resolution;+ setResolution(plan, settings.resolution);plan.seed = settings.seed;plan.clip_seconds = settings.clipSeconds;plan.roles = settings.role.replace(/\s+/g, " ").trim();diff --git a/test/object.test.ts b/test/object.test.tsindex 07bf6b3..c643260 100644--- a/test/object.test.ts+++ b/test/object.test.ts@@ -31,6 +31,7 @@ test("change with a picture keeps the graph and sets the plan", () => {assert.equal(node(pruned, "Grow mask").inputs.expand, 40);assert.equal(node(pruned, "Tracked area").inputs.max_people, 1);assert.ok(node(graph, OBJECT_PICTURE), "the source graph is not modified");+ assert.equal(node(pruneObject(graph, true, { ...settings("change"), resolution: 640 }), "Object Swap Plan (what to change)").inputs.short_edge, 640);});test("several things are tracked together, one of each when only the biggest is kept", () => {diff --git a/test/simple.test.ts b/test/simple.test.tsindex e2bee17..283ef34 100644--- a/test/simple.test.ts+++ b/test/simple.test.ts@@ -3,7 +3,7 @@ import { readFileSync } from "node:fs";import test from "node:test";import { clipCount, clipStarts } from "../lib/clips.ts";import { nodeDescriber } from "../lib/progress.ts";-import { MASK_NODES, NODES, pruneMask, pruneSimple, setSection, type ApiGraph, type SimpleSettings } from "../lib/simple.ts";+import { MASK_NODES, NODES, canvasSize, pruneMask, pruneSimple, setSection, type ApiGraph, type SimpleSettings } from "../lib/simple.ts";const isLinkTo = (g: ApiGraph, value: unknown, type: string) => Array.isArray(value) && g[value[0] as string]?.class_type === type;const load = (name: string): ApiGraph => JSON.parse(readFileSync(new URL(`../workflows/${name}.api.json`, import.meta.url), "utf8"));@@ -34,6 +34,15 @@ test("without hat fix the hat nodes go and the grow mask reads the people mask",assert.ok(graph[NODES.hat[0]], "the source graph is not modified");});+test("a custom resolution is the Swap Plan's short edge; the canvas keeps the source shape", () => {+ const plan = pruneSimple(graph, { ...settings(true), resolution: 1000 })[NODES.plan].inputs;+ assert.deepEqual([plan.short_edge, plan.resolution], [1000, graph[NODES.plan].inputs.resolution]);+ assert.equal(pruneSimple(graph, settings(true))[NODES.plan].inputs.short_edge, undefined);+ assert.deepEqual(canvasSize(640, 360, "736p"), [1312, 736]);+ assert.deepEqual(canvasSize(640, 360, 1000), [1792, 992]);+ assert.deepEqual(canvasSize(1080, 1920, 1000), [992, 1792]);+});+test("a part of the video becomes VHS's skip and frame cap; the whole video keeps 0", () => {const pruned = pruneSimple(graph, settings(false));setSection(pruned, NODES.video, { start: 3.5, length: 4 });