2016-08-18 160 views
0

我在試圖找到解決方案,爲什麼我的代碼無法正常工作。我使用的解決方案來自Recording synthesized text-to-speech to a file in Python,它有點沒有爲我工作。問題是爲什麼2個方法/函數text_to_wav和all_texts_to_files不適用於我。如何將PyTTSx的輸出保存到wav文件

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 
+0

是在路上的espeak? – dabhand

+0

是的,它在路上。 – degath

+0

'item ['text']'包含完整路徑的文件名,如果是這樣的話,嘗試在'text_to_wav'方法內打印到'file_name'並檢查它 – Mourya

回答

0

假設你是在一個雙贏的操作系統使用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]) 
+0

sys.path.insert(0,espeak_module) – pyaddict

+0

即使你可能在開始處包含了一個用於python的sys.path, – pyaddict

+0

調用子進程時可能會忽略它,這在linux操作系統上看起來不是問題。 – pyaddict

-1
from gtts import gTTS 
import os 
tts = gTTS(text='hi how r u', lang='en') 
tts.save("good.wav") 
os.system("mpg321 good.wav") 

此代碼將輸出將被保存在其中,u安裝UR蟒蛇文件夾。各種音頻格式的 只需更改擴展名文件。

+0

由於您指定了後綴,因此它似乎不會保存'.wav'文件。對於相同的文本,無論後綴如何,生成的音頻文件都將具有逐位相同的文件。 –

相關問題