Skip to content

Short vertical cut-paper explainer reels, made by your coding agent. Nine Agent Skills plus a Remotion library. Works with Claude Code, OpenCode, Codex, Gemini CLI, Cursor, Copilot and more.

1.9 KB
Raw Blame
# Build timing.js: per-scene narration duration + word timestamps aligned to the script text.
# With ASR json (from voice/asr.py) words get real times; without it, a 2.75 wps estimate.
import json, sys, re, difflib, os
script = json.load(open("script.json"))
asr = json.load(open(sys.argv[1])) if len(sys.argv) > 1 else {}
CH = os.environ.get("CHOSEN", "voice/chosen.json")
chosen = json.load(open(CH)) if os.path.exists(CH) else {}
norm = lambda w: re.sub(r"[^a-z0-9]", "", w.lower())
seg = {}
for ln in script:
    words = ln["text"].split()
    path = chosen.get(ln["id"])
    a = asr.get(path) if path else None
    if a:
        aw = a["words"]; dur = a["dur"]
        sm = difflib.SequenceMatcher(a=[norm(w) for w in words], b=[norm(x["w"]) for x in aw], autojunk=False)
        times = [None] * len(words)
        for blk in sm.get_matching_blocks():
            for k in range(blk.size): times[blk.a + k] = (aw[blk.b + k]["s"], aw[blk.b + k]["e"])
        # interpolate gaps between matched neighbours
        for i in range(len(words)):
            if times[i] is None:
                j = i
                while j < len(words) and times[j] is None: j += 1
                lo = times[i - 1][1] if i else 0.0
                hi = times[j][0] if j < len(words) else dur
                n = j - i
                for k in range(n): times[i + k] = (lo + (hi - lo) * k / n, lo + (hi - lo) * (k + 1) / n)
    else:
        dur = len(words) / 2.75 + 0.2
        times = [(i / 2.75, (i + 1) / 2.75) for i in range(len(words))]
    seg[ln["id"]] = {"dur": round(dur, 3), "file": path, "words": [{"w": w, "s": round(t[0], 3), "e": round(t[1], 3)} for w, t in zip(words, times)]}
open(os.environ.get("TIMING_OUT", "timing.js"), "w").write("// generated by build_timing.py\nconst VO = " + json.dumps({"order": [l["id"] for l in script], "seg": seg}) + ";\n")
print({k: v["dur"] for k, v in seg.items()}, "total", round(sum(v["dur"] for v in seg.values()), 1))