#!/usr/bin/env python3 """manju H3AV 取回器:把 queue_h3_native.py 排队的产物拉回本地。 可反复跑:服务端成功一个就下一个,已下载的跳过。不等待、不阻塞。 用法: python3 tools/fetch_h3_native.py --ep EP01 # 取回所有已完成的 python3 tools/fetch_h3_native.py --ep EP01 --status # 只看状态 python3 tools/fetch_h3_native.py --ep EP01 --only S04 落盘 work//videos/native/.mp4,并顺手检查 mp4 里是否真有 aac 音轨 (没有音轨说明节点图串错了 —— 台词镜头必须是音画同出)。 """ import argparse import json import os import sys import time import urllib.parse sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import submit_h3_native as H # noqa: E402 MIN_BYTES = 20000 def has_audio(path): """mp4 里找 mp4a/esds box —— 足够判断有没有音轨,不引第三方库。""" try: with open(path, "rb") as f: blob = f.read() except Exception: return None return (b"mp4a" in blob) or (b"esds" in blob) def main(): ap = argparse.ArgumentParser() ap.add_argument("--ep", required=True) ap.add_argument("--only", nargs="*", default=None) ap.add_argument("--status", action="store_true") a = ap.parse_args() root = H.find_root() cfg = H.load_cfg(root) host = cfg["endpoint"] outdir = os.path.join(root, "work", a.ep, "videos", "native") mapfile = os.path.join(outdir, "_queue_map.json") if not os.path.isfile(mapfile): print("没有排队映射表,先跑 queue_h3_native.py --ep %s" % a.ep) return 0 m = json.load(open(mapfile, encoding="utf-8")) os.makedirs(outdir, exist_ok=True) done = fail = pend = 0 for sid in sorted(m): if a.only and sid not in a.only: continue info = m[sid] pid = info["prompt_id"] out = os.path.join(outdir, sid + ".mp4") if os.path.exists(out) and os.path.getsize(out) > MIN_BYTES: au = has_audio(out) print("%-5s 已有 %.1f MB 音轨=%s" % (sid, os.path.getsize(out) / 1e6, "有" if au else "无⚠")) done += 1 continue try: hh = H.getj(host, "/history/" + urllib.parse.quote(pid), timeout=25) except Exception as e: print("%-5s history 读取失败 %s" % (sid, str(e)[:60])) pend += 1 continue item = hh.get(pid) if not item: print("%-5s 排队/运行中(若队列为空则是已丢失,见 --check)" % sid) pend += 1 continue st = item.get("status", {}) if any(isinstance(x, list) and x and x[0] == "execution_error" for x in st.get("messages", [])): print("%-5s 失败" % sid) fail += 1 continue if not (st.get("completed") or st.get("status_str") == "success"): print("%-5s 未完成 (%s)" % (sid, st.get("status_str"))) pend += 1 continue f = H.find_video(item) if a.status: print("%-5s 服务端已就绪 %s" % (sid, f.get("filename"))) done += 1 continue q = urllib.parse.urlencode({"filename": f["filename"], "subfolder": f.get("subfolder", ""), "type": f.get("type", "output")}) raw, _ = H.req(host, "GET", "/view?" + q, timeout=1200) open(out, "wb").write(raw) au = has_audio(out) print("%-5s 已下载 %.1f MB 音轨=%s -> %s" % (sid, len(raw) / 1e6, "有" if au else "无⚠", os.path.relpath(out, root))) info.update({"output": os.path.relpath(out, root), "bytes": len(raw), "audio_track": bool(au), "fetched_at": time.strftime("%Y-%m-%d %H:%M:%S")}) json.dump(m, open(mapfile, "w", encoding="utf-8"), ensure_ascii=False, indent=2) done += 1 print("\n完成 %d / 未就绪 %d / 失败 %d" % (done, pend, fail)) return 0 if __name__ == "__main__": sys.exit(main())