elevenlabs-tts
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseElevenLabs Text-to-Speech
ElevenLabs 文本转语音
Generate high-quality audio from text using the ElevenLabs API.
借助ElevenLabs API从文本生成高质量音频。
Prerequisites
前提条件
-
ElevenLabs API Key: Must be set as environment variable
ELEVEN_API_KEY- The API key should already be configured in your environment
- If not set, get your API key from: https://elevenlabs.io/app/settings/api-keys
-
Python requests library: Install if needed with
pip install requests --break-system-packages
-
ElevenLabs API密钥:必须设置为环境变量
ELEVEN_API_KEY- API密钥应已在您的环境中配置
- 若未设置,请从以下地址获取API密钥:https://elevenlabs.io/app/settings/api-keys
-
Python requests库:若未安装,可通过安装
pip install requests --break-system-packages
Quick Start
快速开始
Use the provided script to generate audio with your default voice:
bash
python scripts/generate_audio.py "Your text here" output.mp3The script is pre-configured to use your preferred voice (ID: ).
dq5fzy66iCKSIQWm5YMU使用提供的脚本,通过您的默认语音生成音频:
bash
python scripts/generate_audio.py "Your text here" output.mp3脚本已预先配置为使用您偏好的语音(ID: )。
dq5fzy66iCKSIQWm5YMUUsing a Different Voice
使用其他语音
If you need to use a different voice, specify it with the parameter:
--voicebash
python scripts/generate_audio.py "Hello world" output.mp3 --voice "voice_id_here"如果需要使用其他语音,可通过参数指定:
--voicebash
python scripts/generate_audio.py "Hello world" output.mp3 --voice "voice_id_here"Common Workflows
常见工作流程
Voiceover for Videos
视频配音
- Prepare your script text
- Choose appropriate voice for your content
- Generate the audio file
- Use the audio in your video editing software
Example:
bash
undefined- 准备脚本文本
- 为内容选择合适的语音
- 生成音频文件
- 在视频编辑软件中使用该音频
示例:
bash
undefinedRead script from file and generate audio
从文件读取脚本并生成音频
python scripts/generate_audio.py "$(cat video_script.txt)" voiceover.mp3
undefinedpython scripts/generate_audio.py "$(cat video_script.txt)" voiceover.mp3
undefinedMultiple Segments
多段内容
For longer content or different scenes, generate separate audio files:
bash
undefined对于较长内容或不同场景,生成单独的音频文件:
bash
undefinedIntro
开场
python scripts/generate_audio.py "Welcome to our channel" intro.mp3
python scripts/generate_audio.py "Welcome to our channel" intro.mp3
Main content
主要内容
python scripts/generate_audio.py "$(cat main_script.txt)" main.mp3
python scripts/generate_audio.py "$(cat main_script.txt)" main.mp3
Outro
结尾
python scripts/generate_audio.py "Thanks for watching" outro.mp3
undefinedpython scripts/generate_audio.py "Thanks for watching" outro.mp3
undefinedFrom Markdown or Text Files
从Markdown或文本文件转换
Convert documents to audio:
bash
undefined将文档转换为音频:
bash
undefinedExtract text from markdown (remove formatting)
提取Markdown中的文本(移除格式)
python scripts/generate_audio.py "$(cat article.md | sed 's/#//g')" article.mp3
undefinedpython scripts/generate_audio.py "$(cat article.md | sed 's/#//g')" article.mp3
undefinedAdvanced Usage
高级用法
Custom Voice ID
自定义语音ID
If you have access to custom voices or want to use a specific voice ID:
bash
python scripts/generate_audio.py "Text" output.mp3 --voice "your_voice_id_here"如果您有权限使用自定义语音或需要指定特定语音ID:
bash
python scripts/generate_audio.py "Text" output.mp3 --voice "your_voice_id_here"Providing API Key Directly
直接提供API密钥
Instead of setting environment variable:
bash
python scripts/generate_audio.py "Text" output.mp3 --api-key "your_api_key"替代设置环境变量的方式:
bash
python scripts/generate_audio.py "Text" output.mp3 --api-key "your_api_key"Script Parameters
脚本参数
The script accepts:
generate_audio.py- text (required): The text to convert to speech
- output (required): Output file path (e.g., )
output.mp3 - --voice (optional): Custom voice ID (default: dq5fzy66iCKSIQWm5YMU)
- --api-key (optional): API key (overrides ELEVEN_API_KEY env var)
generate_audio.py- text(必填):要转换为语音的文本
- output(必填):输出文件路径(例如:)
output.mp3 - --voice(可选):自定义语音ID(默认值:dq5fzy66iCKSIQWm5YMU)
- --api-key(可选):API密钥(覆盖环境变量ELEVEN_API_KEY)
Best Practices
最佳实践
- Text Length: Keep individual requests under 2,500 characters for best results
- Character Limits: Monitor your monthly character quota to avoid interruptions
- File Naming: Use descriptive names (e.g., ,
intro_v2.mp3)scene1_narration.mp3 - Testing: Generate a short sample first to verify quality before processing long texts
- Punctuation: Use proper punctuation to improve natural speech rhythm and pauses
- 文本长度:单个请求的文本长度最好控制在2500字符以内,以获得最佳效果
- 字符限额:监控您的月度字符配额,避免服务中断
- 文件命名:使用描述性的文件名(例如:、
intro_v2.mp3)scene1_narration.mp3 - 测试:在处理长文本前,先生成短样本验证音频质量
- 标点符号:使用正确的标点符号,提升语音的自然节奏和停顿效果
Troubleshooting
故障排除
API Key Error: Ensure ELEVEN_API_KEY is set correctly:
bash
echo $ELEVEN_API_KEY # Should display your keyModule Not Found: Install requests:
bash
pip install requests --break-system-packagesRate Limiting: Free tier has usage limits. Space out requests or upgrade plan.
Poor Audio Quality: Try different voices or adjust the text (add punctuation, break into shorter sentences).
API密钥错误:确保ELEVEN_API_KEY已正确设置:
bash
echo $ELEVEN_API_KEY # 应显示您的密钥模块未找到:安装requests库:
bash
pip install requests --break-system-packages速率限制:免费版有使用限制,可间隔发送请求或升级套餐。
音频质量不佳:尝试使用不同的语音,或调整文本(添加标点、拆分为短句)。
Resources
资源
- ElevenLabs Dashboard: https://elevenlabs.io/app
- API Documentation: https://elevenlabs.io/docs/api-reference
- Voice Library: https://elevenlabs.io/voice-library
- Pricing: https://elevenlabs.io/pricing