SAGEA LogoDocs & API
CookbooksVoice Cookbooks

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.
  • ffmpeg and ffprobe installed 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.

wc -m chapter1.txt
head -c 500 chapter1.txt

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.

curl -X POST https://api.sagea.space/v1/audio/speech \
  -H "Authorization: Bearer $SAGEA_API_KEY" -H "Content-Type: application/json" \
  -d '{"model": "sonus-tts", "input": "Chapter one. It was a quiet morning.", "voice": "nova", "response_format": "mp3", "emotion": "calm", "speed": 1.0}' \
  --output part_000.mp3

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:

ls part_*.mp3 | sort | sed "s/^/file '/; s/$/'/" > concat.txt
ffmpeg -f concat -safe 0 -i concat.txt -c copy chapter1_raw.mp3
ffmpeg -i chapter1_raw.mp3 -c copy -metadata title="Chapter 1: The Beginning" -metadata artist="Sonus Narrator" chapter1.mp3
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1 chapter1.mp3

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.

ErrorCauseFix
401 UnauthorizedAPI key missing or invalidRe-export SAGEA_API_KEY and retry
400 text_too_longA chunk exceeds 4096 charsRe-split at sentence boundaries under 3800 chars
422 voice_not_foundVoice name misspelledUse nova or a valid voice_id starting with vox_
429 rate_limit_exceededOver 50 req per min on Starter, 500 on ProSlow the loop or use batch endpoint for discount

What's next

On this page