Audiobook Pipeline with Sonus
Turn a full chapter into one MP3 by chunking text, calling POST https://api.sagea.space/v1/audio/speech, and joining with ffmpeg.
- Split chapter text into chunks under 4096 chars at sentence boundaries
- Synthesize each chunk and concatenate with
ffmpeg - Embed chapter title and track metadata
Time to complete: ~20 minutes
Prerequisites
- Python 3.9+ with
pip install requests. ffmpegandffprobeinstalled for concatenation and metadata.- A SAGEA API key exported as
SAGEA_API_KEY. - Read Synthesize speech with Sonus for synthesis basics.
- Skim the Sonus TTS model card for formats, sample rates, and pricing.
Step 1: Split the chapter at sentence boundaries
Never split mid-sentence. Keep every chunk under 4096 chars. For books, use POST https://api.sagea.space/v1/batch for a 50 percent discount.
We use 3800 chars as a safety margin. Standard narration costs $15 per 1M chars.
Step 2: Synthesize every chunk
Loop over chunk files with fixed voice, emotion, and speed for a consistent narrator.
Run python split_chapter.py, python synth_chunks.py, then ls -lh part_*.mp3.
Step 3: Concatenate with ffmpeg and tag metadata
Join parts in order and embed chapter metadata:
Use 44.1kHz for retail quality. Sonus also supports 16kHz and 24kHz.
Verify
chapter1.mp3 should play end to end with even pacing. If a step fails, check the error table below.
| Error | Cause | Fix |
|---|---|---|
401 Unauthorized | API key missing or invalid | Re-export SAGEA_API_KEY and retry |
400 text_too_long | A chunk exceeds 4096 chars | Re-split at sentence boundaries under 3800 chars |
422 voice_not_found | Voice name misspelled | Use nova or a valid voice_id starting with vox_ |
429 rate_limit_exceeded | Over 50 req per min on Starter, 500 on Pro | Slow the loop or use batch endpoint for discount |
