#!/usr/bin/env python3 """manju 缝合器:已采用片段 + 配音 + 字幕 + HUD 标注 + 落版 -> 成片。 规格 720x1280 / 30fps / H.264+AAC。字幕取 30_shots.json 的 line 原文, 按标点折 1–3 行;HUD 文字全片只标一次(一横一竖);落版 zoompan 缓推。 去水印用 crop 底部带 + 居中填充,不用 drawbox。 用法: python3 tools/render.py projects/EPxx/30_shots.json """ import argparse import json import os import re 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() CFG = json.load(open(os.path.join(ROOT, "config", "pipeline.json"), encoding="utf-8")) OUT = CFG["output"] def ff(): import imageio_ffmpeg return imageio_ffmpeg.get_ffmpeg_exe() def run(args, quiet=True): cmd = [ff(), "-hide_banner", "-loglevel", "error", "-y"] + args r = subprocess.run(cmd, capture_output=True, text=True) if r.returncode != 0: print("FFMPEG FAILED:\n" + " ".join(cmd) + "\n" + r.stderr[-3000:], file=sys.stderr) raise SystemExit(1) return r def probe(path): r = subprocess.run([ff(), "-hide_banner", "-i", str(path)], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True) m = re.search(r"Duration:\s*(\d+):(\d+):([\d.]+)", r.stderr) dur = (int(m.group(1)) * 3600 + int(m.group(2)) * 60 + float(m.group(3))) if m else None return dur, r.stderr def ts(t): h = int(t // 3600) m = int(t % 3600 // 60) s = t % 60 return "%d:%02d:%05.2f" % (h, m, s) PUNCT = set(",。;!?、,.;!?") def wrap_line(text, maxlen=11): """按标点折 1–3 行;标点永不落单,永远附着在前一行末尾。""" parts = [p for p in re.split(r"([,。;!?、,.;!?])", text) if p] lines, cur = [], "" for p in parts: if p in PUNCT: # 标点直接并入当前行(哪怕略超 maxlen),绝不起新行 cur += p elif len(cur) + len(p) <= maxlen or not cur: cur += p else: lines.append(cur) cur = p if cur: lines.append(cur) # 若最后一行只有标点(理论上不会发生),并入上一行 if len(lines) > 1 and all(c in PUNCT for c in lines[-1]): lines[-2] += lines.pop() if len(lines) > 3: # 极端长句:均分三段,切点尽量落在标点后 n = len(text) cuts = [i + 1 for i, c in enumerate(text) if c in PUNCT] best = None for a in cuts: for b in cuts: if a < b < n and max(a, b - a, n - b) < (best[0] if best else 999): best = (max(a, b - a, n - b), a, b) if best and best[0] <= maxlen + 3: _, a, b = best lines = [text[:a], text[a:b], text[b:]] else: step = maxlen lines = [text[i:i + step] for i in range(0, n, step)][:3] return "\\N".join(lines) ASS_HEAD = """[Script Info] ScriptType: v4.00+ PlayResX: 720 PlayResY: 1280 ScaledBorderAndShadow: yes WrapStyle: 2 [V4+ Styles] Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding Style: Default,PingFang SC,46,&H00FFFFFF,&H00FFFFFF,&H000C1420,&H90000000,0,0,0,0,100,100,0,0,1,3,1,2,60,60,150,1 Style: HUD,PingFang SC,26,&H00E8D4A8,&H00E8D4A8,&H000C1420,&H90000000,0,0,0,0,100,100,2,0,1,2,0,7,40,40,60,1 [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text """ def build_ass(shots, ass_path): lines = [ASS_HEAD] t = 0.0 for i, s in enumerate(shots): d = float(s["duration"]) body = wrap_line(s["line"]) lines.append("Dialogue: 0,%s,%s,Default,,0,0,0,,{\\fad(250,250)}%s\n" % (ts(t + 0.22), ts(t + d - 0.22), body)) if i == 0: # 横排 HUD:全片只标一次 lines.append("Dialogue: 0,%s,%s,HUD,,0,0,0,,{\\an7\\pos(56,92)\\fad(400,400)}2010 / 机房\n" % (ts(t + 0.5), ts(t + 2.8))) if i == len(shots) - 1: # 竖排 HUD:全片只标一次 lines.append("Dialogue: 0,%s,%s,HUD,,0,0,0,,{\\an5\\pos(650,340)\\fad(400,400)}二\\N〇\\N二\\N六\n" % (ts(t + 0.5), ts(t + 2.8))) t += d with open(ass_path, "w", encoding="utf-8") as f: f.writelines(lines) return t def main(): ap = argparse.ArgumentParser() ap.add_argument("shots") ap.add_argument("--crop-bottom", type=int, default=0, help="底部水印带高度;A14B 实测无水印,默认 0(不裁画面)") ap.add_argument("--outro-seconds", type=float, default=2.0) ap.add_argument("--name", default=None) a = ap.parse_args() shots_path = os.path.abspath(a.shots) data = json.load(open(shots_path, encoding="utf-8")) ep = data["episode"] shots = data["shots"] vid_dir = os.path.join(ROOT, "work", ep, "videos") aud_dir = os.path.join(ROOT, "work", ep, "audio") tmp = os.path.join(ROOT, "work", ep, "render") os.makedirs(tmp, exist_ok=True) runs_path = os.path.join(vid_dir, "a14b_runs.json") runs = json.load(open(runs_path, encoding="utf-8")) if os.path.exists(runs_path) else {} # 1) 逐段标准化:去水印带 -> 居中填充到 720x1280 -> 30fps -> clone 补足精确时长 norm = [] for i, s in enumerate(shots, 1): sid = s["shotId"] rec = runs.get(sid) if not rec: print("ERROR: 缺 %s 的 A14B 产物记录" % sid, file=sys.stderr) return 1 src = os.path.join(ROOT, rec["out"]) if not os.path.exists(src): print("ERROR: 缺视频 %s" % src, file=sys.stderr) return 1 d = float(s["duration"]) keep = float(s.get("keepSeconds", d)) dst = os.path.join(tmp, "norm_%s.mp4" % sid) base = ("crop=iw:ih-%d:0:0,scale=%d:%d:force_original_aspect_ratio=increase," "crop=%d:%d,setsar=1,fps=%d" % (a.crop_bottom, OUT["width"], OUT["height"], OUT["width"], OUT["height"], OUT["fps"])) if keep < d - 0.05: # 局部修正:只采用前 keep 秒(漂移前的稳定段),慢放到目标时长 tail = ("trim=duration=%.3f,setpts=PTS*%.6f,fps=%d," "trim=duration=%.3f,setpts=PTS-STARTPTS,format=yuv420p" % (keep, d / keep, OUT["fps"], d)) note = "采用前 %.2fs 慢放至 %.2fs" % (keep, d) else: # 常规:A14B 约 5.06s,clone 尾帧补足精确时长 tail = ("tpad=stop_mode=clone:stop_duration=3," "trim=duration=%.3f,setpts=PTS-STARTPTS,format=yuv420p" % d) note = "全段" vf = base + "," + tail run(["-i", src, "-vf", vf, "-an", "-c:v", "libx264", "-preset", "veryfast", "-crf", "19", "-r", str(OUT["fps"]), dst]) norm.append(dst) print(" 标准化 %s -> %.2fs(%s)" % (sid, d, note), flush=True) # 2) 落版:光环 / 光束 / 星点 + zoompan 缓推,无文字 outro_png = os.path.join(ROOT, "assets", "overlay", "outro.png") outro = os.path.join(tmp, "outro.mp4") frames = int(a.outro_seconds * OUT["fps"]) zvf = ("zoompan=z='min(zoom+0.0008,1.10)':x='iw/2-(iw/zoom/2)':" "y='ih/2-(ih/zoom/2)':d=%d:s=%dx%d:fps=%d,format=yuv420p" % (frames, OUT["width"], OUT["height"], OUT["fps"])) run(["-loop", "1", "-framerate", str(OUT["fps"]), "-i", outro_png, "-vf", zvf, "-t", str(a.outro_seconds), "-an", "-c:v", "libx264", "-preset", "veryfast", "-crf", "19", outro]) print(" 落版 %.2fs" % a.outro_seconds, flush=True) # 3) 字幕 + HUD(一横一竖,各一次) ass_path = os.path.join(tmp, "ep01.ass") main_dur = build_ass(shots, ass_path) total = main_dur + a.outro_seconds print(" 字幕 %d 条 / 主片 %.2fs / 总长 %.2fs" % (len(shots), main_dur, total), flush=True) # 4) 一次性拼接 + 烧字幕(单次编码,避免多次重编码掉画质) video_only = os.path.join(tmp, "video_final.mp4") ins = [] for p in norm + [outro]: ins += ["-i", p] concat_in = "".join("[%d:v]" % i for i in range(len(norm) + 1)) fc = ("%sconcat=n=%d:v=1:a=0[cat];[cat]ass=%s:fontsdir=/System/Library/Fonts[v]" % (concat_in, len(norm) + 1, ass_path)) run(ins + ["-filter_complex", fc, "-map", "[v]", "-an", "-c:v", "libx264", "-preset", "veryfast", "-crf", "19", "-pix_fmt", OUT["pix_fmt"], "-r", str(OUT["fps"]), video_only]) # 5) 音轨:每段配音补静音到本镜时长,再补落版静音 audio_m4a = os.path.join(tmp, "audio_final.m4a") ains, parts, labels = [], [], [] for i, s in enumerate(shots): p = os.path.join(aud_dir, "vo_%02d.mp3" % (i + 1)) if not os.path.exists(p): print("ERROR: 缺配音 %s" % p, file=sys.stderr) return 1 ains += ["-i", p] parts.append("[%d:a]apad,atrim=duration=%.3f,aresample=48000[a%d]" % (i, float(s["duration"]), i)) labels.append("[a%d]" % i) afc = ";".join(parts) + ";" + "".join(labels) + \ "concat=n=%d:v=0:a=1,apad,atrim=duration=%.3f[aout]" % (len(shots), total) run(ains + ["-filter_complex", afc, "-map", "[aout]", "-c:a", "aac", "-b:a", "192k", "-ar", "48000", audio_m4a]) # 6) 封装 out_name = a.name or "%s_成片_v2.mp4" % ep out_path = os.path.join(ROOT, "renders", out_name) os.makedirs(os.path.dirname(out_path), exist_ok=True) run(["-i", video_only, "-i", audio_m4a, "-c", "copy", "-shortest", "-movflags", "+faststart", out_path]) dur, info = probe(out_path) streams = [l.strip() for l in info.splitlines() if "Stream #" in l] report = {"output": os.path.relpath(out_path, ROOT), "duration": dur, "mainDuration": round(main_dur, 2), "outroSeconds": a.outro_seconds, "bytes": os.path.getsize(out_path), "streams": streams, "shots": [{"shotId": s["shotId"], "duration": s["duration"], "video": runs[s["shotId"]]["out"]} for s in shots]} with open(os.path.join(tmp, "render_report.json"), "w", encoding="utf-8") as f: json.dump(report, f, ensure_ascii=False, indent=2) print("\n=== 成片 ===", flush=True) print(out_path, flush=True) print("时长 %.2fs | %.2f MB" % (dur or 0, os.path.getsize(out_path) / 1048576), flush=True) for l in streams: print(" " + l, flush=True) return 0 if __name__ == "__main__": sys.exit(main())