Files
ViGent2/backend/app/services/tts_service.py
2026-01-20 17:14:10 +08:00

34 lines
1.1 KiB
Python

"""
TTS 服务 (EdgeTTS)
"""
import edge_tts
import asyncio
from pathlib import Path
from loguru import logger
class TTSService:
VOICES = {
"zh-CN-YunxiNeural": "云希 (男, 轻松)",
"zh-CN-YunjianNeural": "云健 (男, 体育)",
"zh-CN-YunyangNeural": "云扬 (男, 专业)",
"zh-CN-XiaoxiaoNeural": "晓晓 (女, 活泼)",
"zh-CN-XiaoyiNeural": "晓伊 (女, 卡通)",
}
async def generate_audio(self, text: str, voice: str, output_path: str) -> str:
"""生成语音"""
logger.info(f"TTS Generating: {text[:20]}... ({voice})")
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
try:
communicate = edge_tts.Communicate(text, voice)
await communicate.save(output_path)
# Create SUBTITLES (vtt -> srt conversion logic omitted for brevity in restore)
return output_path
except Exception as e:
logger.error(f"TTS Failed: {e}")
raise
async def list_voices(self):
return [{"id": k, "name": v} for k, v in self.VOICES.items()]