Files
ai-manju/scripts/render_native.py
T
rong a8e10b7b17 fix: 字幕按实测发声段对齐 + 静音镜/包装踩坑 (v1.2.1)
- 纠正 render.md/pitfalls.md「按设计时间缩放字幕」错误做法,改为只认实测发声段
- 新增 scripts/align_subtitles.py(泛化对齐工具,配置外置、支持 dump-segs/init-config/dry-run)
- render_native.py 加 --ass 参数,读对齐 ASS 不被内置设计时间轴覆盖
- pitfalls/native-av/README/SKILL 补六条踩坑;install.sh 清单同步
2026-11-19 17:14:14 +08:00

253 lines
11 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
"""原生音画同出缝合器:8 镜直拼(保留模型原生音轨)+ 烧字幕 + 画面瑕疵遮盖 -> 成片。
与旧 render.py 的区别:不再用 TTS 配音(会与原生音轨叠成双份)、不再额外加落版
(S08 已是落版镜)、不强制 720x1280(保持源 576x1024 无损)。
字幕时间轴取自各镜 40_prompts/Sxx.h3v3.txt 的 [Audio] 段逐句时间戳
(与口型对齐同源,比 ASR 更准);台词文本取 [Audio] 引号内原文(=定稿逐字)。
画面瑕疵遮盖:
- S01 药盒乱码中文字:对药盒区域做局部模糊(可选,--mask)
- S05 监控左上角模型自发时间戳 + 椅背座位数字:局部模糊(可选,--mask)
用法:
python3 tools/render_native.py projects/EP01/30_shots.json
python3 tools/render_native.py projects/EP01/30_shots.json --mask # 开启瑕疵遮盖
"""
import argparse
import json
import os
import re
import subprocess
import sys
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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_duration(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)
return (int(m.group(1)) * 3600 + int(m.group(2)) * 60 + float(m.group(3))) if m else None
def parse_ass_timeline(prompt_path, shot_start, shot_dur, design_dur):
"""从 h3v3 [Audio] 段解析 (start,end,text) 逐句,转成全片绝对秒。
h3v3 时间戳按设计时长 design_dur 写;源实际 shot_dur 略长,按比例缩放对齐口型。"""
t = open(prompt_path, encoding="utf-8").read()
if "[Audio]" not in t:
return []
seg = t.split("[Audio]")[1].split("[Narrative]")[0]
scale = shot_dur / design_dur if design_dur > 0 else 1.0
out = []
# 匹配 0.0s-0.9s: "台词"
for m in re.finditer(r"(\d+\.?\d*)s\s*-\s*(\d+\.?\d*)s\s*:\s*\"([^\"]+)\"", seg):
a, b, txt = float(m.group(1)), float(m.group(2)), m.group(3)
a = min(a * scale, shot_dur); b = min(b * scale, shot_dur)
if b - a < 0.15:
continue
out.append((shot_start + a, shot_start + b, txt))
return out
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=13):
parts = [p for p in re.split(r"([,。;!?、,.;!?])", text) if p]
lines, cur = [], ""
for p in parts:
if p in PUNCT:
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) > 3:
lines = lines[:3]
return "\\N".join(lines)
ASS_HEAD = """[Script Info]
ScriptType: v4.00+
PlayResX: 576
PlayResY: 1024
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,40,&H00FFFFFF,&H00FFFFFF,&H000C1420,&H90000000,1,0,0,0,100,100,0,0,1,3,1,2,40,40,120,1
[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
"""
def build_ass(timeline, ass_path):
lines = [ASS_HEAD]
for a, b, txt in timeline:
body = wrap_line(txt)
lines.append("Dialogue: 0,%s,%s,Default,,0,0,0,,{\\fad(180,180)}%s\n" %
(ts(a), ts(b), body))
with open(ass_path, "w", encoding="utf-8") as f:
f.writelines(lines)
return len(timeline)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("shots")
ap.add_argument("--mask", action="store_true", help="开启画面瑕疵局部模糊遮盖")
ap.add_argument("--ass", default=None,
help="用现成的对齐 ASS(align_subtitles.py 产出)烧字幕,跳过内置的设计时间轴。"
"强烈建议台词镜用这个——内置时间轴按 h3v3 设计秒数,会与口型错位")
ap.add_argument("--name", default=None)
a = ap.parse_args()
data = json.load(open(os.path.abspath(a.shots), encoding="utf-8"))
ep = data["episode"]
shots = data["shots"]
vid_dir = os.path.join(ROOT, "work", ep, "videos", "native")
tmp = os.path.join(ROOT, "work", ep, "render_native")
os.makedirs(tmp, exist_ok=True)
# 1) 逐镜标准化 + 可选瑕疵遮盖(保留原生音轨)
# 576x1024@24fps 源,统一 fps=24, SAR=1
# 瑕疵遮盖矩形 (x,y,w,h) 基于 576x1024
# 遮盖类型:blur=局部轻度模糊(药盒乱码字);solid=深色半透明条(监控时间戳)
# 矩形 (x,y,w,h) 基于 576x1024
MASK_REGIONS = {
"S01": [("blur", 290, 742, 230, 165)], # 手中白药盒上的乱码中文字
"S05": [("solid", 0, 0, 300, 62)], # 左上角模型自发时间戳
}
norm = []
t_cursor = 0.0
timeline = []
for s in shots:
sid = s["shotId"]
src = os.path.join(vid_dir, sid + ".mp4")
if not os.path.exists(src):
print("ERROR 缺视频", src, file=sys.stderr); return 1
d = probe_duration(src) or float(s["duration"]) # 用源实际时长,音画一致
dst = os.path.join(tmp, "norm_%s.mp4" % sid)
regions = MASK_REGIONS.get(sid, []) if a.mask else []
if regions:
# filter_complex: 主链做 solid 遮罩;blur 区用 split+crop+boxblur+overlay
base_filters = ["setsar=1", "fps=24", "format=yuv420p"]
for typ, x, y, w, h in regions:
if typ == "solid":
base_filters.append(
"drawbox=x=%d:y=%d:w=%d:h=%d:[email protected]:t=fill" % (x, y, w, h))
blurs = [(i, x, y, w, h) for i, (typ, x, y, w, h) in enumerate(regions)
if typ == "blur"]
if blurs:
fc = []
fc.append("[0:v]" + ",".join(base_filters) + ",split=%d[b0]" % (len(blurs) + 1)
+ "".join("[b%d]" % (i + 1) for i in range(len(blurs))))
for j, (idx, x, y, w, h) in enumerate(blurs):
fc.append("[b%d]crop=%d:%d:%d:%d,boxblur=7:2[bl%d]" %
(j + 1, w, h, x, y, j))
ov = "".join("[bl%d]overlay=%d:%d" % (j, x, y)
for j, (idx, x, y, w, h) in enumerate(blurs))
fc.append("[b0]%s,setpts=PTS-STARTPTS[v]" % ov)
run(["-i", src, "-filter_complex", ";".join(fc),
"-map", "[v]", "-map", "0:a",
"-c:v", "libx264", "-preset", "veryfast", "-crf", "18",
"-c:a", "aac", "-b:a", "128k", "-ar", "32000", dst])
else:
vf = ",".join(base_filters) + ",setpts=PTS-STARTPTS"
run(["-i", src, "-vf", vf,
"-c:v", "libx264", "-preset", "veryfast", "-crf", "18",
"-c:a", "aac", "-b:a", "128k", "-ar", "32000", dst])
else:
vf = "setsar=1,fps=24,format=yuv420p,setpts=PTS-STARTPTS"
run(["-i", src, "-vf", vf,
"-c:v", "libx264", "-preset", "veryfast", "-crf", "18",
"-c:a", "aac", "-b:a", "128k", "-ar", "32000", dst])
norm.append(dst)
pf = os.path.join(ROOT, "projects", ep, "40_prompts", sid + ".h3v3.txt")
sent = parse_ass_timeline(pf, t_cursor, d, float(s["duration"]))
timeline += sent
print(" 标准化 %s -> %.2fs,字幕 %d 句%s" %
(sid, d, len(sent), " +遮盖" if regions else ""), flush=True)
t_cursor += d
total = t_cursor
print(" 主片总长 %.2fs,字幕共 %d 句" % (total, len(timeline)), flush=True)
# 2) 烧字幕(单次编码)
ass_path = os.path.join(tmp, "ep.ass")
if a.ass:
# 用对齐后的现成 ASS:先复制到 tmp 路径,避免改写用户源文件
import shutil
src_ass = os.path.abspath(a.ass)
if not os.path.exists(src_ass):
print("ERROR 缺 ASS", src_ass, file=sys.stderr); return 1
if os.path.abspath(ass_path) != src_ass:
shutil.copy(src_ass, ass_path)
nsub = sum(1 for l in open(ass_path, encoding="utf-8") if l.startswith("Dialogue:"))
print(" 字幕使用对齐 ASS: %s(%d 行)" % (src_ass, nsub), flush=True)
else:
nsub = build_ass(timeline, ass_path)
print(" ⚠ 使用内置设计时间轴字幕——可能与口型错位,建议改用 --ass + align_subtitles.py", flush=True)
video_only = os.path.join(tmp, "video_sub.mp4")
# concat demuxer
list_path = os.path.join(tmp, "concat.txt")
with open(list_path, "w", encoding="utf-8") as f:
for p in norm:
f.write("file '%s'\n" % p.replace("'", "'\\''"))
run(["-f", "concat", "-safe", "0", "-i", list_path,
"-vf", "ass=%s:fontsdir=/System/Library/Fonts" % ass_path,
"-c:v", "libx264", "-preset", "veryfast", "-crf", "18",
"-pix_fmt", "yuv420p", "-r", "24",
"-c:a", "aac", "-b:a", "128k", "-ar", "32000", video_only])
# 3) 输出
out_name = a.name or "%s_音画同出_粗剪%s.mp4" % (ep, "_遮盖" if a.mask else "")
out_path = os.path.join(ROOT, "renders", out_name)
os.makedirs(os.path.dirname(out_path), exist_ok=True)
run(["-i", video_only, "-c", "copy", "-movflags", "+faststart", out_path])
# 4) 核验
r = subprocess.run([ff(), "-hide_banner", "-i", out_path],
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True)
dur_m = re.search(r"Duration:\s*(\d+):(\d+):([\d.]+)", r.stderr)
dur = (int(dur_m.group(1)) * 3600 + int(dur_m.group(2)) * 60 + float(dur_m.group(3))) if dur_m else None
streams = [l.strip() for l in r.stderr.splitlines() if "Stream #" in l]
print("\n=== 成片 ===")
print(out_path)
print("时长 %.2fs | %.2f MB | 字幕 %d 句" %
(dur or 0, os.path.getsize(out_path) / 1048576, nsub))
for l in streams:
print(" " + l)
return 0
if __name__ == "__main__":
sys.exit(main())