#!/usr/bin/env python3 """xv 批量审片:从 A14B 片段抽首/中/末帧,按九宫格联系表分组。 用法: python3 tools/review.py projects/EPxx/30_shots.json --group 1 # S01-S09 联系表 python3 tools/review.py projects/EPxx/30_shots.json --group 2 # S10-S18 联系表 python3 tools/review.py projects/EPxx/30_shots.json --only S13,S15 联系表每格 = 一段视频的首/中/末三帧横排,便于看漂移轨迹; 9 段一表(3 行 × 3 列),输出到 work//review/contact_G.png。 """ import argparse import json import os import subprocess import sys 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() def ff(): import imageio_ffmpeg return imageio_ffmpeg.get_ffmpeg_exe() def run(args): r = subprocess.run([ff(), "-hide_banner", "-loglevel", "error", "-y"] + args, capture_output=True, text=True) if r.returncode != 0: print("FFMPEG FAILED:\n" + r.stderr[-2000:], file=sys.stderr) raise SystemExit(1) return r def frame(path, at, out): run(["-ss", "%.2f" % at, "-i", path, "-frames:v", "1", "-vf", "scale=180:-1", out]) def main(): ap = argparse.ArgumentParser() ap.add_argument("shots") ap.add_argument("--group", type=int, default=0, help="1=S01-S09, 2=S10-S18, 0=不限配合 --only") ap.add_argument("--only", default="", help="逗号分隔镜号") a = ap.parse_args() shots_path = os.path.abspath(a.shots) data = json.load(open(shots_path, encoding="utf-8")) ep = data["episode"] vid_dir = os.path.join(ROOT, "work", ep, "videos") rev_dir = os.path.join(ROOT, "work", ep, "review") os.makedirs(rev_dir, exist_ok=True) runs = json.load(open(os.path.join(vid_dir, "a14b_runs.json"), encoding="utf-8")) wanted = set(x.strip() for x in a.only.split(",") if x.strip()) if a.group == 1: wanted = {"S%02d" % i for i in range(1, 10)} elif a.group == 2: wanted = {"S%02d" % i for i in range(10, 19)} cells, labels = [], [] for s in data["shots"]: sid = s["shotId"] if sid not in wanted: continue rec = runs.get(sid) if not rec: print("SKIP %s: 无产物记录" % sid, flush=True) continue src = os.path.join(ROOT, rec["out"]) if not os.path.exists(src): print("SKIP %s: 文件不存在" % sid, flush=True) continue d = float(s["duration"]) cell_frames = [] for k, at in enumerate((0.4, d / 2, d - 0.4)): f = os.path.join(rev_dir, "_%s_f%d.png" % (sid, k)) frame(src, at, f) cell_frames.append(f) # 三帧横排成一行 row = os.path.join(rev_dir, "_%s_row.png" % sid) ins = [] for f in cell_frames: ins += ["-i", f] run(ins + ["-filter_complex", "[0:v][1:v][2:v]hstack=inputs=3[r]", "-map", "[r]", row]) cells.append(row) labels.append(sid) print(" %s -> 3 帧" % sid, flush=True) if not cells: print("无可审片片段", file=sys.stderr) return 1 # every 9 cells -> one 3x3 contact sheet batch = a.group or 1 for start in range(0, len(cells), 9): part = cells[start:start + 9] tag = "G%d" % batch if a.group else "custom" if a.group: batch += 1 while len(part) < 9: part.append(part[-1]) # 补齐 3x3,重复最后格 ins = [] for p in part: ins += ["-i", p] fc = ("[0:v][1:v][2:v]vstack=inputs=3[r0];" "[3:v][4:v][5:v]vstack=inputs=3[r1];" "[6:v][7:v][8:v]vstack=inputs=3[r2];" "[r0][r1][r2]vstack=inputs=3[out]") out = os.path.join(rev_dir, "contact_%s.png" % tag) run(ins + ["-filter_complex", fc, "-map", "[out]", out]) print("联系表:", os.path.relpath(out, ROOT), "| 格顺序:", " ".join(labels[start:start + 9]), flush=True) return 0 if __name__ == "__main__": sys.exit(main())