跳转到主要内容

文本转语音(TTS)

from openai import OpenAI

client = OpenAI(api_key="sk-xxx", base_url="https://crazyrouter.com/v1")

# 生成语音
response = client.audio.speech.create(
    model="tts-1",
    voice="alloy",
    input="你好,欢迎使用 Crazyrouter API 服务。"
)

# 保存为 MP3 文件
response.stream_to_file("output.mp3")
print("语音已保存到 output.mp3")

可用声音

声音特点
alloy中性、平衡
echo男性、沉稳
fable男性、温暖
onyx男性、深沉
nova女性、活泼
shimmer女性、柔和

高质量 TTS

# 使用 tts-1-hd 获得更高音质
response = client.audio.speech.create(
    model="tts-1-hd",
    voice="nova",
    input="高质量语音合成示例",
    response_format="opus",  # 支持 mp3, opus, aac, flac
    speed=1.0  # 0.25 到 4.0
)

response.stream_to_file("output_hd.opus")

语音转文本(STT)

# Whisper 语音识别
audio_file = open("recording.mp3", "rb")

transcript = client.audio.transcriptions.create(
    model="whisper-1",
    file=audio_file,
    language="zh"  # 可选,指定语言提高准确率
)

print(transcript.text)

带时间戳的转录

transcript = client.audio.transcriptions.create(
    model="whisper-1",
    file=open("recording.mp3", "rb"),
    response_format="verbose_json",
    timestamp_granularities=["segment"]
)

for segment in transcript.segments:
    print(f"[{segment['start']:.1f}s - {segment['end']:.1f}s] {segment['text']}")

语音翻译

将非英语语音翻译为英文文本:
audio_file = open("chinese_audio.mp3", "rb")

translation = client.audio.translations.create(
    model="whisper-1",
    file=audio_file
)

print(translation.text)  # 英文输出
TTS 支持的模型包括 tts-1tts-1-hdgpt-4o-mini-tts。STT 使用 whisper-1 模型。