【问题标题】:how can i convert a text file to mp3 file using python pyttsx3 and sapi5?如何使用 python pyttsx3 和 sapi5 将文本文件转换为 mp3 文件?
【发布时间】:2019-05-24 21:29:57
【问题描述】:

这是我的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()

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

【问题讨论】:

    标签: python


    【解决方案1】:
    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
    

    【讨论】:

      【解决方案2】:

      我已经尝试过@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 而不是 Mac,因为库 COMTypes 是为 Windows 设计的,而不是 Linux stackoverflow.com/questions/37161560/…
      【解决方案3】:

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

      似乎没有记录,但查看 github 中“engine.py”(https://github.com/nateshmbhat/pyttsx3/blob/master/pyttsx3/engine.py)中引擎类的源代码,我能够找到“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: https://pypi.org/project/pydub/

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

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-09
      • 2016-03-23
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 2015-03-17
      • 1970-01-01
      • 2018-02-07
      相关资源
      最近更新 更多