#!/usr/bin/env python3 """manju I2V:ComfyUI 图生视频提交器(默认适配 Wan2.2 A14B)。 零第三方依赖;模型参数来自 config/pipeline.json,运动提示词来自 40_prompts/Sxx.i2v.txt, 产物只写 work//videos/Sxx_.mp4。换模型需改下方 UNET/LORA 常量。 用法: python3 tools/submit_i2v.py projects/EPxx/30_shots.json --only S01 python3 tools/submit_i2v.py projects/EPxx/30_shots.json """ import argparse import hashlib import json import mimetypes import os import sys import time import urllib.error import urllib.parse import urllib.request import uuid def _find_root(): """工作空间根:MANJU_ROOT > cwd 向上找 config/pipeline.json > 脚本上级。""" env = os.environ.get("MANJU_ROOT") if env: return os.path.abspath(os.path.expanduser(env)) d = os.getcwd() while True: if os.path.isfile(os.path.join(d, "config", "pipeline.json")): return d nd = os.path.dirname(d) if nd == d: break d = nd return os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ROOT = _find_root() CFG = json.load(open(os.path.join(ROOT, "config", "pipeline.json"), encoding="utf-8")) VM = CFG["videoModel"] HOST = VM["endpoint"] # 本机代理会把明文 HTTP 请求改写成 text/html,必须显式绕过 opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) UNET_HIGH = "wan2.2_i2v_high_noise_14B_fp8_scaled.safetensors" UNET_LOW = "wan2.2_i2v_low_noise_14B_fp8_scaled.safetensors" LORA_HIGH = "wan2.2_i2v_lightx2v_4steps_lora_v1_high_noise.safetensors" LORA_LOW = "wan2.2_i2v_lightx2v_4steps_lora_v1_low_noise.safetensors" CLIP = "umt5_xxl_fp8_e4m3fn_scaled.safetensors" VAE = "wan_2.1_vae.safetensors" NEGATIVE = ("blurry, low quality, deformed, distorted face, face morphing, identity drift, " "extra limbs, extra fingers, text, letters, subtitles, watermark, logo, brand, UI, " "jitter, flicker, morphing, fast camera movement, camera shake, oversaturated, " "cartoonish deformation, sudden rotation") def request(method, path, data=None, headers=None, timeout=90): r = urllib.request.Request(HOST + path, data=data, method=method, headers=headers or {}) with opener.open(r, timeout=timeout) as resp: return resp.read(), resp.headers.get("Content-Type", "") def get_json(path, timeout=90): raw, _ = request("GET", path, timeout=timeout) return json.loads(raw) def health(): """按启动参数预检,不按 RAM 硬挡(旧坑:--force-fp16 把 fp8 升 fp16 撞 4GB cgroup)。""" d = get_json("/system_stats", timeout=20) info = d.get("system", {}) argv = info.get("argv", []) or [] if "--force-fp16" in argv and os.environ.get("A14B_FORCE_CAPACITY") != "1": raise RuntimeError("服务端仍带 --force-fp16:fp8 权重被升成 fp16 会 OOM,请先修服务端") return info.get("comfyui_version", "unknown") def upload_image(path): boundary = "----xv" + uuid.uuid4().hex fn = os.path.basename(path) ctype = mimetypes.guess_type(fn)[0] or "image/png" payload = open(path, "rb").read() head = ("--" + boundary + "\r\n" 'Content-Disposition: form-data; name="image"; filename="' + fn + '"\r\n' "Content-Type: " + ctype + "\r\n\r\n").encode() tail = ("\r\n--" + boundary + "\r\n" 'Content-Disposition: form-data; name="overwrite"\r\n\r\ntrue\r\n' "--" + boundary + "--\r\n").encode() raw, _ = request("POST", "/upload/image", data=head + payload + tail, headers={"Content-Type": "multipart/form-data; boundary=" + boundary}, timeout=180) d = json.loads(raw) name = d.get("name") or d.get("filename") if not name: raise RuntimeError("上传首帧失败: " + raw.decode(errors="replace")[:500]) return name def build_workflow(image_name, prompt, width, height, frames, seed, steps, cfg, fps, prefix): """双专家 MoE:high_noise 跑前 2 步,low_noise 跑后 2 步,各挂对应 4-step LoRA。 节点图对齐 0.45(30113) 与 0.38(30112) 都能跑的官方模板: * ModelSamplingSD3 shift=5 必须挂(缺了画面发糊/漂移) * UNETLoader.weight_dtype="default" 用文件原生 fp8(别强制 fp8_e4m3fn 再转一次) * SaveVideo.format="auto"(该字段是动态组合,取值 auto/mp4/mkv/webm; 传 "video/h264-mp4" 这类组合串会被静默丢弃) """ half = steps // 2 return { "1": {"class_type": "UNETLoader", "inputs": {"unet_name": UNET_HIGH, "weight_dtype": "default"}}, "2": {"class_type": "UNETLoader", "inputs": {"unet_name": UNET_LOW, "weight_dtype": "default"}}, "3": {"class_type": "LoraLoaderModelOnly", "inputs": {"model": ["1", 0], "lora_name": LORA_HIGH, "strength_model": 1.0}}, "4": {"class_type": "LoraLoaderModelOnly", "inputs": {"model": ["2", 0], "lora_name": LORA_LOW, "strength_model": 1.0}}, "5": {"class_type": "CLIPLoader", "inputs": {"clip_name": CLIP, "type": "wan"}}, "6": {"class_type": "CLIPTextEncode", "inputs": {"text": prompt, "clip": ["5", 0]}}, "7": {"class_type": "CLIPTextEncode", "inputs": {"text": NEGATIVE, "clip": ["5", 0]}}, "8": {"class_type": "VAELoader", "inputs": {"vae_name": VAE}}, "9": {"class_type": "LoadImage", "inputs": {"image": image_name}}, "10": {"class_type": "ImageScale", "inputs": {"image": ["9", 0], "upscale_method": "lanczos", "width": width, "height": height, "crop": "center"}}, "11": {"class_type": "WanImageToVideo", "inputs": { "positive": ["6", 0], "negative": ["7", 0], "vae": ["8", 0], "width": width, "height": height, "length": frames, "batch_size": 1, "start_image": ["10", 0]}}, "12": {"class_type": "ModelSamplingSD3", "inputs": {"model": ["3", 0], "shift": 5.0}}, "13": {"class_type": "ModelSamplingSD3", "inputs": {"model": ["4", 0], "shift": 5.0}}, "14": {"class_type": "KSamplerAdvanced", "inputs": { "model": ["12", 0], "add_noise": "enable", "noise_seed": seed, "steps": steps, "cfg": cfg, "sampler_name": "euler", "scheduler": "simple", "positive": ["11", 0], "negative": ["11", 1], "latent_image": ["11", 2], "start_at_step": 0, "end_at_step": half, "return_with_leftover_noise": "enable"}}, "15": {"class_type": "KSamplerAdvanced", "inputs": { "model": ["13", 0], "add_noise": "disable", "noise_seed": seed, "steps": steps, "cfg": cfg, "sampler_name": "euler", "scheduler": "simple", "positive": ["11", 0], "negative": ["11", 1], "latent_image": ["14", 0], "start_at_step": half, "end_at_step": steps, "return_with_leftover_noise": "disable"}}, "16": {"class_type": "VAEDecode", "inputs": {"samples": ["15", 0], "vae": ["8", 0]}}, "17": {"class_type": "CreateVideo", "inputs": {"images": ["16", 0], "fps": fps}}, "18": {"class_type": "SaveVideo", "inputs": {"video": ["17", 0], "filename_prefix": prefix, "format": "auto"}}, } def submit(wf, client_id): body = json.dumps({"prompt": wf, "client_id": client_id}).encode() try: raw, _ = request("POST", "/prompt", data=body, headers={"Content-Type": "application/json"}, timeout=90) except urllib.error.HTTPError as e: detail = e.read().decode(errors="replace") raise RuntimeError("工作流校验失败 (HTTP %s): %s" % (e.code, detail[:3000])) d = json.loads(raw) if d.get("node_errors"): raise RuntimeError("节点错误: " + json.dumps(d["node_errors"], ensure_ascii=False)[:3000]) return d.get("prompt_id") def wait(prompt_id, timeout): t0 = time.time() gaps = 0 while True: try: d = get_json("/history/" + urllib.parse.quote(prompt_id), timeout=30) except Exception as e: gaps += 1 print(" ...服务暂不可达 (%d): %s" % (gaps, str(e)[:90]), flush=True) if gaps >= 8 or time.time() - t0 > timeout: raise TimeoutError("等待出片超时或服务重启") time.sleep(8) continue gaps = 0 item = d.get(prompt_id) if item: status = item.get("status", {}) if status.get("completed") or item.get("outputs"): if status.get("status_str") == "error": raise RuntimeError("渲染失败: " + json.dumps(status.get("messages", []), ensure_ascii=False)[:2000]) return item if time.time() - t0 > timeout: raise TimeoutError("等待出片超时 %s 秒" % timeout) time.sleep(4) def find_video(item): for out in (item.get("outputs") or {}).values(): for key in ("images", "videos", "gifs", "video"): for f in out.get(key, []) or []: if isinstance(f, dict) and f.get("filename", "").lower().endswith((".mp4", ".webm", ".mkv")): return f raise RuntimeError("未找到视频输出: " + json.dumps(item.get("outputs"), ensure_ascii=False)[:1500]) def download(f, out): q = urllib.parse.urlencode({"filename": f["filename"], "subfolder": f.get("subfolder", ""), "type": f.get("type", "output")}) raw, _ = request("GET", "/view?" + q, timeout=300) os.makedirs(os.path.dirname(os.path.abspath(out)) or ".", exist_ok=True) with open(out, "wb") as fh: fh.write(raw) return len(raw) def main(): ap = argparse.ArgumentParser() ap.add_argument("shots", help="30_shots.json 路径") ap.add_argument("--only", default="", help="只跑指定镜,逗号分隔") ap.add_argument("--force", action="store_true", help="已存在也重跑") ap.add_argument("--timeout", type=int, default=1800) ap.add_argument("--cleanup", action="store_true", help="导出后清理 ComfyUI 的 history/queue 记录与产物(等价于 cleanup.auto=true)") ap.add_argument("--no-cleanup", action="store_true", help="本次强制不清理,覆盖 config 里的 cleanup.auto") a = ap.parse_args() do_cleanup = (a.cleanup or CFG.get("cleanup", {}).get("auto", False)) and not a.no_cleanup shots_path = os.path.abspath(a.shots) data = json.load(open(shots_path, encoding="utf-8")) ep = data["episode"] base = os.path.dirname(shots_path) img_dir = os.path.join(ROOT, "work", ep, "images") vid_dir = os.path.join(ROOT, "work", ep, "videos") os.makedirs(vid_dir, exist_ok=True) log_path = os.path.join(vid_dir, "a14b_runs.json") runs = json.load(open(log_path, encoding="utf-8")) if os.path.exists(log_path) else {} print("ComfyUI:", health(), flush=True) width, height = VM["width"], VM["height"] frames, steps, fps = VM["frames"], VM["steps"], VM["fps"] wanted = set(x.strip() for x in a.only.split(",") if x.strip()) jobs = [s for s in data["shots"] if not wanted or s["shotId"] in wanted] for s in jobs: sid = s["shotId"] image = os.path.join(img_dir, sid + ".png") prompt_file = os.path.join(base, "40_prompts", sid + ".i2v.txt") if not os.path.exists(image): print("SKIP %s: 缺首帧 %s" % (sid, image), file=sys.stderr) continue if not os.path.exists(prompt_file): print("SKIP %s: 缺运动提示词 %s" % (sid, prompt_file), file=sys.stderr) continue prompt = open(prompt_file, encoding="utf-8").read().strip() h8 = hashlib.sha256(prompt.encode("utf-8")).hexdigest()[:8] out = os.path.join(vid_dir, "%s_%s.mp4" % (sid, h8)) if os.path.exists(out) and os.path.getsize(out) > 100000 and not a.force: print("SKIP %s: 已存在 %s" % (sid, os.path.basename(out)), flush=True) continue seed = int(hashlib.sha256((sid + prompt).encode()).hexdigest()[:8], 16) % (2 ** 31) t0 = time.time() print("[%s] %s -> %s | %dx%d %d帧 seed=%d" % (time.strftime("%H:%M:%S"), sid, os.path.basename(out), width, height, frames, seed), flush=True) name = upload_image(image) wf = build_workflow(name, prompt, width, height, frames, seed, steps, 1.0, fps, "video/xv_%s_%s" % (ep, sid)) # client_id 会被 ComfyUI 持久化在 history 项 prompt[3],是清理时的归属凭证 cid = "manju-i2v-" + uuid.uuid4().hex[:8] pid = submit(wf, cid) item = wait(pid, a.timeout) size = download(find_video(item), out) runs[sid] = {"out": os.path.relpath(out, ROOT), "promptHash": h8, "seed": seed, "promptId": pid, "clientId": cid, "image": os.path.relpath(image, ROOT), "width": width, "height": height, "frames": frames, "fps": fps, "steps": steps, "seconds": round(time.time() - t0, 1), "bytes": size, "at": time.strftime("%Y-%m-%d %H:%M:%S")} with open(log_path, "w", encoding="utf-8") as f: json.dump(runs, f, ensure_ascii=False, indent=2) print("[ok] %s %.2fMB in %.1fs" % (sid, size / 1048576, time.time() - t0), flush=True) if do_cleanup: try: sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from cleanup_comfyui import after_export print("[cleanup] " + after_export(ROOT, pid, HOST, log_path, sid, out, cid), flush=True) except Exception as e: # 成片已安全落地;清理失败只提示,绝不影响主流程 print("[cleanup] FAILED %s(成片已落地,可稍后跑 cleanup_comfyui.py 补清)" % e, file=sys.stderr, flush=True) print("DONE %d/%d" % (sum(1 for s in jobs if s["shotId"] in runs), len(jobs)), flush=True) return 0 if __name__ == "__main__": try: sys.exit(main()) except Exception as e: print("ERROR: %s" % e, file=sys.stderr, flush=True) sys.exit(1)