Convert text to speech using ElevenLabs voice AI. Use when generating audio from text, creating voiceovers, building voice apps, or synthesizing speech in 70+ languages.
Instrucciones de origen · Vista previa de solo lectura
name
text-to-speech
description
Convert text to speech using ElevenLabs voice AI. Use when generating audio from text, creating voiceovers, building voice apps, or synthesizing speech in 70+ languages.
license
MIT
compatibility
Requires internet access and an ElevenLabs API key (ELEVENLABS_API_KEY).
Generate natural speech from text - supports 70+ languages, multiple models for quality vs latency tradeoffs.
Setup: See Installation Guide. For JavaScript, use @elevenlabs/* packages only.
Project defaults — load .env FIRST
Before any TTS call in this repo, load .env and use the project defaults defined there. Pull voice_id, model_id, and all voice_settings from environment variables — do not hardcode them, even in throwaway scripts.
use _SHORTS for vertical 1080×1920 / Shorts compositions, otherwise ELEVENLABS_SPEED
Full snippets (Python / JS / cURL) and the speed-selection rule live in references/voice-settings.md. The Quick Start below shows hardcoded values for illustration only — every real call must read from env.
Quick Start
Python
from elevenlabs import ElevenLabs
client = ElevenLabs()
audio = client.text_to_speech.convert(
text="Hello, welcome to ElevenLabs!",
voice_id="JBFqnCBsd6RMkjVDRZzb", # George
model_id="eleven_multilingual_v2"
)
withopen("output.mp3", "wb") as f:
for chunk in audio:
f.write(chunk)
Use pre-made voices or create custom voices in the dashboard.
Popular voices:
JBFqnCBsd6RMkjVDRZzb - George (male, narrative)
EXAVITQu4vr4xnSDxMaL - Sarah (female, soft)
onwK4e9ZLuTAKqWW03F9 - Daniel (male, authoritative)
XB0fDUnXU5powFXDhCwa - Charlotte (female, conversational)
voices = client.voices.get_all()
for voice in voices.voices:
print(f"{voice.voice_id}: {voice.name}")
Voice Settings
Fine-tune how the voice sounds:
Stability: How consistent the voice stays. Lower values = more emotional range and variation, but can sound unstable. Higher = steady, predictable delivery.
Similarity boost: How closely to match the original voice sample. Higher values sound more like the original but may amplify audio artifacts.
Style: Exaggerates the voice's unique style characteristics (only works with v2+ models).
Speaker boost: Post-processing that enhances clarity and voice similarity.
from elevenlabs import VoiceSettings
audio = client.text_to_speech.convert(
text="Customize my voice settings.",
voice_id="JBFqnCBsd6RMkjVDRZzb",
voice_settings=VoiceSettings(
stability=0.5,
similarity_boost=0.75,
style=0.5,
speed=1.0, # 0.25 to 4.0 (default 1.0)
use_speaker_boost=True
)
)
Controls how numbers, dates, and abbreviations are converted to spoken words. For example, "01/15/2026" becomes "January fifteenth, twenty twenty-six":
"auto" (default): Model decides based on context
"on": Always normalize (use when you want natural speech)
"off": Speak literally (use when you want "zero one slash one five...")
audio = client.text_to_speech.convert(
text="Call 1-800-555-0123 on 01/15/2026",
voice_id="JBFqnCBsd6RMkjVDRZzb",
apply_text_normalization="on"
)
Request Stitching
When generating long audio in multiple requests, the audio can have pops, unnatural pauses, or tone shifts at the boundaries. Request stitching solves this by letting each request know what comes before/after it:
# First request
audio1 = client.text_to_speech.convert(
text="This is the first part.",
voice_id="JBFqnCBsd6RMkjVDRZzb",
next_text="And this continues the story."
)
# Second request using previous context
audio2 = client.text_to_speech.convert(
text="And this continues the story.",
voice_id="JBFqnCBsd6RMkjVDRZzb",
previous_text="This is the first part."
)
Output Formats
Format
Description
mp3_44100_128
MP3 44.1kHz 128kbps (default) - compressed, good for web/apps
μ-law 8kHz - standard for phone systems (Twilio, telephony)
alaw_8000
A-law 8kHz - telephony (alternative to μ-law)
opus_48000_64
Opus 48kHz 64kbps - efficient streaming codec
wav_44100
WAV 44.1kHz - uncompressed with headers
Word/character timestamps — default for any sync use case
If downstream code needs to know when each word is spoken (subtitles, captions, marker highlights, animation triggers, scene transitions tied to narration), use convert_with_timestamps — never generate audio first and run Whisper on it. ElevenLabs returns character-level alignment alongside the audio in a single call, so timestamps come from the same model that produced the audio (sample-accurate, no transcription drift, no extra dependency).
Python — audio + word-level transcript
import base64, json, os, wave
from dotenv import load_dotenv
from elevenlabs import ElevenLabs, VoiceSettings
load_dotenv()
client = ElevenLabs(api_key=os.environ["ELEVENLABS_API_KEY"])
resp = client.text_to_speech.convert_with_timestamps(
voice_id=os.environ["ELEVENLABS_VOICE_ID"],
text="Claude just got fifteen new connectors. AllTrails. Spotify.",
model_id=os.environ["ELEVENLABS_MODEL_ID"],
output_format="pcm_44100",
voice_settings=VoiceSettings(
stability=float(os.environ["ELEVENLABS_STABILITY"]),
similarity_boost=float(os.environ["ELEVENLABS_SIMILARITY_BOOST"]),
style=float(os.environ["ELEVENLABS_STYLE"]),
speed=float(os.environ["ELEVENLABS_SPEED"]),
use_speaker_boost=True,
),
)
# 1. Audio: base64-decode and wrap raw PCM in a WAV header.
pcm = base64.b64decode(resp.audio_base_64)
with wave.open("narration.wav", "wb") as f:
f.setnchannels(1); f.setsampwidth(2); f.setframerate(44100)
f.writeframes(pcm)
# 2. Word-level transcript: collapse character alignment into whitespace-delimited tokens.
align = resp.normalized_alignment or resp.alignment # normalized strips punctuation oddities
words, current = [], Nonefor ch, t0, t1 inzip(align.characters, align.character_start_times_seconds, align.character_end_times_seconds):
if ch.isspace():
if current: words.append(current); current = Noneelse:
if current isNone: current = {"word": ch, "start": t0, "end": t1}
else: current["word"] += ch; current["end"] = t1
if current: words.append(current)
withopen("transcript.json", "w", encoding="utf-8") as f:
json.dump(words, f, ensure_ascii=False, indent=2)
Response shape
AudioWithTimestampsResponse has:
audio_base_64 — the audio (base64-encoded; decode before writing to disk)
normalized_alignment — same shape, but for the normalized text (numbers expanded, abbreviations spelled out, etc.). Prefer this when grouping into words — it matches what the model actually spoke.
When to skip timestamps
Plain convert (no timestamps) is fine when the audio is the only output and nothing downstream needs sync — e.g. one-off voiceovers, podcasts where word-by-word timing doesn't matter. For anything visual that has to land on a syllable, use convert_with_timestamps.
Streaming
For real-time applications, use the stream method (returns audio chunks as they're generated):
audio_stream = client.text_to_speech.stream(
text="This text will be streamed as audio.",
voice_id="JBFqnCBsd6RMkjVDRZzb",
model_id="eleven_flash_v2_5"# Ultra-low latency
)
for chunk in audio_stream:
play_audio(chunk)