1

我正在使用 pyttsx3 将文本转换为语音。

import pyttsx3

def tts(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.runAndWait()

但问题是我无法将声音保存到文件中(这对于像这样的库来说太奇怪了)。
我尝试了其他一些替代方案,例如espeakpyttsx 的驱动程序,但即使在调整了一些选项后声音也很糟糕。
如果您对如何保存提供良好语音质量(即使使用其他编程语言)的其他离线库的声音或名称有任何建议,那将非常有帮助。
谢谢你。

4

1 回答 1

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)
于 2019-12-10T09:23:42.383 回答