12.26
This commit is contained in:
29
services/a2f_api/edge_tts_service.py
Normal file
29
services/a2f_api/edge_tts_service.py
Normal file
@ -0,0 +1,29 @@
|
||||
import os
|
||||
import asyncio
|
||||
import edge_tts
|
||||
|
||||
class EdgeTTSService:
|
||||
def __init__(self, lang='zh-CN'):
|
||||
self.lang = lang
|
||||
# 中文语音选项
|
||||
self.voice_map = {
|
||||
'zh-CN': 'zh-CN-XiaoxiaoNeural', # 晓晓
|
||||
'zh-TW': 'zh-TW-HsiaoChenNeural',
|
||||
'en-US': 'en-US-AriaNeural'
|
||||
}
|
||||
|
||||
def text_to_audio(self, text: str, output_path: str) -> str:
|
||||
"""将文本转换为WAV音频文件(使用edge-tts)"""
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
|
||||
voice = self.voice_map.get(self.lang, 'zh-CN-XiaoxiaoNeural')
|
||||
|
||||
# edge-tts 是异步的,需要在同步函数中运行
|
||||
asyncio.run(self._async_text_to_audio(text, output_path, voice))
|
||||
|
||||
return output_path
|
||||
|
||||
async def _async_text_to_audio(self, text: str, output_path: str, voice: str):
|
||||
"""异步生成音频"""
|
||||
communicate = edge_tts.Communicate(text, voice)
|
||||
await communicate.save(output_path)
|
||||
Reference in New Issue
Block a user