def save_to_file(self, text, filename, name=None):
'''
Adds an utterance to speak to the event queue.
@param text: Text to sepak
@type text: unicode
@param filename: the name of file to save.
@param name: Name to associate with this utterance. Included in
notifications about this utterance.
@type name: str
'''
self.proxy.save_to_file(text, filename, name)
字符串 我可以这样使用它:
engine.save_to_file('the text I want to save as audio', path_to_save)
import pyttsx3
engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")[0]
engine.setProperty('voice', voices)
text = 'Your Text'
engine.save_to_file(text, 'name.mp3')
engine.runAndWait() # don't forget to use this line
def txt_zu_wav(eingabe, ausgabe, text_aus_datei = True, geschwindigkeit = 2, Stimmenname = "Zira"):
from comtypes.client import CreateObject
engine = CreateObject("SAPI.SpVoice")
engine.rate = geschwindigkeit # von -10 bis 10
for stimme in engine.GetVoices():
if stimme.GetDescription().find(Stimmenname) >= 0:
engine.Voice = stimme
break
else:
print("Fehler Stimme nicht gefunden -> Standard wird benutzt")
if text_aus_datei:
datei = open(eingabe, 'r')
text = datei.read()
datei.close()
else:
text = eingabe
stream = CreateObject("SAPI.SpFileStream")
from comtypes.gen import SpeechLib
stream.Open(ausgabe, SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
engine.speak(text)
stream.Close()
txt_zu_wav("test.txt", "test_1.wav")
txt_zu_wav("It also works with a string instead of a file path", "test_2.wav", False)
4条答案
按热度按时间pcww981p1#
截至2019年7月14日,我可以使用pyttsx 3库保存文件(无需使用其他库或互联网连接)。
它似乎没有被记录下来,但是在github中查看“engine.py“(https://github.com/nateshmbhat/pyttsx3/blob/master/pyttsx3/engine.py)中Engine类的源代码,我能够找到一个“保存_to_file”函数:
字符串
我可以这样使用它:
型
不知道格式-这是一些原始音频格式(我猜它可能是像aiff)-但我可以在音频播放器播放它。
如果你安装pydub:https://pypi.org/project/pydub/
然后你可以很容易地将其转换为mp3,例如:
型
rggaifut2#
字符串
hi3rlvi23#
我试过@Brian的解决方案,但它对我不起作用。
我搜索了一下,我不知道如何在pyttx 3中保存语音到mp3,但我发现了另一个没有pyttx 3的解决方案。
它可以采取一个.txt文件,并直接输出一个.wav文件,
字符串
这是在Windows 10上用Python 3.7.4测试的。
wvmv3b1j4#
尝试以下代码片段将文本转换为音频并将其保存为mp3文件。
字符串
注:
pyttsx3
save_to_file()
方法创建一个原始音频文件,即使我们能够在媒体播放器中播放它,它也不会对其他应用程序有用。pydub
是一个有用的软件包,可以将原始音频转换为特定格式。