Skip to content

majiayu000/spellbook / skills/code-film

code-film

Verified

用纯代码(three.js 离线渲染 + 合成音频)做有故事、有电影质感的动画短片并导出 MP4。当用户想做短片、动画片、宣传片、角色动画、循环视频、片头、产品故事片,或者说“用代码做个视频”“做一个有趣的短片”“帮我设计个小动画”“make a short film / animated video / motion piece with code”时使用,即使没提 three.js。也用于继续修改这类片子。写实真人、实拍感、AI 视频生成、普通视频剪辑不适用。

scripts/stills.py

View on GitHub
"""Render stills through the offline path and tile them into contact sheets.
Usage: uv run --with playwright --python 3.12 python -I stills.py <film.html> <outdir> <t1,t2,...> [samples=8]
"""
import base64
import subprocess
import sys
from pathlib import Path

from playwright.sync_api import sync_playwright

SKILL = Path(__file__).resolve().parent.parent
ARGS = ["--ignore-gpu-blocklist"] + (["--use-angle=metal"] if sys.platform == "darwin" else [])


def sheets(out):
    frames = sorted(out.glob("t*.jpg"))
    for i in range(0, len(frames), 12):
        part = frames[i:i + 12]
        inputs = [a for f in part for a in ("-i", str(f))]
        scaled = ";".join(f"[{k}:v]scale=640:360[v{k}]" for k in range(len(part)))
        if len(part) == 1:
            graph = f"[0:v]scale=640:360"
        else:
            layout = "|".join(f"{(j % 3) * 640}_{(j // 3) * 360}" for j in range(len(part)))
            graph = scaled + ";" + "".join(f"[v{k}]" for k in range(len(part))) + f"xstack=inputs={len(part)}:layout={layout}:fill=black"
        subprocess.run(["ffmpeg", "-y", "-loglevel", "error", *inputs, "-filter_complex", graph, "-frames:v", "1", "-q:v", "3", str(out / f"sheet{i // 12}.jpg")], check=True)


def main():
    html, out, times = Path(sys.argv[1]).resolve(), Path(sys.argv[2]), [float(x) for x in sys.argv[3].split(",")]
    samples = int(sys.argv[4]) if len(sys.argv) > 4 else 8
    out.mkdir(parents=True, exist_ok=True)
    for old in out.glob("*.jpg"):
        old.unlink()
    with sync_playwright() as p:
        browser = p.chromium.launch(channel="chrome", headless=True, args=ARGS)
        page = browser.new_page(viewport={"width": 1320, "height": 800})
        logs = []
        page.on("pageerror", lambda e: logs.append(f"pageerror: {e}"))
        page.on("console", lambda m: logs.append(f"{m.type}: {m.text}") if m.type == "error" else None)
        page.route("**/three.min.js", lambda r: r.fulfill(path=str(SKILL / "assets/vendor/three.min.js"), content_type="application/javascript"))
        page.goto(f"file://{html}?render=1")
        try:
            page.wait_for_function("window.__ready===true", timeout=60000)
        except Exception:
            sys.exit("Film did not start:\n" + "\n".join(logs[:10]))
        for t in times:
            data = page.evaluate("([t, n]) => { window.renderFrame(t * 60, n, 60); return document.querySelector('canvas').toDataURL('image/jpeg', .92); }", [t, samples])
            (out / f"t{t:06.2f}.jpg").write_bytes(base64.b64decode(data.split(",")[1]))
        browser.close()
    sheets(out)
    print("sheets:", *sorted(str(p) for p in out.glob("sheet*.jpg")))
    for line in sorted(set(logs))[:10]:
        print(line[:300])


main()
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall