One-click character swap for videos: an open-source, local Higgsfield Genjutsu alternative built on ComfyUI and MiniMax H3, with FLUX.3 Klein relighting.
"""Writes workflows/face.api.json, the Openjutsu Fix faces workflow, with Follow the mouth on.
The face of the person in Picture 1 is tracked and cropped over the whole video, its edit mask regenerated by H3 in
independent overlapping chunks (H3 Face Fix Chunks), crossfaded back (H3 Face Fix Join) and pasted onto the video.
Needs ComfyUI-H3-FaceRefine (track, SAM face masks, stitch) and Impact Pack (SAMLoader).
usage: python openjutsu_face_build.py [video picture] with names in ComfyUI input/ also queues a test run
"""
import json, sys, urllib.request
from pathlib import Path
g = {}
def node(i, cls, title, **inputs):
g[i] = {"inputs": inputs, "class_type": cls, "_meta": {"title": title}}
return [i, 0]
node("1", "VHS_LoadVideo", "Video to fix", video="", force_rate=24, custom_width=0, custom_height=0, frame_load_cap=0,
skip_first_frames=0, select_every_nth=1, format="None")
node("2", "LoadImage", "Picture 1", image="")
node("3", "ImageScaleToMaxDimension", "Shrink for the describer", upscale_method="area", largest_size=768, image=["2", 0])
node("4", "CLIPLoader", "Describer: Qwen3-VL 8B", clip_name="qwen3vl_8b_int8_convrot.safetensors", type="stable_diffusion", device="default")
node("5", "TextGenerate", "Describe the face in picture 1",
prompt="Describe this person's face for a casting sheet, in one sentence of about 30 words: apparent age and gender, face shape, skin tone, "
"eyebrows, facial hair and any glasses. No preamble, no clothing, no background.",
max_length=90, sampling_mode="off", thinking=False, use_default_template=True, mtp="auto", clip=["4", 0], image=["3", 0])
track = dict(detector="bbox\\face_yolov8m.pt", confidence=0.35, crop_factor=2.5, canvas_width=512, canvas_height=512, canvas_mode="manual",
smooth_window=21, size_smooth_window=51, smooth_method="gaussian", size_mode="per_frame", identity_track=True,
identity_threshold=0.28, select="largest", fallback_head_frac=0.5)
node("6", "H3FaceTrackCrop", "Track + crop the face", images=["1", 0], identity_reference=["2", 0],
fallback_detector="segm\\person_yolov8m-seg.pt", **track)
node("7", "H3FaceTrackCrop", "Face from picture 1", images=["2", 0], fallback_detector="none", **track)
node("8", "SAMLoader", "SAM (face masks)", model_name="sam_vit_b_01ec64.pth", device_mode="AUTO")
# H3 regenerates a little more than is pasted back, so the paste edge lands inside regenerated pixels
node("9", "H3FaceMaskSAM", "Face mask (regenerated)", crops=["6", 0], sam_model=["8", 0], transform=["6", 1], threshold=0.93, dilation=24, temporal_smooth=5)
node("10", "H3FaceMaskSAM", "Face mask (pasted back)", crops=["6", 0], sam_model=["8", 0], transform=["6", 1], threshold=0.93, dilation=8, temporal_smooth=5)
node("11", "ImageBlur", "Blurred face (follow the mouth)", image=["6", 0], blur_radius=9, sigma=3.0)
node("12", "H3FaceChunks", "Face Fix Chunks", crops=["6", 0], masks=["9", 0], audio=["1", 2], transform=["6", 1], chunk_seconds=6.5,
skip_faces_above=0.2, follow_mouth=["11", 0], picture_text=["5", 0])
node("13", "UNETLoader", "H3 Diffusion Model", unet_name="minimax_h3_ref2va_pruned_int8_convrot.safetensors", weight_dtype="default")
node("14", "LoraLoaderModelOnly", "DMAD LoRA", lora_name="minimax_h3_DMAD_4step_full_lora_avg_rank_39_bf16.safetensors", strength_model=1, model=["13", 0])
node("15", "ModelAttentionBackend", "Comfy Kitchen Attention", attention="comfy kitchen attention", model=["14", 0])
node("16", "CLIPLoader", "H3 Text Encoder", clip_name="qwen3vl_32b_minimax_h3_int4_convrot.safetensors", type="minimax", device="default")
node("17", "VAELoader", "Video VAE", vae_name="minimax_h3_video_vae_int8_convrot.safetensors")
node("18", "VAELoader", "Audio VAE", vae_name="minimax_h3_audio_vae_fp32.safetensors")
node("19", "MiniMaxH3FantasticVideoEditLatent", "Edit latent (face + audio)", grow=0, keep_audio=True, megapixels=0, vae=["17", 0],
images=["12", 0], mask=["12", 1], audio_vae=["18", 0], audio=["12", 3])
node("20", "MiniMaxH3ContexMaskedTarget", "Masked target", mask_meaning="white = generate", audio_mode="preserve source audio",
mask_conversion="H3 exact (causal/token max)", target_latent=["19", 0], mask=["12", 1])
node("21", "MiniMaxH3ReferenceToVideo", "Reference Conditioning", prompt=["12", 4], width=["19", 1], height=["19", 2], length=["19", 3],
ref_image_size="match", clip=["16", 0], vae=["17", 0], audio_vae=["18", 0],
**{"ref_images.ref_image_0": ["7", 0], "ref_videos.ref_video_0": ["12", 2]})
node("22", "BasicGuider", "Guider", model=["15", 0], conditioning=["21", 0])
node("23", "KSamplerSelect", "Sampler", sampler_name="euler")
node("24", "RandomNoise", "Seed", noise_seed=0)
node("25", "BasicScheduler", "Schedule", scheduler="simple", steps=6, denoise=1, model=["15", 0])
node("26", "SamplerCustomAdvanced", "Sample face", noise=["24", 0], guider=["22", 0], sampler=["23", 0], sigmas=["25", 0], latent_image=["20", 0])
node("27", "VAEDecode", "Decode face", samples=["26", 0], vae=["17", 0])
node("28", "H3FaceJoin", "Face Fix Join", images=["27", 0], plan=["12", 5], crops=["6", 0], transform=["6", 1])
node("29", "H3FaceStitch", "Paste the face back", paste_region="face_only", mask_dilation=0, feather=8, colour_match=1.0, blend=1.0,
undetected_frames="fade_out", feather_scales_with_crop=False, base_images=["1", 0], refined_crops=["28", 0], transform=["28", 1], masks=["10", 0])
node("30", "VHS_VideoCombine", "Fixed video", frame_rate=24, loop_count=0, filename_prefix="openjutsu_face/face", format="video/h264-mp4",
pix_fmt="yuv420p", crf=16, save_metadata=False, trim_to_audio=False, pingpong=False, save_output=True, images=["29", 0], audio=["1", 2])
with open(Path(__file__).resolve().parent.parent / "face.api.json", "w", encoding="utf-8") as f:
json.dump(g, f, indent=2)
f.write("\n")
print("wrote face.api.json,", len(g), "nodes")
if len(sys.argv) == 3:
g["1"]["inputs"]["video"], g["2"]["inputs"]["image"] = sys.argv[1], sys.argv[2]
r = urllib.request.urlopen(urllib.request.Request("http://127.0.0.1:8188/prompt", json.dumps({"prompt": g}).encode(), {"content-type": "application/json"}))
print(r.read().decode())