Generating narrated screencast tutorials from a script (PIL + Piper TTS + ffmpeg)
videotutorialsttspiperffmpegpillowpythonscreencastautomation
Programming tutorials can be produced without recording a screen or a voice: write each episode as a small script of narration lines and editor/terminal actions, then render it to a video. Frames are drawn with Pillow, narration comes from a local text-to-speech engine, and ffmpeg encodes the result. In 2026-11 eight 1 to 1.5 minute H language tutorials were made this way and published on videos.jhampton.io; all eight render in parallel in about 40 seconds.
The episode script
A small line-based DSL. A beat is one narration line (spoken and shown as a caption) plus the screen actions that play while it is spoken:
# title: Hello, H
> Programs live in files ending in dot underscore.
@file hello._
@type echo "hello";
> Compile and run it.
@run hc hello._ && ./hello
Useful directives: @file (new editor buffer), @type (typed at a fixed characters-per-second rate, multi-line up to @end), @set (show code instantly), @erase N / @eraseline, @write name ... @end (a data file the program reads, not shown), @run cmd (type a command in the terminal pane and show its output), @clear, @mark A-B (highlight lines), @pause S. A caption can carry a separate spoken form: > Caption || what the voice says.
Run the real code at render time
Every @run really executes in a temp dir, and the video shows the actual output, including compiler errors. Write every command and its output to a log next to the video. After any language change, re-render and read the log: if the narration claims something the compiler no longer does, the log shows it. This is the main advantage over hand-made screencasts, which silently go stale.
- Clear test-only environment (here H_SEED, which fixes the random seed) so the demo behaves like a user's run, and put the tool's install dir on PATH.
Narration with Piper
Piper is a fast, offline neural TTS. Install it in its own venv (pip install piper-tts) and download a voice model (en_US-ryan-high.onnx from the rhasspy/piper-voices collection on Hugging Face). Each beat is synthesized to its own WAV; the beat lasts as long as the longer of its audio and its screen actions.
- Rewrite text for the ear before synthesis: code reads badly aloud. Strip backticks, and map tokens to words: hc → "H C", ._ → "dot underscore", ++ → "plus plus", == → "equals equals", fn → "F N", and the single letter H → "aitch" (otherwise TTS may say "huh"). Listen to at least one episode; a rule list never catches everything.
Rendering
- Draw each frame with Pillow at 1920×1080: an editor pane with syntax highlighting (a single regex tokenizer is enough for a small language), a terminal pane, and a caption bar. Use a real monospace font (Fira Mono) and compute character width once.
- Pipe raw RGB frames to ffmpeg's stdin (
-f rawvideo -pix_fmt rgb24 -s 1920x1080 -r 30 -i -) and encode H.264 (-preset medium -crf 20 -tune stillimage -pix_fmt yuv420p). Cache frames by screen state: most frames are identical (narration over static code), so reuse the bytes instead of redrawing. - Mix the narration into one track by writing each beat's PCM at its start time (plus a short lead-in) into a silent buffer, then mux:
-c:v copy -c:a aac -b:a 128k -shortest -movflags +faststart. - Save the title card as the poster image.
- Run the renderer with whichever Python has Pillow; Piper is called as a subprocess from its own venv, so the two environments never need to merge.
Publishing
Upload newest-first if the site lists new items at the top, so a series reads 1, 2, 3 in order. Keep a README with the re-render + republish recipe, because a language change means replacing videos, not editing them.
See also
- Scripted video editing with FilmCraft (CLI/MCP), whisper transcripts, and a dashcam highlight reel
- Encoding phone clips for the web (HEVC remux, H.264 fallback)
- Generating app assets with Python scripts (logos, GIFs, sounds)
- Designing a programming language's syntax and semantics (lessons from H)