【问题标题】:IBM Watson text to speech audio file not being able to be played after synthesizingIBM Watson 文本到语音音频文件在合成后无法播放
【发布时间】:2021-08-05 05:26:59
【问题描述】:

我正在做的是写入音频输出文件,等到文件存在并且大小不是 0,然后播放它(我尝试了许多不同的库,例如 subprocess、playsound、pygame、vlc 等. 我也尝试了许多不同的文件类型 mp3、wav 等),但由于某种原因,我收到一个错误,说它没有关闭或已损坏。偶尔它会播放一次,但一旦播放另一个 watson 制作的 mp3,它就会再次出错。有谁知道解决办法吗?

...
from ibm_watson import TextToSpeechV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
...
authenticator = IAMAuthenticator(ibmApiKey);
textToSpeech = TextToSpeechV1(authenticator = authenticator);
textToSpeech.set_service_url(ibmServiceUrl);
...
file = str(int(random.random() * 100000)) + ".mp3";
    with open(file, "wb") as audioFile:
        audioFile.write(textToSpeech.synthesize(text, voice = "en-GB_JamesV3Voice", accept = "audio/mp3").get_result().content);

    fileExists = False;

    while (fileExists == False):
        if (os.path.isfile(file)):
            fileExists = os.stat(file).st_size != 0;
            playsound(file);
            os.remove(file);
Error 263 for command:
        open temp/77451.mp3
    The specified device is not open or is not recognized by MCI.

    Error 263 for command:
        close temp/77451.mp3
    The specified device is not open or is not recognized by MCI.
Failed to close the file: temp/77451.mp3
Traceback (most recent call last):
  File "main.py", line 457, in <module>
    runMain(name, config.get("main", "callName"), voice);
  File "main.py", line 156, in runMain
    speak("The time is: " + datetime.now().strptime(datetime.now().time().strftime("%H:%M"), "%H:%M").strftime("%I:%M %p"), voice);
  File "main.py", line 123, in speak
    playsound(file);
  File "C:\Users\turtsis\AppData\Local\Programs\Python\Python35-32\lib\site-packages\playsound.py", line 72, in _playsoundWin
    winCommand(u'open {}'.format(sound))
  File "C:\Users\turtsis\AppData\Local\Programs\Python\Python35-32\lib\site-packages\playsound.py", line 64, in winCommand
    raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
    Error 263 for command:
        open temp/77451.mp3
    The specified device is not open or is not recognized by MCI.

【问题讨论】:

    标签: python text-to-speech ibm-watson watson-text-to-speech


    【解决方案1】:

    错误可能存在于不同的地方。

    首先我会试试这个:

    from ibm_watson import ApiException
    try:
        file = str(int(random.random() * 100000)) + ".mp3";
            with open(file, "wb") as audioFile:
                audioFile.write(textToSpeech.synthesize(text, voice = "en-GB_JamesV3Voice", accept = "audio/mp3").get_result().content);
    except ApiException as ex:
        print ("Method failed with status code " + str(ex.code) + ": " + ex.message)
    

    如果对 Watson 的调用返回错误,它可能会将您从运行时中弹出。

    但是,如果问题出在 playsound 上,我会建议这条路线:

    import pyttsx3
    from ibm_watson import ApiException
    
    engine = pyttsx3.init()
    try:
        file = str(int(random.random() * 100000)) + ".mp3";
            with open(file, "wb") as audioFile:
                audioFile.write(textToSpeech.synthesize(text, voice = "en-GB_JamesV3Voice", accept = "audio/mp3").get_result().content);
    
            fileExists = False;
    
            while (fileExists == False):
                if (os.path.isfile(file)):
                    fileExists = os.stat(file).st_size != 0;
                    engine.say(file);
                    os.remove(file); 
                    engine.runAndWait()          
    
    except ApiException as ex:
        print ("Method failed with status code " + str(ex.code) + ": " + ex.message)
    

    如果这些都不起作用,我会尝试使用 curl 看看你是否可以复制你的场景:

    Replace {apikey} and {url} with your API key and URL.
    
    
    curl -X POST -u "apikey:{apikey}" --header "Content-Type: application/json" --data "{\"text\":\"hello world\"}" --output hello_world.ogg "{url}/v1/synthesize?voice=en-US_AllisonV3Voice"
    

    祝你好运。

    【讨论】:

    • Answer #1 导致问题在很大程度上消失了,但每隔一段时间我就会收到一个新错误:Traceback(最近一次调用最后一次):文件“Brain/main.py”,行10、在中处理.parse(response);文件“C:\Users\turtsis\Desktop\Desktop\Code\Python\JARVIS2\Brain\processing.py”,第 287 行,解析 Speech.textToSpeech(usage + "正在使用的 cpu 百分比");文件“C:\Users\turtsis\Desktop\Desktop\Code\Python\JARVIS2\Brain\speech.py​​”,第 35 行,在 textToSpeech 中,ApiException 除外:NameError: name 'ApiException' is not defined
    • 您多久致电一次 Watson?另外,你在哪里定义~~~ from ibm_watson import ApiException ~~~ 你有没有可能将导入嵌套在本地范围内?您的 IBM Watson 库是最新的吗?
    • 我更新了库并且它工作正常。谢谢!
    猜你喜欢
    • 2019-05-29
    • 2019-06-23
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    相关资源
    最近更新 更多