如何将 PyTTSx 的输出保存到 wav 文件 [英] How to save the output of PyTTSx to wav file

查看:89
本文介绍了如何将 PyTTSx 的输出保存到 wav 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找解决方案,为什么我的代码无法正常工作.我使用了 录制合成的文本到语音的解决方案Python 中的一个文件,但它对我来说有点没用.问题是为什么 2 个方法/函数 text_to_wav 和 all_texts_to_files 对我不起作用.

I'm trying to find a solution why my code doesn't work properly. I used solution from Recording synthesized text-to-speech to a file in Python, and it kinda didnt worked out for me. The question is why 2 methods/functions text_to_wav and all_texts_to_files doesnt work for me.

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

推荐答案

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

Assuming you are using Python on a win os, you will need to specify the full path to the subprocess, and of course the full output file path eg;

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

这篇关于如何将 PyTTSx 的输出保存到 wav 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆