#!/usr/bin/env python3 """manju H3AV 排队器:把整集台词镜头一次性推进 ComfyUI 队列(只提交、不等待)。 为什么分成「排队 / 取回」两步:单镜约 90 分钟,一集 8 镜 ≈ 12 小时。 本地进程挂在等待上没有任何收益 —— 会话被回收、终端断掉,任务还在服务端跑,pid 却丢了。 所以:一次排完 → 落盘 prompt_id → 任意时刻 fetch_h3_native.py 取回。 用法: python3 tools/queue_h3_native.py --ep EP01 # 排全部带 .h3v3.txt 的镜 python3 tools/queue_h3_native.py --ep EP01 --only S01a S02 python3 tools/queue_h3_native.py --ep EP01 --check # 只做丢失自检,不提交 ★ 交完必须自检(脚本最后自动做):/queue 里找不到、history 里也没有 = 任务已丢。 queue 与 history 都是内存态,实例重启会静默清空 —— 见 references/native-av.md §6。 """ import argparse import hashlib import json import os import re import sys import time import uuid sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import submit_h3_native as H # noqa: E402 def load_shots(root, ep): p = os.path.join(root, "projects", ep, "30_shots.json") if not os.path.isfile(p): raise SystemExit("找不到分镜表:%s" % p) d = json.load(open(p, encoding="utf-8")) return d.get("shots", d) if isinstance(d, dict) else d def find_prompt(root, ep, sid): base = os.path.join(root, "projects", ep, "40_prompts", sid) for ext in (".h3v3.txt", ".h3.txt"): if os.path.isfile(base + ext): return base + ext return None def find_first(root, ep, sid): """优先用分镜表里的 firstFrame,其次 work//images/.png,再退回去掉尾字母。""" cand = [os.path.join(root, "work", ep, "images", sid + ".png"), os.path.join(root, "work", ep, "images", re.sub(r"[a-z]$", "", sid) + ".png")] return next((p for p in cand if os.path.isfile(p)), None) def queue_pids(host): try: q = H.getj(host, "/queue", timeout=20) except Exception as e: print("读取队列失败(继续):", str(e)[:80], flush=True) return set() return {str(it[1]) for it in (q.get("queue_running") or []) + (q.get("queue_pending") or [])} def selfcheck(host, m, only_new=None): """排队/取回前后都能跑的丢失自检。返回 (在队, 已完成, 丢失)。""" live = queue_pids(host) q_ok = q_done = lost = 0 for sid in sorted(m): pid = m[sid].get("prompt_id") if not pid or (only_new and sid not in only_new): continue if pid in live: q_ok += 1 continue try: hh = H.getj(host, "/history/" + pid, timeout=20) except Exception: q_ok += 1 # 网络问题不下结论 continue if pid in hh and hh[pid].get("status", {}).get("status_str") == "success": q_done += 1 elif pid in hh: # 有 history 记录但非 success:被中断/报错,不能算"在队" _st = hh[pid].get("status", {}).get("status_str", "?") lost += 1 print(" ⛔ %-5s pid=%s 状态=%s(非 success,疑似被中断/失败)→ 需 --force 重排" % (sid, pid[:8], _st), flush=True) else: lost += 1 print(" ⛔ %-5s pid=%s 既不在队列也不在 history → 已随实例重启丢失,需重排" % (sid, pid[:8]), flush=True) return q_ok, q_done, lost def main(): ap = argparse.ArgumentParser() ap.add_argument("--ep", required=True) ap.add_argument("--only", nargs="*", default=None) ap.add_argument("--force", action="store_true", help="已出片/已提交的也重排(换提示词后用)") ap.add_argument("--check", action="store_true", help="只做丢失自检,不提交") ap.add_argument("--w", type=int, default=0) ap.add_argument("--h", type=int, default=0) ap.add_argument("--steps", type=int, default=0) a = ap.parse_args() root = H.find_root() cfg = H.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)) outdir = os.path.join(root, "work", a.ep, "videos", "native") os.makedirs(outdir, exist_ok=True) mapfile = os.path.join(outdir, "_queue_map.json") m = {} if os.path.isfile(mapfile): try: m = json.load(open(mapfile, encoding="utf-8")) except Exception: m = {} if a.check: q_ok, q_done, lost = selfcheck(host, m) print("\n在队 %d / 已完成 %d / 丢失 %d" % (q_ok, q_done, lost)) return 0 if lost == 0 else 2 weights = H.pick_weights(host) # 一次探测,全批复用 live = queue_pids(host) new = set() for s in load_shots(root, a.ep): sid = s.get("shotId") or s.get("id") if not sid or (a.only and sid not in a.only): continue pf = find_prompt(root, a.ep, sid) if not pf: print("[skip] %-5s 没有 .h3v3.txt 提示词" % sid, flush=True) continue out = os.path.join(outdir, sid + ".mp4") if os.path.exists(out) and not a.force: print("[skip] %-5s 已出片 %.1f MB" % (sid, os.path.getsize(out) / 1e6), flush=True) continue prev = m.get(sid, {}).get("prompt_id") if prev and not a.force: if prev in live: print("[skip] %-5s 已在本机队列 pid=%s" % (sid, prev[:8]), flush=True) continue try: hh = H.getj(host, "/history/" + prev, timeout=20) if prev in hh and hh[prev].get("status", {}).get("status_str") == "success": print("[skip] %-5s 服务端已成功(用 fetch 取回)" % sid, flush=True) continue except Exception: pass print("[skip] %-5s 已提交过 pid=%s(如已随重启丢失,加 --force 重排)" % (sid, prev[:8]), flush=True) continue img = find_first(root, a.ep, sid) if not img: print("[skip] %-5s 没有首帧图(要纯文生视频用 submit_h3_native.py --t2v)" % sid, flush=True) continue prompt = open(pf, encoding="utf-8").read().strip() secs = float(s.get("duration") or 7.0) length = H.snap_len(round(secs * fps)) if length < 124: print(" ⚠ %-5s %.2fs → %d 帧,低于训练区间 124 帧,画质会掉;" "建议把这场戏并到相邻镜" % (sid, secs, length), flush=True) image_name = H.upload(host, img) seed = int(hashlib.sha256((sid + prompt).encode()).hexdigest()[:8], 16) % (2 ** 31) graph = H.build(weights, image_name, prompt, W, Hh, length, seed, steps, True, True, fps, cfg.get("prefix", "video/h3k")) pid, num = H.submit(host, graph, "manju-h3av-" + uuid.uuid4().hex[:8]) m[sid] = {"prompt_id": pid, "first": os.path.relpath(img, root), "promptFile": os.path.relpath(pf, root), "seconds": secs, "length": length, "w": W, "h": Hh, "steps": steps, "seed": seed, "endpoint": host, "submitted_at": time.strftime("%Y-%m-%d %H:%M:%S")} json.dump(m, open(mapfile, "w", encoding="utf-8"), ensure_ascii=False, indent=2) new.add(sid) print("[queue] %-5s pid=%s 队列#%s %dx%d %d帧=%.2fs" % (sid, pid, num, W, Hh, length, length / float(fps)), flush=True) time.sleep(1.5) total = sum(v.get("length", 0) for v in m.values()) / float(fps) print("\n本次新排 %d 镜,累计 %d 镜 / %.1f 秒成片;单镜约 90 分钟," "按串行估 ≈ %.1f 小时" % (len(new), len(m), total, len(m) * 1.5), flush=True) print("映射表 ->", mapfile, flush=True) print("\n丢失自检(在队=队列有任务;丢失=重启清掉或被中断/error):", flush=True) q_ok, q_done, lost = selfcheck(host, m) print("在队 %d / 已完成 %d / 丢失 %d" % (q_ok, q_done, lost), flush=True) if lost: print("⚠ 有任务丢失/失败:加 --force 重排;error 状态多为被中断,核对是否有人动了队列", flush=True) return 0 if __name__ == "__main__": sys.exit(main())