Repository navigation
Expand file tree
/
Copy pathbuild.py
More file actions
342 lines (297 loc) · 15.1 KB
/
Copy pathbuild.py
File metadata and controls
342 lines (297 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python3
"""Render a scene and produce the finished video with narration and captions.
python build.py scene01_vertical_city # 1080p30 (default)
python build.py scene01_vertical_city -q l # fast 480p preview
python build.py scene01_vertical_city -q k # 4K
python build.py scene01_vertical_city --voice am_michael
python build.py scene01_vertical_city --skip-render # re-mix audio only
python build.py 2/scene00_title # <film>/<scene> when the name is in several films
Scenes live in films/filmNN_<name>/ (see films.py). The film can be given by
number (2), prefix (film02) or folder name; a bare scene name works when only
one film has it. Output goes to out/<film folder>/<scene>.mp4.
Steps: manim render -> Kokoro TTS for each line of <scene>.voice.txt ->
mix narration with a tick track synced to the escapement (and any sound
effects listed in <scene>.sfx.txt) -> write .srt ->
mux into out/<scene>.mp4 (with soft subtitles).
Requires ffmpeg on PATH. Kokoro model files (~350 MB) are downloaded once
into models/ on first use.
"""
import argparse, glob, hashlib, os, re, subprocess, sys, urllib.request
import numpy as np
from films import find_scene
HERE = os.path.dirname(os.path.abspath(__file__))
MODELS = os.path.join(HERE, "models")
CACHE = os.path.join(HERE, "build_cache")
OUT = os.path.join(HERE, "out")
SR = 48000
KOKORO_URL = "https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files-v1.0/"
KOKORO_FILES = ["kokoro-v1.0.onnx", "voices-v1.0.bin"]
# Words Kokoro mispronounces, respelled for the voice only (captions keep the real
# spelling). Kokoro reads "wound" as the injury and "wind" as moving air; in these
# films both always mean winding. "ETA" (the movement maker) is read as letters.
# "Brocot" (of the Stern-Brocot tree) is French: final t silent.
# Film 4: "Elinvar" is EL-in-var (Kokoro says ih-LIN-var), "Nivarox" NEE-va-rox,
# "masselottes" mass-LOTS (French).
# Film 5: "Breguet" is breh-GAY (Kokoro says BREG-it), "Edouard" AY-dwar; Kokoro reads years
# digit by digit ("one thousand seven hundred ..."), so they are spelled out.
# Film 6: "Petrangeli" is Italian, pe-tran-JEL-li (Kokoro stresses the first syllable).
# Whole words, case-insensitive.
PRONOUNCE = {"wound": "wownd", "wind": "wined", "eta": "E-T-A", "brocot": "broco",
"elinvar": "Ellinvar", "nivarox": "Neevarox", "masselottes": "mass-lots",
"breguet": "Bregay", "édouard": "Ehdwahr",
"1795": "seventeen ninety-five", "1860": "eighteen sixty",
"petrangeli": "Petran-jelly", "1826": "eighteen twenty-six"}
QUALITY = {"l": "480p15", "m": "720p30", "h": "1080p30", "p": "1440p60", "k": "2160p60"}
def run(cmd):
print(" $", " ".join(cmd))
subprocess.run(cmd, check=True)
def scene_class(py):
m = re.search(r"^class\s+(\w+)\s*\(", open(py).read(), re.M)
if not m:
sys.exit(f"no Scene class found in {py}")
return m.group(1)
def read_voice(path):
lines = []
for raw in open(path, encoding="utf-8"):
raw = raw.strip()
if not raw or raw.startswith("#"):
continue
t, text = raw.split("|", 1)
lines.append((float(t), text.strip()))
return lines
def ensure_models():
os.makedirs(MODELS, exist_ok=True)
for f in KOKORO_FILES:
p = os.path.join(MODELS, f)
if not os.path.exists(p):
print(f"downloading {f} (one time) ...")
urllib.request.urlretrieve(KOKORO_URL + f, p)
def spoken(text):
"""The text as Kokoro should say it (see PRONOUNCE)."""
def fix(m):
w = PRONOUNCE[m.group(0).lower()]
return w[0].upper() + w[1:] if m.group(0)[0].isupper() else w
return re.sub(r"\b(" + "|".join(PRONOUNCE) + r")\b", fix, text, flags=re.I)
def tts(lines, voice, speed):
"""Return [(start, samples)] at SR; each sentence is cached by text+voice."""
import soundfile as sf
os.makedirs(CACHE, exist_ok=True)
kokoro, out = None, []
for start, text in lines:
text = spoken(text)
key = hashlib.sha1(f"{voice}|{speed}|{text}".encode()).hexdigest()[:16]
wav = os.path.join(CACHE, f"tts_{key}.wav")
if not os.path.exists(wav):
if kokoro is None:
ensure_models()
from kokoro_onnx import Kokoro
kokoro = Kokoro(os.path.join(MODELS, KOKORO_FILES[0]), os.path.join(MODELS, KOKORO_FILES[1]))
audio, sr = kokoro.create(text, voice=voice, speed=speed, lang="en-us")
sf.write(wav, audio, sr)
audio, sr = sf.read(wav)
if sr != SR: # simple linear resample (Kokoro outputs 24 kHz)
x = np.arange(len(audio)) / sr
audio = np.interp(np.arange(int(len(audio) * SR / sr)) / SR, x, audio)
out.append((start, audio))
return out
def ticks(duration, beats, level, times=None):
"""Steady ticks at `beats` per second; or, when `times` is given (from a scene's
.ticks.txt, e.g. for slow motion), one tick at each listed time."""
rng = np.random.default_rng(1)
n = int(SR * duration)
out = np.zeros(n)
L = int(0.012 * SR)
env = np.exp(-np.linspace(0, 9, L))
tt = np.arange(L) / SR
starts = [k / beats for k in range(int(duration * beats))] if times is None else times
for k, ts in enumerate(starts):
t0 = int(ts * SR)
f = 4200 if k % 2 == 0 else 3700 # alternate tick / tock
if t0 + L < n:
out[t0:t0 + L] += (np.sin(2 * np.pi * f * tt) * 0.6 + rng.normal(0, 0.4, L)) * env
t = np.arange(n) / SR
return out * np.clip(t / 1.6, 0, 1) * np.clip((duration - t) / 1.5, 0, 1) * level
def click_sound():
"""A short metallic click: a filtered noise burst with a bright ring."""
rng = np.random.default_rng(7)
L = int(0.018 * SR)
t = np.arange(L) / SR
env = np.exp(-t / 0.0028)
s = (np.sin(2 * np.pi * 5200 * t) * 0.5 + np.sin(2 * np.pi * 2900 * t) * 0.35 + rng.normal(0, 0.5, L)) * env
return s / np.max(np.abs(s))
def tone_sound(freq, decay, noise):
"""A tick with a clear pitch: a decaying tone over a short noise transient."""
rng = np.random.default_rng(11)
L = int(0.09 * SR)
t = np.arange(L) / SR
s = (np.sin(2 * np.pi * freq * t) + 0.3 * np.sin(2 * np.pi * 2 * freq * t)) * np.exp(-t / decay)
s += noise * rng.normal(0, 1, L) * np.exp(-t / 0.002)
return s / np.max(np.abs(s))
# Film 5 scene 07's listening demo: two distinct tick sounds, a low wood block and a high bell.
SOUNDS = {"click": click_sound,
"wood": lambda: tone_sound(900, 0.018, 0.5),
"bell": lambda: tone_sound(2400, 0.035, 0.3)}
def read_sfx(path):
"""<scene>.sfx.txt: one `time_in_seconds | sound [| level]` per line (sound: click, wood, bell)."""
out = []
for raw in open(path, encoding="utf-8"):
raw = raw.strip()
if not raw or raw.startswith("#"):
continue
parts = [x.strip() for x in raw.split("|")]
out.append((float(parts[0]), parts[1], float(parts[2]) if len(parts) > 2 else 1.0))
return out
def srt_time(s):
ms = int(round(s * 1000))
return f"{ms // 3600000:02d}:{ms // 60000 % 60:02d}:{ms // 1000 % 60:02d},{ms % 1000:03d}"
CAPTION_CHARS = 42 # per caption line, the usual subtitle limit
def caption_chunks(text, max_lines=2, width=CAPTION_CHARS):
"""Split one narration line into caption cues of at most `max_lines` lines of
`width` characters, breaking at sentence ends, then at commas, colons and
semicolons, then between words. Returns a list of cue texts (lines joined by \\n)."""
import textwrap
limit = max_lines * width - 6 # a little slack, so word breaks never force a third line
pieces = re.split(r"(?<=[.!?])\s+", text.strip())
out = []
for p in pieces:
if len(p) <= limit:
out.append(p)
continue
parts, cur = re.split(r"(?<=[,;:])\s+", p), ""
for q in parts:
if cur and len(cur) + 1 + len(q) > limit:
out.append(cur)
cur = q
else:
cur = f"{cur} {q}" if cur else q
if cur:
out.append(cur)
cues = []
for c in out: # any piece still too long: split between words
while len(c) > limit:
cut = c.rfind(" ", 0, limit)
cut = cut if cut > 0 else limit
cues.append(c[:cut])
c = c[cut:].strip()
cues.append(c)
merged = [] # rejoin short neighbors that fit together
for c in cues:
if merged and len(merged[-1]) + 1 + len(c) <= limit and not re.search(r"[.!?]$", merged[-1]):
merged[-1] += " " + c
else:
merged.append(c)
# one line if it fits; otherwise two lines of about equal length
def two_lines(c):
if len(c) <= width:
return c
rows = textwrap.wrap(c, width=min(width, len(c) // 2 + 8))
return "\n".join(rows if len(rows) <= max_lines else textwrap.wrap(c, width=width))
return [two_lines(c) for c in merged]
def write_srt(path, lines, clips, short):
"""One cue per narration line; with `short`, each line is split into cues of at
most two short lines, timed in proportion to their length."""
n = 0
with open(path, "w", encoding="utf-8") as f:
for (start, audio), (_, text) in zip(clips, lines):
end = start + len(audio) / SR
if not short:
n += 1
f.write(f"{n}\n{srt_time(start)} --> {srt_time(end + 0.2)}\n{text}\n\n")
continue
cues = caption_chunks(text)
weights = [len(c) + 8 for c in cues] # +8: each cue's pause is roughly constant
t = start
for k, (c, w) in enumerate(zip(cues, weights)):
t1 = start + (end - start) * sum(weights[:k + 1]) / sum(weights)
n += 1
f.write(f"{n}\n{srt_time(t)} --> {srt_time(t1 + (0.2 if k == len(cues) - 1 else 0.0))}\n{c}\n\n")
t = t1
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("scene", help="scene name, e.g. scene01_vertical_city, or <film>/<scene>, e.g. 2/scene00_title")
ap.add_argument("-q", "--quality", default="h", choices=QUALITY, help="l=480p m=720p h=1080p p=1440p k=4K")
ap.add_argument("--voice", default="bm_george", help="Kokoro voice, e.g. bm_george, am_michael, bf_emma")
ap.add_argument("--speed", type=float, default=0.92, help="narration speed (1.0 = Kokoro default)")
ap.add_argument("--tick-level", type=float, default=0.05, help="0 disables the tick track")
ap.add_argument("--sfx-level", type=float, default=0.16, help="level of sound effects from <scene>.sfx.txt; 0 disables")
ap.add_argument("--skip-render", action="store_true", help="reuse the last render, redo audio only")
a = ap.parse_args()
film, name = find_scene(a.scene)
fdir = os.path.join(HERE, "films", film)
py = os.path.join(fdir, name + ".py")
if not os.path.exists(py):
sys.exit(f"no scene file {os.path.relpath(py, HERE)}")
cls = scene_class(py)
fps_flag = ["--fps", "30"] if a.quality == "h" else []
media = os.path.join(HERE, "media", film) # one media dir per film: scene names repeat across films
# scenes import the shared movement.py (repo root) and any helpers in their own film folder
env = dict(os.environ, PYTHONPATH=os.pathsep.join([HERE, fdir] + [p for p in [os.environ.get("PYTHONPATH")] if p]))
if not a.skip_render:
print(f"[1/4] rendering {film}/{name} ({cls}) at {QUALITY[a.quality]}")
print(" $ manim", f"-q{a.quality}", *fps_flag, "--disable_caching", "--media_dir", media, py, cls)
subprocess.run(["manim", f"-q{a.quality}", *fps_flag, "--disable_caching", "--media_dir", media, py, cls],
check=True, env=env)
video = os.path.join(media, "videos", name, QUALITY[a.quality], cls + ".mp4")
if not os.path.exists(video):
cands = sorted(glob.glob(os.path.join(media, "videos", name, "*", cls + ".mp4")), key=os.path.getmtime)
if not cands:
sys.exit("no rendered video found; run without --skip-render")
video = cands[-1]
dur = float(subprocess.check_output(["ffprobe", "-v", "error", "-show_entries", "format=duration",
"-of", "csv=p=0", video]).decode())
print(f"[2/4] narration ({a.voice})")
voice_file = os.path.join(fdir, name + ".voice.txt")
lines = read_voice(voice_file) if os.path.exists(voice_file) else []
clips = tts(lines, a.voice, a.speed)
tail = max((s + len(c) / SR for s, c in clips), default=0.0) + 0.4
pad = max(0.0, tail - dur)
if pad > 0:
print(f" WARNING: the narration runs {pad:.2f} s past the end of the animation; the last frame is held"
f" to fit. Lengthen the scene's final hold (or move the line earlier) to fix it properly.")
dur += pad
print("[3/4] mixing audio")
import soundfile as sf
mix = np.zeros(int(SR * dur))
vo = np.zeros_like(mix)
for start, audio in clips:
i = int(start * SR)
seg = audio[: max(0, len(vo) - i)]
vo[i:i + len(seg)] += seg
peak = np.max(np.abs(vo)) if vo.any() else 1.0
mix += vo * (0.8 / peak) # narration peaks at -2 dBFS
if a.tick_level > 0:
sys.path.insert(0, HERE)
from movement import BEATS
tick_file = os.path.join(fdir, name + ".ticks.txt") # optional, written by slow-motion scenes
times = None
if os.path.exists(tick_file):
times = [float(x) for x in open(tick_file) if x.strip() and not x.startswith("#")]
mix += ticks(dur, BEATS, a.tick_level, times)
out = os.path.join(OUT, film)
sfx_file = os.path.join(fdir, name + ".sfx.txt")
if a.sfx_level > 0 and os.path.exists(sfx_file):
bank = {}
for t0, snd, lvl in read_sfx(sfx_file):
clip = bank.setdefault(snd, SOUNDS[snd]())
i = int(t0 * SR)
seg = clip[: max(0, len(mix) - i)]
mix[i:i + len(seg)] += seg * a.sfx_level * lvl
os.makedirs(out, exist_ok=True)
wav = os.path.join(CACHE, f"{film}_{name}_mix.wav")
sf.write(wav, np.clip(mix, -1, 1), SR)
srt = os.path.join(out, name + ".srt")
from films import running_order
short = bool(running_order(film).get("SHORT_CAPTIONS", False)) # opt-in per film (Film 6 on)
write_srt(srt, lines, clips, short)
print("[4/4] muxing")
final = os.path.join(out, name + ".mp4")
subs = ["-i", srt] if clips else [] # ffmpeg rejects an empty .srt (e.g. title cards)
sub_maps = ["-map", "2", "-c:s", "mov_text", "-metadata:s:s:0", "language=eng"] if clips else []
vcodec = ["-c:v", "copy"] if pad == 0 else \
["-vf", f"tpad=stop_mode=clone:stop_duration={pad:.3f}", "-c:v", "libx264", "-crf", "16", "-pix_fmt", "yuv420p"]
run(["ffmpeg", "-v", "error", "-y", "-i", video, "-i", wav, *subs,
"-map", "0:v", "-map", "1:a", *sub_maps, *vcodec, "-c:a", "aac", "-b:a", "192k",
"-t", f"{dur:.3f}", final])
print(f"\ndone: {os.path.relpath(final)} ({dur:.1f} s)")
if __name__ == "__main__":
main()