This commit is contained in:
yinsx
2025-12-24 15:15:13 +08:00
commit df085f3f8f
27 changed files with 1926 additions and 0 deletions

View File

@ -0,0 +1,20 @@
import os
import pyttsx3
class TTSService:
def __init__(self, lang='zh-CN'):
self.lang = lang
self.engine = pyttsx3.init()
if lang == 'zh-CN':
voices = self.engine.getProperty('voices')
for voice in voices:
if 'chinese' in voice.name.lower() or 'zh' in voice.id.lower():
self.engine.setProperty('voice', voice.id)
break
def text_to_audio(self, text: str, output_path: str) -> str:
os.makedirs(os.path.dirname(output_path), exist_ok=True)
self.engine.save_to_file(text, output_path)
self.engine.runAndWait()
return output_path