Build an Audiobook Generator Using ElevenLabs API
Why an Audiobook Generator? If you’ve ever wanted to turn a long‑form article, a novel, or a set of documentation pages into a spoken‑word experience, you’re not alone. Audiobooks are booming, and with modern text‑to‑
Why an Audiobook Generator?
If you’ve ever wanted to turn a long‑form article, a novel, or a set of documentation pages into a spoken‑word experience, you’re not alone. Audiobooks are booming, and with modern text‑to‑speech (TTS) services you can spin up a fully‑automated pipeline in a single afternoon. In this guide we’ll walk through building a simple audiobook generator that pulls plain text, sends it to the ElevenLabs API, and stitches the resulting MP3s together into a ready‑to‑listen file.
TL;DR – Grab an API key from ElevenLabs, split your text into manageable chunks, call the TTS endpoint, and concatenate the audio files. All of this can be done with a few lines of Python (or a curl one‑liner if you prefer).
What Makes ElevenLabs a Good Fit?
ElevenLabs has quickly become a go‑to for developers who need high‑quality, natural‑sounding speech. Their neural models support multiple voices, custom voice cloning, and a straightforward REST API. The pricing tier includes a generous free quota, which is perfect for prototyping an audiobook generator. You can sign up and get your API key here: https://try.elevenlabs.io/kr07zfuqn1bp
Prerequisites
| What you need | Why |
|---|---|
| Python 3.8+ (or Node.js) | To run the script that talks to the API |
requests (Python) or axios (JS) |
Simple HTTP client |
| An ElevenLabs API key | Authenticates your calls |
Source text (plain .txt or markdown) |
The content you’ll convert |
ffmpeg (optional) |
Concatenates MP3 chunks into a single file |
If you’re on a Unix‑like system, installing ffmpeg is as easy as:
# macOS (Homebrew)
brew install ffmpeg
# Ubuntu/Debian
sudo apt-get install ffmpeg
Step 1 – Grab an API Key
Head over to the ElevenLabs sign‑up page, create an account, and copy the API key from the dashboard. Keep it secret – treat it like a password.
ELEVENLABS_API_KEY=your_secret_key_here
You’ll reference this environment variable in the code examples below.
Step 2 – Prepare Your Text
Audiobook TTS works best when the input is split into chunks of ~2 000 characters. This keeps latency low and avoids hitting the maximum token limit.
def split_text(text, max_len=2000):
"""Yield text chunks no longer than max_len, breaking on sentence boundaries."""
import re
sentences = re.split(r'(?<=[.!?])\s+', text)
chunk = ''
for s in sentences:
if len(chunk) + len(s) + 1 > max_len:
yield chunk.strip()
chunk = s
else:
chunk += ' ' + s
if chunk:
yield chunk.strip()
Load your source file and split it:
with open('my_book.txt', encoding='utf-8') as f:
raw_text = f.read()
chunks = list(split_text(raw_text))
print(f'Created {len(chunks)} chunks for synthesis')
Step 3 – Call the ElevenLabs TTS Endpoint
Python (requests)
import os, requests, json, pathlib
API_KEY = os.getenv('ELEVENLABS_API_KEY')
VOICE_ID = '21m00Tcm4TlvDq8ikWAM' # default "Rachel" voice – change as needed
OUTPUT_DIR = pathlib.Path('audio_chunks')
OUTPUT_DIR.mkdir(exist_ok=True)
def synthesize(text, idx):
url = f'https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}'
headers = {
'xi-api-key': API_KEY,
'Content-Type': 'application/json'
}
payload = {
"text": text,
"model_id": "eleven_monolingual_v1",
"voice_settings": {
"stability": 0.75,
"similarity_boost": 0.85
}
}
resp = requests.post(url, headers=headers, json=payload)
resp.raise_for_status()
# Save the raw MP3 bytes
out_path = OUTPUT_DIR / f'chunk_{idx:04}.mp3'
out_path.write_bytes(resp.content)
print(f'✅ Saved {out_path}')
return out_path
# Iterate over chunks
audio_files = [synthesize(chunk, i) for i, chunk in enumerate(chunks)]
Curl (quick test)
If you just want to sanity‑check a single sentence:
curl -X POST "https://api.elevenlabs.io/v1/text-to-speech/21m00Tcm4TlvDq8ikWAM" \
-H "xi-api-key: $ELEVENLABS_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"text":"Hello, this is a test of ElevenLabs TTS.",
"model_id":"eleven_monolingual_v1",
"voice_settings":{"stability":0.75,"similarity_boost":0.85}
}' \
--output test.mp3
Replace the voice ID with any of the voices you’ve created in the ElevenLabs console. If you want a custom cloned voice, simply swap the VOICE_ID with the ID you receive after uploading a few minutes of reference audio.
Step 4 – Stitch the Audio Together
Now that each chunk lives as an individual MP3, we can concatenate them with ffmpeg:
# Create a file list for ffmpeg
for f in audio_chunks/*.mp3; do echo "file '$PWD/$f'" >> files.txt; done
# Concatenate
ffmpeg -f concat -safe 0 -i files.txt -c copy audiobook.mp3
The resulting audiobook.mp3 should play seamlessly from start to finish. If you prefer to stay in Python, pydub can also handle the merging:
from pydub import AudioSegment
combined = AudioSegment.empty()
for path in sorted(OUTPUT_DIR.glob('chunk_*.mp3')):
combined += AudioSegment.from_mp3(path)
combined.export('audiobook.mp3', format='mp3')
print('🎧 Audiobook ready: audiobook.mp3')
Optional: Voice Cloning for a Personal Touch
ElevenLabs lets you clone a voice from as little as 30 seconds of audio. After you upload the reference file in the dashboard, you’ll receive a new VOICE_ID. Plug that ID into the script above and the generated audiobook will sound like your narrator (or any public figure you have rights to use). The cloning workflow is documented in the ElevenLabs API docs – you can trigger it programmatically, but the UI is often the quickest way to get started.
Error Handling & Rate Limits
-
429 Too Many Requests – The free tier caps requests per minute. If you hit this, add a short
time.sleep(1)between calls. - 500/503 – Temporary service hiccups; retry with exponential back‑off.
-
Invalid API key – Double‑check that
ELEVENLABS_API_KEYis exported correctly.
A robust wrapper could look like this:
import time
def safe_synthesize(text, idx, retries=3):
for attempt in range(retries):
try:
return synthesize(text, idx)
except requests.HTTPError as e:
if e.response.status_code == 429:
wait = 2 ** attempt
print(f'⏳ Rate limited, sleeping {wait}s')
time.sleep(wait)
else:
raise
raise RuntimeError('Failed after several retries')
Putting It All Together
Here’s a minimal “one‑file” version you can copy‑paste and run:
#!/usr/bin/env python3
import os, pathlib, requests, time, re
API_KEY = os.getenv('ELEVENLABS_API_KEY')
VOICE_ID = '21m00Tcm4TlvDq8ikWAM' # change if you have a custom voice
MAX_LEN = 2000
def split_text(text):
sentences = re.split(r'(?<=[.!?])\s+', text)
chunk = ''
for s in sentences:
if len(chunk) + len(s) + 1 > MAX_LEN:
yield chunk.strip()
chunk = s
else:
chunk += ' ' + s
if chunk:
yield chunk.strip()
def synthesize(text, idx):
url = f'https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}'
headers = {'xi-api-key': API_KEY, 'Content-Type': 'application/json'}
payload = {"text": text, "model_id": "eleven_monolingual_v1"}
resp = requests.post(url, headers=headers, json=payload)
resp.raise_for_status()
out = pathlib.Path('audio_chunks') / f'chunk_{idx:04}.mp3'
out.parent.mkdir(exist_ok=True)
out.write_bytes(resp.content)
return out
def main():
text = pathlib.Path('my_book.txt').read_text(encoding='utf-8')
chunks = list(split_text(text))
for i, chunk in enumerate(chunks):
safe = synthesize(chunk, i)
time.sleep(0.2) # polite pause
# concat with ffmpeg afterwards
print('All chunks generated – run the ffmpeg concat step!')
if __name__ == '__main__':
main()
Run it, then fire the ffmpeg concat command from the previous section. You now have a fully functional audiobook pipeline powered by ElevenLabs.
Next Steps
-
Add chapter metadata – Insert silent pauses or chapter titles using
pydub. -
Support multiple languages – ElevenLabs offers language‑specific models; just swap
model_id. - Deploy as a web service – Wrap the logic in a Flask or FastAPI endpoint and let users upload PDFs for instant conversion.
Ready to Give It a Spin?
ElevenLabs makes high‑fidelity TTS accessible to anyone willing to write a few lines of code. Grab your free API key, clone this repo, and start generating audiobooks for your favorite books, documentation, or personal notes. Happy coding!
Try ElevenLabs today: https://try.elevenlabs.io/kr07zfuqn1bp
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.