#!/usr/bin/env python3 """Build the film from video/scenes/film.scenes and video/scenes/cues.tpl.js. python3 tools/build_film.py Every time in a scene is a phrase in the voiceover, so changing a take retimes the film: @{b09:16 bits} start of that phrase, in scene time @{b09:16 bits+0.2} with an offset; #2 picks the second match (@{b06:network volume#2}) @{b09:start} @{b09:end} @{end} @{end-0.25} Writes compositions/, index.html, assets/cues.js and assets/timeline.json. """ import json, os, re, sys ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") VIDEO = os.path.join(ROOT, "video") VO = os.path.join(ROOT, "assets", "vo", "film") LEAD_IN = 1.2 HOLD_AFTER = {"b01": 0.4, "b02": 0.4, "b03": 0.4, "b04": 1.51, "b05": 7.791} def norm(word): return re.sub(r"[^a-z0-9]", "", word.lower()) def load_beats(): beats, clock = {}, LEAD_IN for beat, hold in HOLD_AFTER.items(): words = json.load(open(os.path.join(VO, beat + ".words.json"))) for word in words: word["key"] = norm(word["word"]) words = [w for w in words if w["key"]] length = words[-1]["end"] beats[beat] = {"start": clock, "length": length, "words": words} clock += length + hold return beats, clock def phrase_time(beats, beat, phrase): phrase, _, nth = phrase.partition("#") data = beats[beat] if phrase == "start": return data["start"] if phrase == "end": return data["start"] + data["length"] wanted = [norm(w) for w in phrase.split()] keys = [w["key"] for w in data["words"]] hits = [i for i in range(len(keys) - len(wanted) + 1) if keys[i : i + len(wanted)] == wanted] if len(hits) < int(nth or 1): sys.exit(f"no match for '{phrase}' in {beat}") return data["start"] + data["words"][hits[int(nth or 1) - 1]]["start"] def resolve(beats, expr, scene=None): match = re.fullmatch(r"\s*(?:(b\d\d):)?(.*?)\s*([+-]\s*\d+(?:\.\d+)?)?\s*", expr) beat, name, offset = match.groups() offset = float(offset.replace(" ", "")) if offset else 0.0 if beat: value = phrase_time(beats, beat, name) if scene: value -= scene["start"] elif name == "end": value = scene["duration"] else: value = float(name or 0) return value + offset def fill(beats, text, scene=None): return re.sub(r"@\{([^}]+)\}", lambda m: f"{resolve(beats, m.group(1), scene):.3f}", text) def parse_scenes(beats, total): source = open(os.path.join(VIDEO, "scenes", "film.scenes")).read() blocks = re.split(r"^=== ", source, flags=re.M)[1:] scenes = [] for block in blocks: head, _, rest = block.partition("\n") name, _, start = head.partition("|") markup, _, script = rest.partition("\n---\n") scenes.append({"id": name.strip(), "start": resolve(beats, start.strip().strip("@{}")), "markup": markup, "script": script}) for scene, following in zip(scenes, scenes[1:] + [None]): scene["duration"] = (following["start"] if following else total) - scene["start"] if scene["duration"] <= 0: sys.exit(f"scene {scene['id']} has no length") return scenes def write_scene(beats, scene): markup = fill(beats, scene["markup"], scene) script = fill(beats, scene["script"], scene) style = "\n".join(re.findall(r"", markup, flags=re.S)) markup = re.sub(r"\s*", "", markup, flags=re.S).strip() page = f""" """ open(os.path.join(VIDEO, "compositions", scene["id"] + ".html"), "w").write(page) def write_index(scenes, total): hosts = [f'
'] for scene in scenes: hosts.append(f'
') if os.path.exists(os.path.join(VIDEO, "assets", "mix.m4a")): hosts.append(f' ') page = """
%s
""" % (total, "\n".join(hosts)) open(os.path.join(VIDEO, "index.html"), "w").write(page) def main(): beats, total = load_beats() scenes = parse_scenes(beats, total) for name in os.listdir(os.path.join(VIDEO, "compositions")): if name != "field.html": os.remove(os.path.join(VIDEO, "compositions", name)) for scene in scenes: write_scene(beats, scene) cues = fill(beats, open(os.path.join(VIDEO, "scenes", "cues.tpl.js")).read()) open(os.path.join(VIDEO, "assets", "cues.js"), "w").write(f"window.FILM_SECONDS = {total:.3f};\n" + cues) write_index(scenes, total) timeline = {"total": total, "beats": {k: {"start": v["start"], "length": v["length"]} for k, v in beats.items()}, "scenes": [{"id": s["id"], "start": s["start"], "duration": s["duration"]} for s in scenes]} json.dump(timeline, open(os.path.join(VIDEO, "assets", "timeline.json"), "w"), indent=1) print(f"{total:.1f} s ({total/60:.2f} min), {len(scenes)} scenes") for s in scenes: print(f" {s['id']:12} {s['start']:7.2f} {s['duration']:6.2f}") main()