# The film: python3 make_film.py voice | frames [t ...] | audio | video # Frames come from the page's own stage SVG; narration is Kokoro af_heart, one sentence per clip; piano made in numpy. import json, asyncio, os, subprocess, sys, numpy as np, soundfile as sf K = '/home/claude/compass/'; F = K + 'film/' LINES = json.load(open(K + 'content/film.json')) FPS = 30; TITLE = 2.8; GAP = 1.15 if 'voice' in sys.argv: sys.path.insert(0, K + 'tools'); from voice import speakable from kokoro_onnx import Kokoro k = Kokoro('/home/claude/kokoro/kokoro-v1.0.onnx', '/home/claude/kokoro/voices-v1.0.bin'); d = [] for i, t in enumerate(LINES): a, sr = k.create(speakable(t), voice='af_heart', speed=1.0, lang='en-us'); a = np.asarray(a, 'float32') idx = np.where(np.abs(a) > .01)[0]; a = a[max(0, idx[0] - 960): idx[-1] + 2880] sf.write(F + 'u%d.wav' % i, a, sr); d.append(round(len(a) / sr, 2)) json.dump(d, open(F + 'udur.json', 'w')); print('voice', d) D = json.load(open(F + 'udur.json')) ST = []; t = TITLE for i, dd in enumerate(D): ST.append(round(t, 2)); t += dd + GAP + (.5 if i in (1, 5, 6) else 0) END = ST[-1] + D[-1]; DUR = round(END + 4.2, 1) page = open(F + 'film.tpl.html').read().replace('{{STAGE}}', open(K + 'src/stage.svg.html').read()).replace('{{STARTS}}', json.dumps(ST)).replace('{{DURS}}', json.dumps(D)).replace('{{LINES}}', json.dumps(LINES)) open(F + 'film.html', 'w').write(page) print('film', DUR, 's, starts', ST) if 'frames' in sys.argv: from playwright.async_api import async_playwright os.makedirs(F + 'frames', exist_ok=True) only = [float(x) for x in sys.argv[sys.argv.index('frames') + 1:]] or None async def go(): async with async_playwright() as p: b = await p.chromium.launch(); pg = await b.new_page(viewport={'width': 1920, 'height': 1080}) await pg.goto('http://localhost:8766/film/film.html'); await pg.wait_for_timeout(600) fr = only if only else range(int(DUR * FPS)) for f in fr: tt = f if only else f / FPS await pg.evaluate('setT(%f)' % tt) await pg.screenshot(path=('/tmp/film_%s.jpg' % f) if only else (F + 'frames/%05d.jpg' % f), type='jpeg', quality=88) await b.close() asyncio.run(go()) if 'audio' in sys.argv: sr = 24000; n = int(DUR * sr); mix = np.zeros(n, 'float32') for i, a0 in enumerate(ST): a, _ = sf.read(F + 'u%d.wav' % i, dtype='float32'); s = int(a0 * sr); mix[s:s + len(a)] += a * .95 def note(f, dur, amp): tt = np.arange(int(dur * sr)) / sr; env = np.exp(-tt * 2.2) * (1 - np.exp(-tt * 60)) w = sum((.6 ** h) * np.sin(2 * np.pi * f * (h + 1) * tt * (1 + .0004 * h)) for h in range(5)) return (w * env * amp).astype('float32') def hz(m): return 440 * 2 ** ((m - 69) / 12) prog = [[48, 60, 64, 67, 72], [43, 55, 59, 62, 67], [45, 57, 60, 64, 69], [41, 53, 57, 60, 65]] music = np.zeros(n + sr * 4, 'float32'); bar = 2.6; t0 = 0.; k = 0 while t0 < DUR: ch = prog[k % 4]; m = note(hz(ch[0]), 3.2, .16); s = int(t0 * sr); music[s:s + len(m)] += m for j, mm in enumerate([ch[1], ch[2], ch[3], ch[4], ch[3], ch[2]]): nn = note(hz(mm), 1.8, .07); s = int((t0 + j * bar / 6) * sr); music[s:s + len(nn)] += nn t0 += bar; k += 1 music = music[:n]; tt = np.arange(n) / sr; music *= np.clip(tt / 1.5, 0, 1) * np.clip((DUR - tt) / 3, 0, 1) voice = np.abs(mix); env = np.convolve(voice, np.ones(sr // 5) / (sr // 5), 'same'); duck = 1 - .6 * np.clip(env * 14, 0, 1) # music sits about 20 dB under the voice, and ducks further while it speaks vr = np.sqrt(np.mean(mix[np.abs(mix) > .01] ** 2)); mr = np.sqrt(np.mean(music ** 2)) + 1e-9 music *= (vr * 10 ** (-20 / 20)) / mr out = mix + music * duck; out *= .92 / np.abs(out).max() sf.write(F + 'mix.wav', out, sr); print('audio ok') if 'video' in sys.argv: os.makedirs(K + 'out/film', exist_ok=True) subprocess.run(['ffmpeg', '-y', '-loglevel', 'error', '-framerate', str(FPS), '-i', F + 'frames/%05d.jpg', '-i', F + 'mix.wav', '-c:v', 'libx264', '-preset', 'veryfast', '-crf', '25', '-pix_fmt', 'yuv420p', '-c:a', 'aac', '-b:a', '128k', '-shortest', '-movflags', '+faststart', K + 'out/film/film.mp4'], check=True) pf = int((ST[5] + .9) * FPS) subprocess.run(['ffmpeg', '-y', '-loglevel', 'error', '-i', F + 'frames/%05d.jpg' % pf, '-vf', 'scale=1280:-1', '-q:v', '4', K + 'out/film/film.jpg'], check=True) print('video ok')