TTS 文字转语音
- 把文本合成为语音
- 6 种音色可选,4 档语速可调
- 单次输入上限 4096 字符
认证
All endpoints authenticate with a Bearer token.
Get an API key: open the console and create one.
Add it to the request header:
Authorization: Bearer YOUR_OAIRoute_KEY请求参数
请求体为 JSON。成功时响应体是二进制音频流,不是 JSON。
语音合成模型。当前取值 gpt-4o-mini-tts。
待朗读的文本。长度上限 4096 字符,超过返回 413。
朗读音色。
alloy中性、均衡echo男声,低沉稳重fable英式口音,偏叙述风格onyx男声,浑厚nova女声,明快有活力shimmer女声,柔和
输出音频封装格式,默认 wav。
wav未压缩opus面向网络流媒体aacAAC 编码flac无损压缩pcm裸音频数据
朗读速率倍数,取值 0.25 至 4.0,默认 1.0。
The body is JSON. On success the response is a binary audio stream, not JSON.
The speech synthesis model. Currently gpt-4o-mini-tts.
The text to read. Up to 4096 characters; exceeding that returns a 413.
The reading voice.
alloyneutral, balancedechomale, low and steadyfableBritish accent, narrativeonyxmale, deep and fullnovafemale, bright and livelyshimmerfemale, soft
The output audio container; defaults to wav.
wavuncompressedopusfor web streamingaacAAC encodingflaclossless compressionpcmraw audio data
Playback speed multiplier, 0.25 to 4.0, default 1.0.
响应
成功时返回二进制音频流:
| 响应头 | 值 |
|---|---|
| Content-Type | 随 response_format 变化,如 audio/wav |
| Content-Length | 音频字节数 |
失败时返回 JSON 错误体,结构为 { error: { code, message, type } }。
On success it returns a binary audio stream:
| Response header | Value |
|---|---|
| Content-Type | Follows response_format, e.g. audio/wav |
| Content-Length | Audio size in bytes |
On failure it returns a JSON error body structured as { error: { code, message, type } }.
音色怎么选
| 场景 | 建议音色 |
|---|---|
| 客服、助手 | alloy 或 nova |
| 有声书、旁白 | fable 或 onyx |
| 儿童内容 | nova 或 shimmer |
| 广告、宣传 | onyx 或 echo |
先用一段代表性文本把六个音色都合成一遍对比,比看文字描述准得多。
| Scenario | Suggested voice |
|---|---|
| Customer service, assistant | alloy or nova |
| Audiobooks, narration | fable or onyx |
| Children's content | nova or shimmer |
| Ads, promos | onyx or echo |
Synthesize one representative piece of text with all six voices and compare — far more reliable than reading descriptions.
长文本处理
单次 4096 字符的上限决定了长文必须分段。按句子边界切分比按字数切分自然,能避免句子中间断开。
import re
def split_text(text, limit=3800):
# 先按句号、问号、叹号切,再按长度贪心合并
sentences = re.split(r"(?<=[。!?.!?])", text)
chunks, cur = [], ""
for s in sentences:
if len(cur) + len(s) > limit:
chunks.append(cur)
cur = s
else:
cur += s
if cur:
chunks.append(cur)
return chunks为什么用 3800 而不是 4096:中文字符在部分实现里按字节计,留 10% 余量更稳妥。
The 4096-character ceiling means long text must be split. Splitting on sentence boundaries is more natural than splitting by character count and avoids chopping mid-sentence.
import re
def split_text(text, limit=3800):
# split on sentence enders first, then greedily merge by length
sentences = re.split(r"(?<=[。!?.!?])", text)
chunks, cur = [], ""
for s in sentences:
if len(cur) + len(s) > limit:
chunks.append(cur)
cur = s
else:
cur += s
if cur:
chunks.append(cur)
return chunksWhy 3800 rather than 4096: some implementations count Chinese characters by bytes, so a 10% margin is safer.
格式怎么选
| 格式 | 适合 |
|---|---|
| wav | 需要后期编辑,无损 |
| opus | 网络传输,体积最小 |
| flac | 归档,无损且比 wav 小 |
| pcm | 送进实时音频管线 |
| aac | 移动端播放兼容性好 |
| Format | Best for |
|---|---|
| wav | Post-editing, lossless |
| opus | Network transfer, smallest size |
| flac | Archival, lossless and smaller than wav |
| pcm | Feeding a real-time audio pipeline |
| aac | Good mobile playback compatibility |
错误
| HTTP | type | 说明 |
|---|---|---|
| 400 | invalid_request_error | 请求参数无效 |
| 401 | authentication_error | 身份验证失败,需检查 API 密钥 |
| 402 | payment_required | 账户余额不足,需先充值 |
| 429 | rate_limit_error | 调用频率过高,稍后重试 |
合成按输入字符计费。同样的文本合成多次会重复计费,批量场景建议把结果按内容哈希缓存起来。
| HTTP | type | Notes |
|---|---|---|
| 400 | invalid_request_error | Invalid request parameters |
| 401 | authentication_error | Authentication failed; check the API key |
| 402 | payment_required | Insufficient account balance; top up first |
| 429 | rate_limit_error | Too many calls; retry later |
Synthesis is billed by input characters. Synthesizing the same text again bills again, so for batch work cache results keyed by a hash of the content.
For more status codes and retry guidance see Error codes and rate limits; for troubleshooting steps on common errors see the FAQ.
支持的模型列表
以下语音合成模型均可通过本接口调用,实际可用范围以你的账号分组为准:
OpenAI 语音 系列
tts-1-hd语音合成,音质优先tts-1语音合成,延迟优先gpt-4o-mini-tts新一代,支持风格指令whisper-1语音转文字 等
Google 语音 系列
gemini-3.1-flash-tts-preview轻量语音合成,多语言 等
更多模型持续更新中…
Every speech synthesis model below can be called through this endpoint; what your account group actually allows takes precedence:
OpenAI 语音 系列
tts-1-hd语音合成,音质优先tts-1语音合成,延迟优先gpt-4o-mini-tts新一代,支持风格指令whisper-1语音转文字 等
Google 语音 系列
gemini-3.1-flash-tts-preview轻量语音合成,多语言 等
更多模型持续更新中…
使用示例
基础合成
传文本、音色与输出格式,直接拿回音频二进制。默认 24kHz 采样率。
音质优先
tts-1-hd 音质更好但延迟更高,适合预生成的有声内容,不适合实时对话。
调整语速
speed 取 0.25 到 4.0,1.0 是原速。教学类内容常降到 0.9 左右,方便跟读。
流式合成
需要边生成边播放时用流式格式,客户端收到首块就能开始播,不必等整段合成完。
Basic synthesis
Pass text, voice, and output format, and get back audio bytes directly. The default sample rate is 24kHz.
Quality first
tts-1-hd sounds better but has higher latency; suited to pre-generated spoken content, not real-time conversation.
Adjusting speed
speed runs from 0.25 to 4.0, with 1.0 at normal speed. Teaching material is often slowed to around 0.9 to make it easy to follow.
Streaming synthesis
When you need to play while generating, use a streaming format: the client can start playing on the first chunk instead of waiting for the whole clip.