【问题标题】:How to save the output of PyTTSx to wav file如何将 PyTTSx 的输出保存到 wav 文件
【发布时间】:2016-08-18 09:22:50
【问题描述】:

我正在尝试找出我的代码无法正常工作的解决方案。我使用了来自Recording synthesized text-to-speech to a file in Python 的解决方案,但对我来说有点效果不佳。问题是为什么 2 个方法/函数 text_to_wav 和 all_texts_to_files 对我不起作用。

import json
import pyttsx
from openpyxl import load_workbook
import subprocess

class Ver2ProjectWithTTS(object):

    def __init__(self):
        self.list_merge = []

    def do_the_job(self):
        self.read_json_file()
        self.read_xml_file()
        #self.say_something()
        self.all_texts_to_files()

    def read_json_file(self):
        with open("json-example.json", 'r') as df:
            json_data = json.load(df)
            df.close()
        for k in json_data['sentences']:
            text_json = k['text']
            speed_json = int(k['speed'])
            volume_json = float(k['volume'])
            dict_json = {'text': text_json, 'speed': speed_json, 'volume': volume_json}
            self.list_merge.append(dict_json)

    def read_xml_file(self):
        tree = et.parse('xml-example.xml')
        root = tree.getroot()
        for k in range(0, len(root)):
            text_xml = root[k][0].text
            speed_xml = int(root[k][1].text)
            volume_xml = float(root[k][2].text)
            dict_xml = {'text': text_xml, 'speed': speed_xml, 'volume': volume_xml}
            self.list_merge.append(dict_xml)

    def say_something(self):
        for item in self.list_merge:
            engine = pyttsx.init()
            engine.getProperty('rate')
            engine.getProperty('volume')
            engine.setProperty('rate', item['speed'])
            engine.setProperty('volume', item['volume'])
            engine.say(cleared_text)
            engine.runAndWait()

    def text_to_wav(self, text, file_name):
        subprocess.call(["espeak", "-w"+file_name+".wav", text])

    def all_texts_to_files(self):
        for item in self.list_merge:
            cleared_text = self.clear_text_from_underscores(item['text'])
            self.text_to_wav(cleared_text, item['text'])

if __name__ == '__main__':
    a = Ver2ProjectWithTTS()
    a.do_the_job()

这里的错误代码:

#In my project:
line 91, in <module> a.do_the_job()
line 21, in do_the_job self.all_texts_to_files()
line 85, in all_texts_to_files self.text_to_wav(cleared_text, item['text'])
line 80, in text_to_wav subprocess.call(["espeak", "-w"+file_name+".wav", text])
#in subprocess:
line 523, in call return Popen(*popenargs, **kwargs).wait()
line 711, in __init__ errread, errwrite)
line 959, in _execute_child startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

【问题讨论】:

  • 是在路上吗?
  • 是的,它在路上。
  • item['text'] 是否包含带有完整路径的文件名,如果是,请尝试在text_to_wav 方法中打印到file_name 并检查它

标签: python python-2.7 text-to-speech pyttsx


【解决方案1】:

以防万一有人想知道如何在 linux 而不是 windows 上执行此操作:

os.system("espeak \"example text to speech\" --stdout > myfile.wav")

【讨论】:

    【解决方案2】:

    假设您在 win 操作系统上使用 Python, 您需要指定子流程的完整路径, 当然还有完整的输出文件路径 例如;

    espeak_path = "C:/Program Files/eSpeak/command_line/espeak.exe"
    file_name = "C:/temp/test"
    subprocess.call([espeak_path,"-w"+file_name+".wav", text])
    

    【讨论】:

    • sys.path.insert(0, espeak_module)
    • 即使您可能在开始时包含了 python 的 sys.path,
    • 调用子进程时可能会被忽略,这在linux操作系统上似乎不是问题。
    【解决方案3】:
    from gtts import gTTS
    import os
    tts = gTTS(text='hi how r u', lang='en')
    tts.save("good.wav")
    os.system("mpg321 good.wav")
    

    此代码将输出将保存在您安装的 python 文件夹中。 对于各种音频格式,只需更改扩展文件即可。

    【讨论】:

    • 它似乎没有保存.wav 文件,因为您指定了后缀。对于相同的文本,无论后缀如何,生成的音频文件都将具有逐位相同的文件。
    • tts 不保存在“.wav”扩展名中。您只能保存在 mp3 中
    猜你喜欢
    • 2012-03-31
    • 2013-01-18
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多