我想使用ffmpeg将视频转换为音频,我还想返回该音频文件,并将其传递给另一个从该音频生成文本的函数。但是当我将“音频”传递给函数错误时,显示找不到文件。
def extract_audio(f):
print("processing", f)
inFile = f
outFile = f[:-3] + "wav"
cmd = "ffmpeg -i {} -vn -ac 2 -ar 44100 -ab 320k -f wav {}".format(inFile, outFile)
os.popen(cmd)
print(outFile)
print("Audio is ready to use..")
return outFile
def audio_to_text(audio):
r = sr.Recognizer()
r.energy_threshold = 4000
with sr.WavFile(open(audio)) as source: # use "test.wav" as the audio source
audio_source = r.record(source) # extract audio data from the file
text = r.recognize_google(audio_source)
try:
print(text) # recognize speech using Google Speech Recognition
except LookupError: # speech is unintelligible
print("Could not understand audio")
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
filename = os.path.basename(file_path)
audio = extract_audio(filename)
audio_to_text(audio)
答案 0 :(得分:1)
文件是否存在?我建议改用exit_state = os.system(cmd)
,并在返回exit_state
之前验证outFile
为零。这样,您可以确保ffmpeg
命令已成功完成。
答案 1 :(得分:0)
一个问题是os.popen()
返回进程输出的句柄并返回到调用方,而使程序在后台运行。
因此,当您调用音频后处理时,ffmpeg
尚未完成运行/可能甚至没有开始创建音频文件(您的进程与ffmpeg
子进程之间的竞争条件)
您必须等待,命令才能结束。为此,我将使用subprocess.check_call
和一个 list 参数,而不是将命令组成为字符串(必须将其拆分为字符串,可能会引起引号/空格错误):
cmd = ["ffmpeg","-i",inFile,"-vn","-ac","2","-ar","44100","-ab","320k","-f","wav",outFile]
subprocess.check_call(cmd)
当check_call
返回(不引发异常)时,这意味着ffmpeg
完成了输出文件的创建。您可以放心尝试将其打开以立即阅读。