Files
ai-manju/scripts/submit_h3_native.py
T
ssw d25c9304e1 feat: 原生音画同出闭环 + 队列自检修复 (v1.2.0)
- 新增 render_native.py: 台词镜缝合(保留原生音轨+烧h3v3字幕+瑕疵遮盖,不叠TTS)
- 新增 fetch_t2i.py: 出图脚本被杀后按pid救回单图
- 修复 queue_h3_native.py --check 将中断/error误判为在队的bug
- 文档: render.md/native-av.md/pitfalls.md/SKILL.md/README 补齐双轨缝合与选机实测
2026-11-19 10:18:38 +08:00

381 lines
16 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""manju H3AV:MiniMax H3 原生音画同出提交器(台词镜头的正路)。
一条节点图同时出画面 + 人声 + 环境音,口型由模型原生对齐 —— 不做事后 lip-sync。
零第三方依赖;读 config/pipeline.json 的 nativeVideoModel;明文 HTTP 必须绕代理。
用法:
python3 tools/submit_h3_native.py --ep EP01 --shot S04 --seconds 7.3
python3 tools/submit_h3_native.py --ep EP01 --shot S01b --length 141 --prefix S01b_take2
python3 tools/submit_h3_native.py --ep EP01 --shot S05 --t2v # 无首帧(纯文生视频)
约定:
- 提示词默认读 projects/<EP>/40_prompts/<SHOT>.h3v3.txt(五段式,见 references/native-av.md §5)
- 首帧默认读 work/<EP>/images/<SHOT>.png;找不到则去掉镜号尾字母再试(S01b → S01.png)
- 产物落 work/<EP>/videos/native/<SHOT>.mp4,台账 _h3av_ledger.json 同目录
★ 只能在 64 GB 卡上跑(实测 192.168.1.23:30121 / K100_AI)。
2080Ti 22 GB 上给 MiniMaxH3ImageToVideo 塞 first_frame 必 CUDA OOM —— 见 references/models.md §0。
★ length 必须吸附到 17k+5 网格,训练区间 124–362 帧;<124 帧画质明显掉。
★ 单镜 ≈ 90 min(175 帧 / 576×1024 / 20 步)。长任务请用 queue_h3_native.py 排队,别挂进程等。
★ 这台机器调 /api/free 会崩服务,禁止调用。
"""
import argparse
import hashlib
import json
import mimetypes
import os
import re
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
import uuid
DEFAULTS = {
"endpoint": "http://192.168.1.23:30121",
"width": 576,
"height": 1024,
"fps": 24,
"steps": 20,
"timeout": 7200,
"prefix": "video/h3k",
}
# 权重按端点 /object_info 自动挑,不写死 —— 端点换权重不用改代码。
PICK = {
"unet": ("fl2va",), # H3 视频(图/文 + 音频联合)
"clip": ("qwen3vl", "minimax_h3"),
"vae_video": ("video_vae",),
"vae_audio": ("audio_vae",),
}
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
# ---------------------------------------------------------------- 路径 / 配置
def find_root():
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__)))
def load_cfg(root):
cfg = dict(DEFAULTS)
p = os.path.join(root, "config", "pipeline.json")
if os.path.isfile(p):
full = json.load(open(p, encoding="utf-8"))
cfg.update(full.get("nativeVideoModel", {}))
cfg["_full"] = full
return cfg
# ---------------------------------------------------------------- HTTP
def req(host, 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 getj(host, path, timeout=90):
return json.loads(req(host, "GET", path, timeout=timeout)[0])
def pick_weights(host):
"""按 PICK 规则在目标端点挑权重;挑不到就明确报错,不猜。"""
out = {}
for key, need in (("unet", PICK["unet"]), ("clip", PICK["clip"]),
("vae_video", PICK["vae_video"]), ("vae_audio", PICK["vae_audio"])):
node = {"unet": "UNETLoader", "clip": "CLIPLoader",
"vae_video": "VAELoader", "vae_audio": "VAELoader"}[key]
field = {"unet": "unet_name", "clip": "clip_name"}.get(key, "vae_name")
names = getj(host, "/object_info/" + node, timeout=40)
cand = names[node]["input"]["required"][field][0]
hit = [n for n in cand if all(s.lower() in n.lower() for s in need)]
if not hit:
raise SystemExit("端点缺权重(%s 里找不到 %s):%s\n去 /object_info/%s 看清单,"
"别猜权重名" % (node, "/".join(need), cand, node))
out[key] = hit[0]
return out
def upload(host, path):
boundary = "----manju" + 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, _ = req(host, "POST", "/upload/image", data=head + payload + tail,
headers={"Content-Type": "multipart/form-data; boundary=" + boundary},
timeout=300)
d = json.loads(raw)
name = d.get("name") or d.get("filename")
if not name:
raise RuntimeError("上传失败 " + raw.decode(errors="replace")[:300])
return name
# ---------------------------------------------------------------- 节点图
def snap_len(n):
"""H3 length 吸附到 17k+5 网格(124 = 17*7+5),向上取整保证台词不被切尾。"""
n = max(5, int(n))
k = -(-(n - 5) // 17)
return int(17 * k + 5)
def build(w, image_name, prompt, width, height, length, seed, steps=20,
has_first=True, audio=True, fps=24, prefix="video/h3k"):
g, c = {}, [0]
def nid():
c[0] += 1
return str(c[0])
n = {}
if has_first:
n["first"] = nid()
g[n["first"]] = {"class_type": "LoadImage", "inputs": {"image": image_name}}
n["clip"] = nid()
g[n["clip"]] = {"class_type": "CLIPLoader",
"inputs": {"clip_name": w["clip"], "type": "minimax", "device": "default"}}
n["vv"] = nid()
g[n["vv"]] = {"class_type": "VAELoader", "inputs": {"vae_name": w["vae_video"]}}
n["unet"] = nid()
g[n["unet"]] = {"class_type": "UNETLoader",
"inputs": {"unet_name": w["unet"], "weight_dtype": "default"}}
itv = {"clip": [n["clip"], 0], "vae": [n["vv"], 0], "prompt": prompt,
"width": width, "height": height, "length": length}
if has_first:
itv["first_frame"] = [n["first"], 0]
n["cond"] = nid()
g[n["cond"]] = {"class_type": "MiniMaxH3ImageToVideo", "inputs": itv}
n["g"] = nid()
g[n["g"]] = {"class_type": "BasicGuider",
"inputs": {"model": [n["unet"], 0], "conditioning": [n["cond"], 0]}}
n["smp"] = nid()
g[n["smp"]] = {"class_type": "KSamplerSelect", "inputs": {"sampler_name": "res_multistep"}}
n["sch"] = nid()
g[n["sch"]] = {"class_type": "BasicScheduler",
"inputs": {"model": [n["unet"], 0], "scheduler": "simple",
"steps": steps, "denoise": 1.0}}
n["rnd"] = nid()
g[n["rnd"]] = {"class_type": "RandomNoise", "inputs": {"noise_seed": int(seed)}}
n["samp"] = nid()
g[n["samp"]] = {"class_type": "SamplerCustomAdvanced",
"inputs": {"noise": [n["rnd"], 0], "guider": [n["g"], 0],
"sampler": [n["smp"], 0], "sigmas": [n["sch"], 0],
"latent_image": [n["cond"], 1]}}
n["dec"] = nid()
g[n["dec"]] = {"class_type": "VAEDecode",
"inputs": {"samples": [n["samp"], 0], "vae": [n["vv"], 0]}}
if audio:
n["va"] = nid()
g[n["va"]] = {"class_type": "VAELoader", "inputs": {"vae_name": w["vae_audio"]}}
n["deca"] = nid()
g[n["deca"]] = {"class_type": "VAEDecodeAudio",
"inputs": {"samples": [n["samp"], 0], "vae": [n["va"], 0]}}
n["cv"] = nid()
g[n["cv"]] = {"class_type": "CreateVideo",
"inputs": {"images": [n["dec"], 0], "audio": [n["deca"], 0],
"fps": fps, "bit_depth": 8, "color_space": "sRGB"}}
else:
n["cv"] = nid()
g[n["cv"]] = {"class_type": "CreateVideo",
"inputs": {"images": [n["dec"], 0], "fps": fps,
"bit_depth": 8, "color_space": "sRGB"}}
n["sv"] = nid()
g[n["sv"]] = {"class_type": "SaveVideo",
"inputs": {"filename_prefix": prefix, "format": "auto",
"video": [n["cv"], 0]}}
return g
def submit(host, graph, cid):
body = json.dumps({"prompt": graph, "client_id": cid}).encode()
try:
raw, _ = req(host, "POST", "/prompt", data=body,
headers={"Content-Type": "application/json"}, timeout=120)
except urllib.error.HTTPError as e:
raise RuntimeError("校验失败 %s: %s" % (e.code, e.read().decode(errors="replace")[:2000]))
d = json.loads(raw)
if d.get("node_errors"):
raise RuntimeError("节点错误 " + json.dumps(d["node_errors"], ensure_ascii=False)[:2000])
return d.get("prompt_id"), d.get("number")
def find_video(item):
for out in (item.get("outputs") or {}).values():
for key in ("videos", "video", "gifs", "images"):
for f in out.get(key, []) or []:
if isinstance(f, dict) and f.get("filename", "").lower().endswith(
(".mp4", ".webm", ".mkv", ".mov")):
return f
raise RuntimeError("未找到视频 " + json.dumps(item.get("outputs"), ensure_ascii=False)[:600])
def wait(host, pid, timeout, tag=""):
t0, gaps, last = time.time(), 0, ""
while True:
try:
d = getj(host, "/history/" + urllib.parse.quote(pid), timeout=30)
gaps = 0
except Exception as e: # 服务重启 / 网络抖动
gaps += 1
print(" [%s] 暂不可达(%d): %s" % (tag, gaps, str(e)[:70]), flush=True)
if gaps >= 12 or time.time() - t0 > timeout:
raise TimeoutError("等待超时或服务重启;任务可能已随重启丢失,"
"先查 /queue 再决定是否重排")
time.sleep(10)
continue
item = d.get(pid)
if item:
st = item.get("status", {})
msgs = st.get("messages", [])
if any(isinstance(m, list) and m and m[0] == "execution_error" for m in msgs):
raise RuntimeError("渲染失败 " + json.dumps(msgs, ensure_ascii=False)[:1500])
if st.get("completed") or st.get("status_str") == "success":
if item.get("outputs"):
return item
raise RuntimeError("完成但无产物 " + json.dumps(msgs, ensure_ascii=False)[:600])
el = time.time() - t0
cur = "已跑 %d 分 %02d 秒" % (el // 60, el % 60)
if cur != last and int(el) % 60 < 6:
print(" [%s] %s" % (tag, cur), flush=True)
last = cur
if el > timeout:
raise TimeoutError("超时 %ss(任务可能仍在服务端跑,先查 /queue)" % timeout)
time.sleep(8)
# ---------------------------------------------------------------- main
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--ep", required=True, help="集号,如 EP01")
ap.add_argument("--shot", required=True, help="镜号,如 S04 / S01b")
ap.add_argument("--prompt-file", default=None, help="默认 projects/<EP>/40_prompts/<SHOT>.h3v3.txt")
ap.add_argument("--first", default=None, help="首帧图,默认 work/<EP>/images/<SHOT>.png")
ap.add_argument("--w", type=int, default=0)
ap.add_argument("--h", type=int, default=0)
ap.add_argument("--length", type=int, default=0, help="帧数;会吸附到 17k+5")
ap.add_argument("--seconds", type=float, default=0.0, help="目标时长(秒),按 fps 吸附")
ap.add_argument("--steps", type=int, default=0)
ap.add_argument("--seed", type=int, default=None)
ap.add_argument("--prefix", default=None, help="本地文件名前缀,默认镜号;SaveVideo 前缀用 cfg.prefix")
ap.add_argument("--no-audio", action="store_true", help="不接音频 VAE(调试用,成片别用)")
ap.add_argument("--t2v", action="store_true", help="不给首帧(纯文生视频)")
ap.add_argument("--timeout", type=int, default=0)
a = ap.parse_args()
root = find_root()
cfg = load_cfg(root)
host = cfg["endpoint"]
fps = int(cfg.get("fps", 24))
W = a.w or int(cfg.get("width", 576))
Hh = a.h or int(cfg.get("height", 1024))
steps = a.steps or int(cfg.get("steps", 20))
# 时长:--length 优先,其次 --seconds,默认 175 帧(7.29 s)
if a.length:
length = snap_len(a.length)
elif a.seconds:
length = snap_len(round(a.seconds * fps))
else:
length = snap_len(round(7.3 * fps))
# 提示词
pf = a.prompt_file
if not pf:
base = os.path.join(root, "projects", a.ep, "40_prompts", a.shot)
for ext in (".h3v3.txt", ".h3.txt"):
if os.path.isfile(base + ext):
pf = base + ext
break
if not pf or not os.path.isfile(pf):
raise SystemExit("找不到提示词文件:%s(五段式模板见 references/native-av.md §5)" % pf)
prompt = open(pf, encoding="utf-8").read().strip()
# 首帧
has_first = not a.t2v
img = a.first
if has_first and not img:
cand = [os.path.join(root, "work", a.ep, "images", a.shot + ".png"),
os.path.join(root, "work", a.ep, "images", re.sub(r"[a-z]$", "", a.shot) + ".png")]
img = next((p for p in cand if os.path.isfile(p)), cand[0])
if has_first and not os.path.isfile(img):
raise SystemExit("首帧图不存在:%s(先出图,或 --t2v)" % img)
weights = pick_weights(host)
image_name = upload(host, img) if has_first else None
seed = a.seed if a.seed is not None else \
int(hashlib.sha256((a.shot + prompt).encode()).hexdigest()[:8], 16) % (2 ** 31)
graph = build(weights, image_name, prompt, W, Hh, length, seed, steps,
has_first, not a.no_audio, fps, cfg.get("prefix", "video/h3k"))
cid = "manju-h3av-" + uuid.uuid4().hex[:8]
pid, num = submit(host, graph, cid)
outdir = os.path.join(root, "work", a.ep, "videos", "native")
os.makedirs(outdir, exist_ok=True)
print("[%s] 提交 %s pid=%s 队列#%s %dx%d %d帧=%.2fs %d步 I2V=%s 音频=%s"
% (time.strftime("%H:%M:%S"), a.shot, pid, num, W, Hh, length,
length / float(fps), steps, has_first, not a.no_audio), flush=True)
print(" ⚠ 单镜约 90 分钟;本进程退出不影响服务端,可用 fetch_h3_native.py 取回", flush=True)
t0 = time.time()
item = wait(host, pid, a.timeout or cfg.get("timeout", 7200), a.shot)
elapsed = time.time() - t0
f = find_video(item)
q = urllib.parse.urlencode({"filename": f["filename"],
"subfolder": f.get("subfolder", ""),
"type": f.get("type", "output")})
raw, _ = req(host, "GET", "/view?" + q, timeout=1200)
out = os.path.join(outdir, (a.prefix or a.shot) + ".mp4")
open(out, "wb").write(raw)
print("[%s] OK -> %s %.2f MB 耗时 %.1f 分钟"
% (time.strftime("%H:%M:%S"), out, len(raw) / 1e6, elapsed / 60), flush=True)
led = os.path.join(outdir, "_h3av_ledger.json")
data = {}
if os.path.isfile(led):
try:
data = json.load(open(led, encoding="utf-8"))
except Exception:
data = {}
data[a.shot] = {"prompt_id": pid, "clientId": cid, "endpoint": host,
"promptFile": os.path.relpath(pf, root),
"first": os.path.relpath(img, root) if has_first else None,
"w": W, "h": Hh, "length": length, "fps": fps,
"seconds": round(length / float(fps), 3), "steps": steps,
"seed": seed, "audio": not a.no_audio,
"output": os.path.relpath(out, root), "bytes": len(raw),
"elapsed_min": round(elapsed / 60, 1),
"finished_at": time.strftime("%Y-%m-%d %H:%M:%S")}
json.dump(data, open(led, "w", encoding="utf-8"), ensure_ascii=False, indent=2)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n已中断(服务端任务可能仍在跑;用 /queue 查 pid 后再决定重排)", file=sys.stderr)
sys.exit(130)
except Exception as e:
print("ERROR:", e, file=sys.stderr, flush=True)
sys.exit(1)