I render short explainer videos from code, and for weeks the voice was the one part I paid for per request. It is now a 50-line script running on the same CPU-only container that renders the frames. This post is that script, the two traps I hit installing it, and the one design decision that made the rest of the pipeline simpler: every line of narration becomes its own WAV, and the visuals are timed from the audio, not the other way round.
Disclosure: I run small data pipelines and AI-agent automations, and this voice setup is part of them. All code is in a public repo, linked below. Kokoro is an open model by its own authors; I'm just a user.
The pieces
| Piece | What it does |
|---|---|
| Kokoro-82M v1.0 (hexgrad/kokoro) | the speech model (82M parameters), Apache-2.0 weights |
| kokoro-onnx releases | ONNX export: kokoro-v1.0.onnx (326 MB) + voices-v1.0.bin (28 MB) |
| onnxruntime | runs the model on CPU |
| misaki (Apache-2.0) + spaCy | text → phonemes (grapheme-to-phoneme, "G2P") |
The model doesn't read text; it reads phonemes. Getting from "Only six answered" to phonemes is the job of misaki, Kokoro's own G2P library. By default it falls back to espeak-ng for words it doesn't know. I switch that fallback off: no GPL dependency in the stack, and, more useful in practice, unknown words get reported instead of silently guessed.
Install: two traps
python3.12 -m venv venv && . venv/bin/activate
pip install onnxruntime numpy spacy num2words addict regex
pip install --no-deps misaki==0.9.4
pip install https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.8.0/en_core_web_sm-3.8.0-py3-none-any.whl
Trap 1: Python 3.13. misaki 0.9.4 declares Requires-Python: <3.13,>=3.8, so on 3.13 pip simply can't find a matching version. My container's default python was 3.13; a separate 3.12 venv fixed it.
Trap 2: the misaki[en] extra pulls in more than you need. The English extra adds the espeak-ng loader, a phonemizer and spacy-curated-transformers. If you only want the lexicon + spaCy path without the espeak fallback, --no-deps plus the five small packages above is enough (misaki's own required deps are just addict and regex). The spaCy model comes straight from its GitHub release, which also helps when your environment can't reach other model hosts.
The model wrapper (core)
The full file is tts.py. The interesting parts:
def g2p(self, british: bool):
from misaki import en
# fallback=None: no espeak; unknown words come back with empty phonemes
return en.G2P(trf=False, british=british, fallback=None, unk='')
def voice(self, spec: str) -> np.ndarray:
"""'am_michael' or a blend like 'am_michael:0.6,am_onyx:0.4'."""
parts = [p.split(':') for p in spec.split(',') if p.strip()]
total, acc = 0.0, None
for p in parts:
w = float(p[1]) if len(p) > 1 else 1.0
v = self.voices[p[0].strip()] * w
acc = v if acc is None else acc + v
total += w
return acc / total
A "voice" in Kokoro is a pack of style vectors, so a blend is just a weighted average. am_michael:0.6,am_onyx:0.4 gave me a narrator voice that sounded calmer than either voice alone. That's taste, not a benchmark, but blending costs nothing to try.
Unknown words are collected per run:
ps, tokens = g2p(text)
for t in tokens:
if t.phonemes in (None, '') and re.search(r'[A-Za-z0-9]', t.text or ''):
self.unknown.add(t.text)
In my test script the only unknown word was, fittingly, "Kokoro". misaki's inline syntax fixes it in the text itself, with no code change: [Kokoro](/kˈOkəɹO/) runs fine. Numbers, "$4.50" and "SWE-bench 2026" all went through without reports. I still write important numbers as words ("twenty four"), because I want to control how they're said, not just that they're pronounceable.
Script → WAVs → timeline
This is the part that changed my video pipeline. voiceover.py takes a list of lines and writes one trimmed WAV per line plus a timeline:
t, timeline = 0.4, [] # short lead-in
for ln in json.load(open(a.lines)):
audio = tts.trim(k.synth(ln["text"], voice=a.voice, speed=a.speed))
write_wav(os.path.join(a.out, ln["id"] + ".wav"), audio)
dur = round(len(audio) / tts.SR, 3)
timeline.append({"id": ln["id"], "start": round(t, 3), "dur": dur, "text": ln["text"]})
t += dur + ln.get("gap", a.gap) # per-line pause, default 0.35 s
Real output for a two-line script (--speed 1.05):
{"duration": 5.983, "lines": [
{"id": "l1", "start": 0.4, "dur": 1.761, "text": "Twenty four shops. One bot."},
{"id": "l2", "start": 2.761, "dur": 2.272, "text": "Only six answered all three questions."}]}
The animation then never uses hard-coded seconds. Scenes are attached to line IDs: "show the counter when l2 starts". Rewrite a sentence, regenerate, and every scene after it moves with the audio. Before this, changing one word meant re-timing half a video by hand. Per-line files also make the mix easy: duck the music under each [start, start+dur] window, and leave it up in the gaps.
How fast is it on a plain CPU?
Measured on a 4-vCPU cloud container with no GPU, four sentences, 14.6 s of speech, voice blend as above, model already loaded:
| Threads | Synthesis time | Real-time factor |
|---|---|---|
| 2 | 7.2 s | 0.49 |
| 4 | 4.6 s | 0.31 |
So a 50-second narration takes about 15–25 s to generate. The first run in a fresh process is slower (it loads a 326 MB model and spaCy). In my case the cold run of the same script took 28.6 s, a warm one 9.3 s, so batch all lines in one process instead of calling the script per line. --speed 1.05 shortened the same lines by about 3%, which is enough to tighten pacing without sounding rushed.
What I'd watch out for
-
Lexicon gaps are a feature. Read
unknownWordsafter every run; brand names and acronyms are where it happens. -
Trim the edges. The model returns some silence at the start and end; the wrapper trims below an amplitude threshold and keeps 50 ms padding, so your
gapvalues mean what they say. - Long text: I split phoneme strings at punctuation into ≤500-token chunks per inference pass; one sentence per line avoids that path entirely and suits subtitles anyway.
- Label it. If the voice is used publicly, say it's AI-generated. I put that in every video description.
All files (wrapper, vocab, script, README) are in data-recipes/snippets/kokoro-voiceover.
Question: if you generate narration for videos or podcasts, do you time visuals from the audio like this, or generate the audio to fit a fixed edit? I'm curious which way breaks less often for you.
Top comments (0)