-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvoice_test.py
More file actions
41 lines (35 loc) · 1.45 KB
/
voice_test.py
File metadata and controls
41 lines (35 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import asyncio
import edge_tts
from playsound import playsound
import os
async def play_chinese_tts(text, voice="zh-CN-XiaoyiNeural", output_filename="output.mp3"):
"""
使用 edge-tts 生成中文语音并播放
:param text: 要转换为语音的中文文本
:param voice: 使用的语音模型 (例如: zh-CN-XiaoyiNeural, zh-CN-XiaohanNeural 等)
:param output_filename: 生成的音频文件名
"""
communicate = edge_tts.Communicate(text, voice)
await communicate.save(output_filename)
print(f"生成音频文件: {output_filename}")
try:
playsound(output_filename)
print("播放完成")
except Exception as e:
print(f"播放音频时发生错误: {e}")
finally:
# 可以选择删除生成的音频文件
# os.remove(output_filename)
pass
if __name__ == "__main__":
chinese_text = "你好,这是一个中文语音合成的测试。"
# 你可以在这里尝试不同的中文语音模型
voice_model = "zh-CN-XiaoyiNeural" # 女声
# voice_model = "zh-CN-XiaohanNeural" # 男声
# voice_model = "zh-CN-YunxiNeural" # 男声
# voice_model = "zh-CN-YunzeNeural" # 男声
# 创建保存音频的目录 (如果不存在)
output_dir = "tts_output"
os.makedirs(output_dir, exist_ok=True)
output_file = os.path.join(output_dir, "test_chinese.mp3")
asyncio.run(play_chinese_tts(chinese_text, voice=voice_model, output_filename=output_file))