我正在使用pyttsx3将文本转换为语音。
import pyttsx3
def tts(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
但问题是我无法将声音保存到文件中(这对于像这样的库来说太奇怪了。)
我尝试了其他一些替代方案,比如espeak
这是pyttsx的驱动程序,但即使调整了一些选项后声音也很糟糕。
如果您有任何建议,我如何保存其他离线库的声音或名称,提供良好的语音质量(即使使用其他编程语言),这将非常有用。
谢谢。
答案 0 :(得分:0)
这可能是较晚的答案,但我希望它会有用:)
# pip install comtypes
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
voiceList = []
for voice in voices:
voiceList.append(voice.name)
print("Voice List: " ,voiceList)
def playItNow(textf, filename, useFile = True, rate = 2, voice = voiceList[0]):
from comtypes.client import CreateObject
engine = CreateObject("SAPI.SpVoice")
engine.rate = rate # can be -10 to 10
for v in engine.GetVoices():
if v.GetDescription().find(voice) >= 0:
engine.Voice = v
break
else:
print("Voice not found")
if useFile:
datei = open(textf, 'r',encoding="utf8")
text = datei.read()
datei.close()
else:
text = textf
stream = CreateObject("SAPI.SpFileStream")
from comtypes.gen import SpeechLib
stream.Open(filename, SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
engine.speak(text)
stream.Close()
import winsound
winsound.PlaySound(filename, winsound.SND_FILENAME)
playItNow("TextFile.txt", "test_2.wav", useFile= False, rate = -1)