# -*- coding: utf-8 -*-
"""EP03 fix8：从重渲的 5 镜（纯画面，无字幕）抽全标签验证帧。
抽帧点取每镜 85% 处，确保所有标签/卡片/航线已出现。"""
import json, os, subprocess, sys

try:
    sys.stdout.reconfigure(encoding="utf-8", errors="replace")
except Exception:
    pass

ROOT = "D:/Download/muse_exchange"
PKG = os.path.join(ROOT, "ep03_package", "ep03_package")
SHOTS = os.path.join(PKG, "shots_fix8")
OUT = os.path.join(ROOT, "ep03_fix8_qa")
os.makedirs(OUT, exist_ok=True)

# 每镜抽帧点：镜内相对时间的 85%（确保全标签出现）
PICKS = {
    "s7_1": (750.88, 787.36),
    "s7_2": (787.36, 856.32),
    "s7_3": (856.32, 1034.24),
    "s8_1": (1034.24, 1177.28),
    "s8_2": (1177.28, 1259.52),  # 下面单独用 98% 抽（航线东非段在 94-100% 才画到）
}
for name, (s, e) in PICKS.items():
    pct = 0.98 if name == "s8_2" else 0.85  # S8-2 航线延伸到东非在 94-100% 才画完
    t_abs = s + (e - s) * pct     # 全片绝对时间（回执报数用）
    t_rel = (e - s) * pct         # 镜内相对时间（镜头 mp4 从 0 起）
    mp4 = os.path.join(SHOTS, name + ".mp4")
    assert os.path.exists(mp4), "缺镜 " + mp4
    out = os.path.join(OUT, "fix8_" + name + "_fulllabel.png")
    subprocess.run(["ffmpeg", "-hide_banner", "-nostdin", "-loglevel", "error", "-y",
                    "-ss", "%.2f" % t_rel, "-i", mp4, "-frames:v", "1", out], check=True)
    print(name, "t_abs=%.2f t_rel=%.2f" % (t_abs, t_rel), "->", os.path.basename(out),
          os.path.getsize(out), "B", flush=True)

# 拼一张 5 镜对照图（2 列），便于花酱一次看全
from PIL import Image, ImageDraw
imgs = [(n, Image.open(os.path.join(OUT, "fix8_%s_fulllabel.png" % n)).convert("RGB"))
        for n in PICKS]
tw, th = 640, 360
sheet = Image.new("RGB", (tw * 2, (th + 28) * 3), (245, 242, 235))
d = ImageDraw.Draw(sheet)
for i, (n, im) in enumerate(imgs):
    col, row = i % 2, i // 2
    x, y = col * tw, row * (th + 28)
    sheet.paste(im.resize((tw, th)), (x, y + 28))
    d.text((x + 8, y + 6), n + "  fix8 full-label", fill=(30, 30, 30))
sheet_path = os.path.join(OUT, "fix8_5shots_qa_sheet.png")
sheet.save(sheet_path)
print("SHEET", sheet_path, os.path.getsize(sheet_path), "B", flush=True)
