使用 Coze API 进行文本转语音 (TTS)。将文本转换为自然的语音音频文件。支持多种音色和输出格式 (mp3、ogg_opus、wav、pcm)。
Convert text to natural-sounding speech using Coze API.
1. Get your API Key: Get a key from Coze Platform
2. Set it in your environment:
bash
export COZE_API_KEY="your-key-here"
Convert text to speech with default settings:
bash
bash scripts/text_to_speech.sh "你好,这是测试语音"
bash
bash scripts/text_to_speech.sh "你好世界" -o output.mp3
bash
bash scripts/text_to_speech.sh "你好" -v 2
bash
bash scripts/text_to_speech.sh "你好" -f ogg_opus
bash
bash scripts/text_to_speech.sh "要转换的文本" -o output.mp3 -v 1 -f mp3
Parameters:
text (required): Text to convert to speech-o, --output (optional): Output file path (default: auto-generated)-v, --voice (optional): Voice ID (default: 1)-f, --format (optional): Output format - mp3/ogg_opus/wav/pcm (default: mp3)The script saves the audio file and outputs:
Example output:
text
✓ Audio saved: coze_tts_20260324_235030_a1b2c3d4.mp3
Size: 25.3 KB
Duration: ~3 seconds
bash
bash scripts/text_to_speech.sh "您有一条新消息" -o notification.mp3
bash
bash scripts/text_to_speech.sh "欢迎使用 Coze 语音服务" -v 2 -o greeting.mp3
bash
bash scripts/text_to_speech.sh "你好" -f ogg_opus -o message.ogg
bash
for text in "你好" "谢谢" "再见"; do
bash scripts/text_to_speech.sh "$text" -o "${text}.mp3"
done
Combine with coze-asr for voice conversation:
bash
# 1. User speaks -> ASR converts to text
bash coze-asr/scripts/speech_to_text.sh input.ogg
# 2. Process text with AI...
# 3. AI response -> TTS converts to speech
bash coze-tts/scripts/text_to_speech.sh "AI的回复" -o response.mp3
Authentication Error:
Invalid Voice ID:
File Not Created:
POST https://api.coze.cn/v1/audio/speechVariableDescriptionRequiredCOZE_API_KEYCoze API authentication keyYes
ToolPurposeRequiredjqJSON processingYesffprobeAudio duration detectionOptional
MIT