1

我对python不太熟悉,如果这是一个太琐碎的问题,请道歉

我有一个从 Url 获取音频文件的脚本,我需要将文件从 .ogg 类型转换为 .wav

然后我想将转换和加载的文件传递给一个以文件路径字符串为参数的函数。

下面是我的代码:

import os
import pydub
import glob
import time

from io import BytesIO

import pandas as pd

from urllib.request import Request, urlopen
import urllib.error

import azure.cognitiveservices.speech as speechsdk
import time

#%%

audio_file = "https = url.whatever.com.co/audio_file.ogg"

req = Request(audio_file)

try: response = urlopen(req).read()
except urllib.error.URLError as e:
    print(e.reason)   

sound =  pydub.AudioSegment.from_ogg(BytesIO(response))
sound_wav = sound.export(format = "wav")

speech_key, service_region = "XXXXXXXXXXXXXXXX", "eastus"
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
speech_config.speech_recognition_language="es-ES"
audio_filename = r"C:\some_file_path\3AC3844337F7E5CEAE95.wav"

#audio_config = speechsdk.audio.AudioConfig(sound_wav)
audio_config = speechsdk.audio.AudioConfig(audio_filename = audio_filename)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

done = False

def stop_cb(evt):
    """callback that stops continuous recognition upon receiving an event `evt`"""
    print('CLOSING on {}'.format(evt))
    speech_recognizer.stop_continuous_recognition()
    global done
    done = True

all_results = []
def handle_final_result(evt):
    all_results.append(evt.result.text)

speech_recognizer.recognized.connect(handle_final_result)
# Connect callbacks to the events fired by the speech recognizer
speech_recognizer.recognized.connect(lambda evt: print('RECOGNIZED: {}'.format(evt)))
# stop continuous recognition on either session stopped or canceled events
speech_recognizer.session_stopped.connect(stop_cb)
speech_recognizer.canceled.connect(stop_cb)

# Start continuous speech recognition
speech_recognizer.start_continuous_recognition()
while not done:
    time.sleep(.5)

print("Printing all results:")
print(all_results)

当我用这一行运行我的代码时:

audio_config = speechsdk.audio.AudioConfig(audio_filename = audio_filename)

它工作正常...

但是,当我用这条线运行它时:

audio_config = speechsdk.audio.AudioConfig(sound_wav)

我收到此错误:

ValueError:use_default_microphone 必须是布尔值,是“tempfile._TemporaryFileWrapper object at 0x0000020EC4297668”

4

1 回答 1

1

您收到的错误消息表明这sound_wav是一个临时文件名。然后,如文档中所见,它看起来audio_config = speechsdk.audio.AudioConfig(filename = sound_wav)就是您所需要的。

当您用作audio_filename参数时,可能会发生不同的版本使用不同的名称。您可以改用它。

于 2020-05-13T17:40:18.937 回答