python 如何将文本文件转换为MP3文件?

dba5bblo  于 2023-11-15  发布在  Python
关注(0)|答案(4)|浏览(144)

下面是我的Python代码:

import pyttsx3

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

字符串
我无法将文件保存为音频文件。

pcww981p

pcww981p1#

截至2019年7月14日,我可以使用pyttsx 3库保存文件(无需使用其他库或互联网连接)。
它似乎没有被记录下来,但是在github中查看“engine.py“(https://github.com/nateshmbhat/pyttsx3/blob/master/pyttsx3/engine.py)中Engine类的源代码,我能够找到一个“保存_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:https://pypi.org/project/pydub/
然后你可以很容易地将其转换为mp3,例如:

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

rggaifut

rggaifut2#

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

字符串

hi3rlvi2

hi3rlvi23#

我试过@Brian的解决方案,但它对我不起作用。
我搜索了一下,我不知道如何在pyttx 3中保存语音到mp3,但我发现了另一个没有pyttx 3的解决方案。
它可以采取一个.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测试的。

wvmv3b1j

wvmv3b1j4#

尝试以下代码片段将文本转换为音频并将其保存为mp3文件。

import pyttsx3
from pydub import AudioSegment

# read text content from a file
f = open("tanjil.txt", 'r')
theText = f.read()
f.close()

# create audio file
engine = pyttsx3.init('sapi5')
engine.save_to_file(theText, 'test.mp3') # raw audio file
engine.runAndWait()

AudioSegment.from_file('test.mp3').export('test.mp3', format="mp3") # audio file in mp3 format

字符串
注:pyttsx3save_to_file()方法创建一个原始音频文件,即使我们能够在媒体播放器中播放它,它也不会对其他应用程序有用。pydub是一个有用的软件包,可以将原始音频转换为特定格式。

相关问题