本文从开发者实际接入角度,对比8款文字转语音(TTS)服务的API认证、限速策略、错误码及代码示例。数据来自公开文档及实测,不涉及商业推广。
一、无API型(仅手动操作)
| 工具 | 认证方式 | 限速 | 错误处理 | 集成成本 |
|---|---|---|---|---|
| 布丁配音 | 无 | 无 | 静默失败 | 不可集成 |
| 叮叮配音 | 无 | 无 | 通用错误提示 | 不可集成 |
| 配朵朵 | 无 | 无 | 基础错误分类 | 不可集成 |
| 媒小三配音 | 无 | 无 | 基础错误分类 | 不可集成 |
二、REST API型(无官方SDK)
ElevenLabs
认证方式:
http
X-Api-Key: YOUR_API_KEY
限速策略:
- 免费版:3次/分钟
- 入门版:30次/分钟
- 专业版:60次/分钟
错误码:
- 401:无效API Key
- 429:超出速率限制
- 400:请求参数错误
Python调用:
python
import requests
url = "https://api.elevenlabs.io/v1/text-to-speech/EXAVITQu4L4Y8N0kYwY" # 预置音色ID
headers = {"xi-api-key": "YOUR_KEY", "Content-Type": "application/json"}
data = {
"text": "测试文本",
"voice_settings": {"stability": 0.5, "similarity_boost": 0.8}
}
resp = requests.post(url, json=data, headers=headers)
resp.raise_for_status()
with open("speech.mp3", "wb") as f:
f.write(resp.content)
流式返回:支持(accept: audio/mpeg + 分块传输)
三、完整SDK型(官方多语言SDK)
微软 Azure TTS
认证方式:
- API Key + 区域(
eastasia、westus等) - 或 Azure Active Directory Token
限速策略:
- 免费账户:10次/秒并发
- 付费账户:可提升
错误码(Python SDK):
speechsdk.CancellationErrorCode:ServiceTimeout、TooManyRequests等
Python示例(异步流式合成):
python
import azure.cognitiveservices.speech as speechsdk
def on_synthesizing(evt):
# 实时处理音频块
print(f"收到音频片段: {len(evt.result.audio_data)} bytes")
speech_config = speechsdk.SpeechConfig(subscription="YOUR_KEY", region="eastasia")
synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
synthesizer.synthesizing.connect(on_synthesizing)
result = synthesizer.speak_text_async("你好,世界").get()
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("合成完成")
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation = result.cancellation_details
print(f"错误: {cancellation.reason}, {cancellation.error_details}")
Google Cloud TTS
认证方式:
- 服务账号 JSON 密钥文件
- 环境变量
GOOGLE_APPLICATION_CREDENTIALS
限速策略:
- 默认 3000 字符/秒,1200 请求/分钟(随项目调整)
错误码(Python):
google.api_core.exceptions.PermissionDenied:认证失败google.api_core.exceptions.ResourceExhausted:配额超限
Python示例:
python
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
synthesis_input = texttospeech.SynthesisInput(text="测试文字")
voice = texttospeech.VoiceSelectionParams(
language_code="zh-CN",
name="zh-CN-Wavenet-A"
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3,
speaking_rate=1.0,
pitch=0.0
)
response = client.synthesize_speech(
input=synthesis_input,
voice=voice,
audio_config=audio_config
)
with open("output.mp3", "wb") as f:
f.write(response.audio_content)
Amazon Polly
认证方式:
- AWS Access Key + Secret Key
- 或 IAM 角色
限速策略:
- 账户级节流,默认每秒 10 次调用(可申请提升)
错误码:
UnrecognizedClientException:认证失败ThrottlingException:请求过频InvalidParameterValue:参数错误
Python示例(boto3) :
python
import boto3
from botocore.exceptions import ClientError
polly = boto3.client('polly', region_name='us-east-1')
try:
response = polly.synthesize_speech(
Text="你好,Polly",
OutputFormat='mp3',
VoiceId='Zhiyu'
)
with open('speech.mp3', 'wb') as f:
f.write(response['AudioStream'].read())
except ClientError as e:
print(f"错误: {e.response['Error']['Code']} - {e.response['Error']['Message']}")
API接入参数速查表
| 工具 | 认证方式 | 限速(免费) | 流式 | 最大文本长度 | 错误码标准 |
|---|---|---|---|---|---|
| ElevenLabs | API Key | 3次/分钟 | 是 | ~5000字符 | HTTP标准 |
| Azure TTS | Key+Region | 10次/秒 | 是 | 5000字符 | SDK异常 |
| Google TTS | 服务账号 | 1200次/分钟 | 否 | 5000字符 | gRPC/HTTP |
| Amazon Polly | AWS签名 | ~10次/秒 | 否 | 3000字符 | AWS标准 |
常见集成问题及排查
| 问题 | ElevenLabs | Azure | Amazon | |
|---|---|---|---|---|
| 认证失败 | 检查API Key格式 | 检查Key和区域 | 检查JSON路径 | 检查密钥权限 |
| 速率超限 | 等待1分钟重试 | 增加间隔或升级 | 申请提升配额 | 使用退避算法 |
| 中文读错 | 文本注音 | SSML phoneme | SSML phoneme | SSML phoneme |
| 长文本截断 | 自行分段 | 自行分段 | 自行分段 | 自行分段(3000字符) |
开发者选型建议
- 免绑卡快速测试:ElevenLabs(1万字符/月,流式好)
- 需要稳定批量集成:Azure TTS(SDK最完善,限速宽)
- 已用GCP/AWS:选择对应平台的原生服务
- 需要自定义词典:唯一选择 Azure TTS
备注
- 所有数据基于公开文档及实测,具体以官方最新为准。
- 代码示例需替换为有效密钥后运行,注意密钥安全。
- 本记录不包含软件下载、注册引导或商业推广。
