elevenlabs-tts

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

ElevenLabs Text-to-Speech

ElevenLabs 文本转语音

Generate high-quality audio from text using the ElevenLabs API.
借助ElevenLabs API从文本生成高质量音频。

Prerequisites

前提条件

  1. ElevenLabs API Key: Must be set as environment variable
    ELEVEN_API_KEY
  2. Python requests library: Install if needed with
    pip install requests --break-system-packages
  1. ElevenLabs API密钥:必须设置为环境变量
    ELEVEN_API_KEY
  2. 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.mp3
The script is pre-configured to use your preferred voice (ID:
dq5fzy66iCKSIQWm5YMU
).
使用提供的脚本,通过您的默认语音生成音频:
bash
python scripts/generate_audio.py "Your text here" output.mp3
脚本已预先配置为使用您偏好的语音(ID:
dq5fzy66iCKSIQWm5YMU
)。

Using a Different Voice

使用其他语音

If you need to use a different voice, specify it with the
--voice
parameter:
bash
python scripts/generate_audio.py "Hello world" output.mp3 --voice "voice_id_here"
如果需要使用其他语音,可通过
--voice
参数指定:
bash
python scripts/generate_audio.py "Hello world" output.mp3 --voice "voice_id_here"

Common Workflows

常见工作流程

Voiceover for Videos

视频配音

  1. Prepare your script text
  2. Choose appropriate voice for your content
  3. Generate the audio file
  4. Use the audio in your video editing software
Example:
bash
undefined
  1. 准备脚本文本
  2. 为内容选择合适的语音
  3. 生成音频文件
  4. 在视频编辑软件中使用该音频
示例:
bash
undefined

Read script from file and generate audio

从文件读取脚本并生成音频

python scripts/generate_audio.py "$(cat video_script.txt)" voiceover.mp3
undefined
python scripts/generate_audio.py "$(cat video_script.txt)" voiceover.mp3
undefined

Multiple Segments

多段内容

For longer content or different scenes, generate separate audio files:
bash
undefined
对于较长内容或不同场景,生成单独的音频文件:
bash
undefined

Intro

开场

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
undefined
python scripts/generate_audio.py "Thanks for watching" outro.mp3
undefined

From Markdown or Text Files

从Markdown或文本文件转换

Convert documents to audio:
bash
undefined
将文档转换为音频:
bash
undefined

Extract text from markdown (remove formatting)

提取Markdown中的文本(移除格式)

python scripts/generate_audio.py "$(cat article.md | sed 's/#//g')" article.mp3
undefined
python scripts/generate_audio.py "$(cat article.md | sed 's/#//g')" article.mp3
undefined

Advanced 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
generate_audio.py
script accepts:
  • 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

最佳实践

  1. Text Length: Keep individual requests under 2,500 characters for best results
  2. Character Limits: Monitor your monthly character quota to avoid interruptions
  3. File Naming: Use descriptive names (e.g.,
    intro_v2.mp3
    ,
    scene1_narration.mp3
    )
  4. Testing: Generate a short sample first to verify quality before processing long texts
  5. Punctuation: Use proper punctuation to improve natural speech rhythm and pauses
  1. 文本长度:单个请求的文本长度最好控制在2500字符以内,以获得最佳效果
  2. 字符限额:监控您的月度字符配额,避免服务中断
  3. 文件命名:使用描述性的文件名(例如:
    intro_v2.mp3
    scene1_narration.mp3
  4. 测试:在处理长文本前,先生成短样本验证音频质量
  5. 标点符号:使用正确的标点符号,提升语音的自然节奏和停顿效果

Troubleshooting

故障排除

API Key Error: Ensure ELEVEN_API_KEY is set correctly:
bash
echo $ELEVEN_API_KEY  # Should display your key
Module Not Found: Install requests:
bash
pip install requests --break-system-packages
Rate 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

资源