#!/usr/bin/env python3 """teaser · canvas 的合成(maliang venv,CPU)。在 live_compose 的部件(按键 HUD / 时间线条 / 字幕 / 语义小窗)上: side 两条 rollout 并排(左 = 参照:原版或白纸对照,右 = 神笔版;标签可改)+ 两边语义小窗 + 编辑字幕 + 闪圈 + 按键 + 时间线 grid4 2×2:同一条编辑时间线、四个不同首帧 / 场景(场景名角标 + 当前编辑字幕 + 语义小窗 + 按键) sheet 眼检拼图:每个事件一组(上 = 参照,下 = 神笔版,列 = 编辑前 1 帧 / +12 / +48,右 = 神笔版语义) clip 从视频里切 [f0, f1) 帧 usage: $PY scripts/teaser/canvas_compose.py side --ref --edit -o x.mp4 [--left "..."] [--right "..."] [--title ..] [--subtitle ..] $PY scripts/teaser/canvas_compose.py grid4 --edits -o g.mp4 [--title ..] $PY scripts/teaser/canvas_compose.py sheet --ref --edit -o x.jpg """ from __future__ import annotations import argparse import json import os import sys M = "/opt/dlami/nvme/zhiyangdeng/maliang" HERE = os.path.dirname(os.path.abspath(__file__)) for _p in (f"{M}/code/gc25d", HERE): if _p not in sys.path: sys.path.insert(0, _p) import cv2 # noqa: E402 import numpy as np # noqa: E402 from PIL import Image, ImageDraw # noqa: E402 import compose as CP # noqa: E402 import live_compose as LC # noqa: E402 FPS = 16 SCENE_TITLE = {"icecave@photoreal": "Ice Cave", "salt_flats@cartoon": "Salt Flats (cartoon)", "honey_hive@photoreal": "Honey Hive", "greek@voxel": "Greek Isles (voxel)", "icecave@cartoon": "Ice Cave (cartoon)", "dunes@cartoon": "Desert Dunes (cartoon)", "synthwave@cartoon": "Synthwave", "snow_forest@photoreal": "Snow Forest", "alien@photoreal": "Alien Planet", "lava@photoreal": "Lava Fields", "dunes@photoreal": "Desert Dunes", "candy@cartoon": "Candy Land (cartoon)", "salt_flats@cinematic": "Salt Flats", "beach@cartoon": "Beach (cartoon)", "greek@painterly": "Greek Isles (painterly)", "autumn_harvest@cinematic": "Autumn Harvest"} def scene_of(d): m = json.load(open(os.path.join(d, "meta.json"))) return (m.get("labels") or {}).get("scene") or (m.get("meta") or {}).get("scene") or "" def title_of(scene): return SCENE_TITLE.get(scene, scene.split("@")[0].replace("_", " ").title()) def flash(img, cur, f, x0, y0, s): sc = None if cur.get("items"): sc = cur["items"][0].get("screen") sc = sc or cur.get("center_screen") or cur.get("screen") if cur["type"] == "vtext" and cur.get("where"): sc = cur["where"] if sc and f - cur["f"] < 24: cx, cy = int(x0 + sc[0] * s), int(y0 + sc[1] * s) r = int(50 * s / 0.9 + 2 * (f - cur["f"])) cv2.circle(img, (cx, cy), r, (255, 214, 64), 3, cv2.LINE_AA) def cmd_side(a): events, _ = LC.load_events(a.edit) ids = np.load(os.path.join(a.edit, "ids.npz"))["ids"] ids_o = np.load(os.path.join(a.ref, "ids.npz"))["ids"] blocks = LC.blocks_of(a.edit) n = len(ids) Wc, Hc = 1920, 1080 pw = (Wc - 3 * 24) // 2 ph = int(round(pw * 480 / 832)) y0 = 262 xs = [24, 24 * 2 + pw] inset_w = 300 inset_h = int(round(inset_w * 480 / 832)) ro, re = CP.iter_video(os.path.join(a.ref, "gen.mp4")), CP.iter_video(os.path.join(a.edit, "gen.mp4")) head = np.zeros((Hc, Wc, 3), np.uint8) head[:] = CP.BG hd = Image.fromarray(head) d = ImageDraw.Draw(hd) CP.draw_text(d, 24, 132, a.title or "Paint on a blank canvas: whatever you draw becomes real", 38, CP.FG, "SemiBold") if a.subtitle: CP.draw_text(d, 24, 184, a.subtitle, 24, CP.MUTED) CP.draw_text(d, xs[0], y0 - 34, a.left or "Original (no edits)", 24, CP.MUTED, "Medium") CP.draw_text(d, xs[1], y0 - 34, a.right or "With the brush (same start, same seed, same keys)", 24, CP.FG, "Medium") base = np.asarray(hd).copy() for x in xs: CP.frame_rect(base, x, y0, pw, ph) s = pw / 832 def frames(): for f, (fo, fe) in enumerate(zip(ro, re)): img = base.copy() img[y0:y0 + ph, xs[0]:xs[0] + pw] = CP.fit(fo, pw, ph) img[y0:y0 + ph, xs[1]:xs[1] + pw] = CP.fit(fe, pw, ph) for x_, ii in ((xs[0], ids_o), (xs[1], ids)): sp = CP.sem_panel(ii[min(f, len(ii) - 1)], inset_w, inset_h) ix, iy = x_ + pw - inset_w - 10, y0 + 10 img[iy:iy + inset_h, ix:ix + inset_w] = sp CP.frame_rect(img, ix, iy, inset_w, inset_h, (230, 230, 230), 1) cur = LC.current_event(events, f) if cur is not None: p = CP.pill(LC.caption(cur, a.zh), 34, fg=(20, 20, 20), bg=(255, 214, 64), alpha=0.92, weight="Bold") CP.paste_rgba(img, p, xs[1] + 16, y0 + ph - p[0].shape[0] - 16) flash(img, cur, f, xs[1], y0, s) if blocks is not None: k = 0 if f == 0 else (f + 3) // 4 CP.paste_rgba(img, LC.key_overlay(blocks[min(k, len(blocks) - 1)]), xs[0] + 12, y0 + ph - 90) tb = LC.timeline_bar(Wc - 48, events, n, f) CP.paste_rgba(img, tb, 24, y0 + ph + 30) yy = y0 + ph + 70 im2 = Image.fromarray(img) d2 = ImageDraw.Draw(im2) CP.draw_text(d2, 24, yy, f"{f / FPS:5.1f} s", 24, CP.FG, "SemiBold") # 事件行:只显示当前附近的 5 个(多了放不下) past = [i for i, ev in enumerate(events) if ev["f"] <= f] j0 = max(0, (past[-1] if past else 0) - 2) x = 140 for ev in events[j0:j0 + 6]: col = (255, 214, 64) if ev["f"] <= f else (110, 116, 130) st = f"{ev['t_sec']:.0f}s {LC.caption(ev, a.zh)}" CP.draw_text(d2, x, yy + 2, st, 20, col, "Medium") x += CP.text_w(st, 20, "Medium") + 28 if x > Wc - 260: break yield np.asarray(im2) CP.write_video(a.o, frames(), crf=18) print(a.o) def cmd_single(a): """神笔版全屏 + 语义小窗 + 字幕 + 按键 + 底部时间线(剪辑用)。""" events, _ = LC.load_events(a.edit) ids = np.load(os.path.join(a.edit, "ids.npz"))["ids"] blocks = LC.blocks_of(a.edit) n = len(ids) Wc, Hc = 1920, 1080 inset_w = 420 inset_h = int(round(inset_w * 480 / 832)) s = 1872 / 832 def frames(): for f, fe in enumerate(CP.iter_video(os.path.join(a.edit, "gen.mp4"))): img = cv2.resize(fe, (1872, 1080), interpolation=cv2.INTER_LANCZOS4) img = cv2.copyMakeBorder(img, 0, 0, (Wc - 1872) // 2, Wc - 1872 - (Wc - 1872) // 2, cv2.BORDER_CONSTANT, value=CP.BG) sp = CP.sem_panel(ids[min(f, n - 1)], inset_w, inset_h) img[20:20 + inset_h, Wc - inset_w - 20:Wc - 20] = sp CP.frame_rect(img, Wc - inset_w - 20, 20, inset_w, inset_h, (230, 230, 230), 2) cur = LC.current_event(events, f) if cur is not None: p = CP.pill(LC.caption(cur, a.zh), 44, fg=(20, 20, 20), bg=(255, 214, 64), alpha=0.92, weight="Bold") CP.paste_rgba(img, p, 40, Hc - p[0].shape[0] - 70) flash(img, cur, f, (Wc - 1872) // 2, 0, s) if blocks is not None: k = 0 if f == 0 else (f + 3) // 4 CP.paste_rgba(img, LC.key_overlay(blocks[min(k, len(blocks) - 1)], 40), Wc - 260, Hc - 160) tb = LC.timeline_bar(Wc - 80, events, n, f, h=26) CP.paste_rgba(img, tb, 40, Hc - 40) yield img CP.write_video(a.o, frames(), crf=18) print(a.o) def cmd_grid4(a): dirs = a.edits[:4] evs = [LC.load_events(d)[0] for d in dirs] idss = [np.load(os.path.join(d, "ids.npz"))["ids"] for d in dirs] names = [title_of(scene_of(d)) for d in dirs] blocks = LC.blocks_of(dirs[0]) its = [CP.iter_video(os.path.join(d, "gen.mp4")) for d in dirs] Wc, Hc = 1920, 1080 pw, ph = 936, 540 pos = [(16, 0), (968, 0), (16, 540), (968, 540)] iw = 220 ih = int(round(iw * 480 / 832)) n = len(idss[0]) def frames(): for f, fr in enumerate(zip(*its)): img = np.zeros((Hc, Wc, 3), np.uint8) img[:] = CP.BG for k, (x, y) in enumerate(pos[:len(fr)]): img[y:y + ph, x:x + pw] = CP.fit(fr[k], pw, ph) sp = CP.sem_panel(idss[k][min(f, len(idss[k]) - 1)], iw, ih) img[y + 8:y + 8 + ih, x + pw - iw - 8:x + pw - 8] = sp CP.paste_rgba(img, CP.pill(names[k], 22, alpha=0.6), x + 10, y + 10) cur = LC.current_event(evs[k], f) if cur is not None: p = CP.pill(LC.caption(cur, a.zh), 28, fg=(20, 20, 20), bg=(255, 214, 64), alpha=0.92, weight="Bold") CP.paste_rgba(img, p, x + 10, y + ph - p[0].shape[0] - 12) if blocks is not None: kk = 0 if f == 0 else (f + 3) // 4 ko = LC.key_overlay(blocks[min(kk, len(blocks) - 1)], 26) CP.paste_rgba(img, ko, Wc // 2 - ko[0].shape[1] // 2, Hc - ko[0].shape[0] - 6) tb = LC.timeline_bar(Wc - 32, evs[0], n, f, h=18) CP.paste_rgba(img, tb, 16, 540 - 9) yield img CP.write_video(a.o, frames(), crf=18) print(a.o) def cmd_sheet(a): ref = CP.read_video(os.path.join(a.ref, "gen.mp4")) edit = CP.read_video(os.path.join(a.edit, "gen.mp4")) ids = np.load(os.path.join(a.edit, "ids.npz"))["ids"] events = LC.load_events(a.edit)[0] n = len(edit) tw = 256 th = int(round(tw * 480 / 832)) offs = [-1, 12, 48] rows = [] for ev in events: f = ev["f"] idx = [min(max(f + o, 0), n - 1) for o in offs] r1 = np.concatenate([CP.fit(ref[i], tw, th) for i in idx], 1) r2 = np.concatenate([CP.fit(edit[i], tw, th) for i in idx], 1) blk = np.concatenate([r1, r2], 0) blk = np.concatenate([blk, np.concatenate([CP.sem_panel(ids[idx[1]], tw, th), CP.sem_panel(ids[idx[2]], tw, th)], 0)], 1).copy() cv2.putText(blk, f"{ev['type']} f{f} {ev['t_sec']}s {ev['label'].encode('ascii', 'replace').decode()}", (6, 18), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (255, 255, 0), 1, cv2.LINE_AA) rows.append(blk) rows.append(np.zeros((6, blk.shape[1], 3), np.uint8)) os.makedirs(os.path.dirname(os.path.abspath(a.o)), exist_ok=True) cv2.imwrite(a.o, np.concatenate(rows, 0)[..., ::-1], [cv2.IMWRITE_JPEG_QUALITY, 86]) print(a.o) def cmd_clip(a): fr = [] for k, x in enumerate(CP.iter_video(a.src)): if k >= a.f1: break if k >= a.f0: fr.append(x) CP.write_video(a.o, fr, crf=18) print(a.o) def main(argv=None): ap = argparse.ArgumentParser() ap.add_argument("cmd", choices=["side", "single", "grid4", "sheet", "clip"]) ap.add_argument("--ref") ap.add_argument("--edit") ap.add_argument("--edits", nargs="*", default=[]) ap.add_argument("--src") ap.add_argument("--f0", type=int, default=0) ap.add_argument("--f1", type=int, default=96) ap.add_argument("-o", required=True) ap.add_argument("--title") ap.add_argument("--subtitle") ap.add_argument("--left") ap.add_argument("--right") ap.add_argument("--zh", action="store_true") a = ap.parse_args(argv) dict(side=cmd_side, single=cmd_single, grid4=cmd_grid4, sheet=cmd_sheet, clip=cmd_clip)[a.cmd](a) if __name__ == "__main__": main()