---
name: kokoro-local-tts-batch
description: Generate zero-API-cost offline commercial narration batches using Apache 2.0 Kokoro-82M on CPU or GPU, resample native 24 kHz PCM WAV to broadcast 48 kHz stereo via FFmpeg, normalize to -14 LUFS integrated (-1.0 dBTP), and write a scene timing manifest JSON for video editors or Sync Lipsync 2.0. Use when producing NDA rough cuts, faceless explainers, or high-volume narration without cloud TTS fees.
---

# Kokoro-82M Local Apache 2.0 TTS Batch Playbook

## 1. Environment Check & One-Time Setup
Verify Python 3.10+, `espeak-ng` (phonemizer), `kokoro`, `soundfile`, and `ffmpeg` before rendering:
```bash
# macOS Homebrew: brew install espeak-ng ffmpeg
# Python venv:    pip install -q kokoro soundfile
python3 -c "import kokoro, soundfile; print('kokoro ready')"
```
If the user wants zero local Python setup, fall back cleanly to fal.ai `fal-ai/kokoro/american-english` (`$0.02 / 1,000 chars`) and note the exact cost first.

## 2. Batch Render Native 24 kHz Stems per Scene
Write a clean deterministic runner reading `scenes.json` (`[{"id":"s01","voice":"af_heart","speed":1.0,"text":"..."}]`):
```python
import json, pathlib, numpy as np, soundfile as sf
from kokoro import KPipeline

out_dir = pathlib.Path("audio_raw_24k")
out_dir.mkdir(exist_ok=True)
pipe = KPipeline(lang_code="a")  # 'a' American EN, 'b' British EN, 'e' ES, 'f' FR, 'j' JA

for row in json.loads(pathlib.Path("scenes.json").read_text()):
    chunks = [audio for _, _, audio in pipe(row["text"], voice=row.get("voice", "af_heart"), speed=row.get("speed", 1.0))]
    wav = np.concatenate(chunks)
    sf.write(out_dir / f"{row['id']}.wav", wav, 24000, subtype="PCM_16")
```

## 3. Mandatory Broadcast Conform (24 kHz → 48 kHz Stereo, -14 LUFS)
Never drop raw 24 kHz mono WAV straight into a 48 kHz video timeline (prevents NLE resampling artifacts and uneven volume). Conform every scene stem with FFmpeg and log exact duration:
```bash
mkdir -p audio_master_48k
for f in audio_raw_24k/*.wav; do
  base=$(basename "$f")
  ffmpeg -y -i "$f" \
    -af "aresample=48000,pan=stereo|c0=c0|c1=c0,loudnorm=I=-14:TP=-1.0:LRA=11" \
    -c:a pcm_s16le -ar 48000 "audio_master_48k/$base"
  dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "audio_master_48k/$base")
  echo "$base -> ${dur}s (48kHz stereo -14 LUFS)"
done
```

## 4. Verification Gate
Confirm every stem exists, is non-silent, matches `sample_rate=48000` and `channels=2`, and emit `audio_manifest.json` containing scene IDs and exact durations in seconds so downstream Remotion, FFmpeg beat-sync, or Sync Lipsync 2.0 jobs align cleanly.
