Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python pyttsx3和sapi5将文本文件转换为mp3文件?_Python - Fatal编程技术网

如何使用python pyttsx3和sapi5将文本文件转换为mp3文件?

如何使用python pyttsx3和sapi5将文本文件转换为mp3文件?,python,Python,这是我的python代码 import pyttsx3; engine = pyttsx3.init(driverName='sapi5') infile = "tanjil.txt" f = open(infile, 'r') theText = f.read() f.close() engine.say(theText) engine.runAndWait() 我无法将文件保存到音频文件截至2019年7月14日,我可以使用pyttsx3库保存到文件(无需使用其他库或internet连接)

这是我的python代码

import pyttsx3;
engine = pyttsx3.init(driverName='sapi5')
infile = "tanjil.txt"
f = open(infile, 'r')
theText = f.read()
f.close()
engine.say(theText)
engine.runAndWait()

我无法将文件保存到音频文件

截至2019年7月14日,我可以使用pyttsx3库保存到文件(无需使用其他库或internet连接)

它似乎没有文档记录,但查看github中“Engine.py”()中Engine类的源代码,我找到了一个“save_to_file”函数:

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)
不确定格式-它是一些原始音频格式(我想可能是类似aiff的格式)-但我可以在音频播放器中播放

如果安装pydub:

然后您可以轻松地将其转换为mp3,例如:

from pydub import AudioSegment
AudioSegment.from_file(path_to_save).export('converted.mp3', format="mp3")

我试过@Brian的解决方案,但对我来说不起作用

我四处搜索了一下,不知道如何用pyttx3将语音保存到mp3中,但我找到了另一个没有pyttx3的解决方案

它可以接受一个.txt文件,直接输出一个.wav文件

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)

这是在Windows 10上用Python 3.7.4测试的。

看起来很有希望,但在Windows上对我不起作用。我可能使用了不同的驱动程序/代理?现在windows和MacOS都支持它,所以你只是在互联网上找到它?你能把你找到的链接编辑到你的帖子里吗?哦,我不是说我在网站上找到了这个功能,只是复制粘贴了它。请注意,这只适用于Windows,不适用于Mac,因为COMTypes库是为Windows而不是Linux设计的
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