#!/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"""