【问题标题】:FFMPEG command from Python 3.5 does not actually create audio filePython 3.5 中的 FFMPEG 命令实际上并不创建音频文件
【发布时间】:2018-06-03 11:56:57
【问题描述】:

我有一个 Django Web 应用程序,它接受用户上传的视频/音频并将它们保存到文件夹“../WebAppDirectory/media/recordings”中。

然后,我使用语音转文本 API 来粗略转录音频。这适用于 .wav 和 .mp4 文件,但网络应用程序也接受我想先转换为 .wav,然后传递给 API 的视频 (.MOV)。

像这样在我的命令行中使用 ffmpeg

ffmpeg -i C:\Users\Nathan\Desktop\MeetingRecorderWebAPP\media\recordings\upload_sample.MOV -ab 160k -ac 2 -ar 44100 -vn upload_sample.wav

从原始 .MOV 正确创建 .wav 文件。

但是,当我从 python 运行它时

subprocess.check_call(command, shell=True)

ffmpeg 以

响应

文件“upload_sample.wav”已经存在。覆盖? [是/否]

Python 告诉我

FileNotFoundError: [Errno 2] 没有这样的文件或目录:'C:\Users\Nathan\Desktop\MeetingRecorderWebAPP\media\recordings\upload_sample.wav'

另外值得注意的是,我在 media/recordings/ 目录中没有看到“upload_sample.wav”文件。

这让我相信 Python 和 ffmpeg 可能在不同的文件夹中查找,但我不确定我哪里出错了。当我从 subprocess.check_call 打印 command 并将其复制/粘贴到 cmd 中时,该文件按预期创建。

希望对 ffmpeg/Python 子进程有一定经验的人可以帮助阐明一些问题!以下是我正在使用的文件:

文件夹结构

DjangoWebApp
|---media
|---|---imgs
|---|---recordings
|---|---|---upload_sample.MOV
|---uploaded_audio_to_text.py

uploaded_audio_to_text.py

import speech_recognition as sr
from os import path
import os
import subprocess


def speech_to_text(file_name):
    AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), 'media','recordings', file_name)
    print("Looking at path: ",AUDIO_FILE)
    # get extension
    AUDIO_FILE_EXT = os.path.splitext(AUDIO_FILE)[1]

    if(AUDIO_FILE_EXT == '.MOV'):
        print("File is not .wav: ", AUDIO_FILE_EXT, "found. Converting...")
        # We will use subprocess and ffmpeg to convert this .MOV file to .wav, so we can send to API
        temp_wav = os.path.splitext(file_name)[0] + '.wav'
        print("New audio file will be: ", temp_wav)
        # build CMD ffmpeg command
        command = "ffmpeg -i "
        command += AUDIO_FILE
        command += " -ab 160k -ac 2 -ar 44100 -vn "
        command += temp_wav

        print("Attempting to run this command: \n",command)
        print(subprocess.check_call(command, shell=True))
        print("Past Subprocess.call")
        AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), 'media','recordings', temp_wav)
        print("AUDIO_FILE now set to: ", AUDIO_FILE)

    else:
        # continue with what we are doing
        pass


    r = sr.Recognizer()
    with sr.AudioFile(AUDIO_FILE) as source:
        audio = r.record(source)  # read the entire audio file
        text_transcription = "Sentinel"
        # recognize speech using Microsoft Bing Voice Recognition
        BING_KEY = "MY_KEY_:)"
        try:
            text_transcription = r.recognize_bing(audio, key=BING_KEY)
        except sr.UnknownValueError:
            print("Microsoft Bing Voice Recognition could not understand audio")
        except sr.RequestError as e:
            print("Could not request results from Microsoft Bing Voice Recognition service; {0}".format(e))

    return text_transcription


#my tests
my_relative_file_path = "upload_sample.MOV"
print(speech_to_text(my_relative_file_path))

控制台输出(回溯和我的 print())

Looking at path:  C:\Users\Nathan\Desktop\MeetingRecorderWebAPP\media\recordings\upload_sample.MOV 
File is not .wav:  .MOV found. Converting... 
New audio file will be:  upload_sample.wav Attempting to run this command:
 ffmpeg -i C:\Users\Nathan\Desktop\MeetingRecorderWebAPP\media\recordings\upload_sample.MOV -ab 160k -ac 2 -ar 44100 -vn upload_sample.wav 
ffmpeg version git-2017-12-18-74f408c Copyright (c) 2000-2017 the FFmpeg developers   built with gcc 7.2.0 (GCC)   
----REMOVED SOME FFMPEG OUTPUT FOR BREVITY----
File 'upload_sample.wav' already exists. Overwrite ? [y/N] y 
Stream mapping:   Stream #0:1 -> #0:0 (aac (native) -> pcm_s16le (native)) Press [q] to stop, [?] for help Output #0, wav, to 'upload_sample.wav':   Metadata:
    major_brand     : qt  
    minor_version   : 0
    compatible_brands: qt  
    com.apple.quicktime.creationdate: 2017-12-19T16:06:10-0500
    com.apple.quicktime.make: Apple
    com.apple.quicktime.model: iPhone 6
    com.apple.quicktime.software: 10.3.3
    ISFT            : Lavf58.3.100
    Stream #0:0(und): Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s (default)
    Metadata:
      creation_time   : 2017-12-19T21:06:11.000000Z
      handler_name    : Core Media Data Handler
      encoder         : Lavc58.8.100 pcm_s16le size=    1036kB time=00:00:06.01 bitrate=1411.3kbits/s speed=N/A     video:0kB audio:1036kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.007352% 
0 
Traceback (most recent call last): Past Subprocess.call   
File "C:\Users\Nathan\Desktop\MeetingRecorderWebAPP\uploaded_audio_to_text.py", line 53, in <module> 
AUDIO_FILE now set to:  C:\Users\Nathan\Desktop\MeetingRecorderWebAPP\media\recordings\upload_sample.wav
    print(speech_to_text(my_relative_file_path))   
File "C:\Users\Nathan\Desktop\MeetingRecorderWebAPP\uploaded_audio_to_text.py", line 36, in speech_to_text
    with sr.AudioFile(AUDIO_FILE) as source:   
File "C:\Users\Nathan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\speech_recognition\__init__.py", line 203, in __enter__
    self.audio_reader = wave.open(self.filename_or_fileobject, "rb")   
File "C:\Users\Nathan\AppData\Local\Programs\Python\Python36-32\lib\wave.py", line 499, in open
    return Wave_read(f)   
File "C:\Users\Nathan\AppData\Local\Programs\Python\Python36-32\lib\wave.py", line 159, in __init__
    f = builtins.open(f, 'rb') 
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Nathan\\Desktop\\MeetingRecorderWebAPP\\media\\recordings\\upload_sample.wav'

Process finished with exit code 1

【问题讨论】:

  • check_call (cwd=) 中设置正确的工作目录,或者在ffmpeg 命令中为输出使用绝对路径,就像对输入所做的那样。在命令中添加-y 以覆盖现有输出而不提示。
  • 完美运行,非常感谢。我的命令现在是:'ffmpeg -y -i C:\Users\Nathan\Desktop\MeetingRecorderWebAPP\media\recordings\upload_test_2.MOV -ab 160k -ac 2 -ar 44100 -vn C:\Users\Nathan\Desktop\MeetingRecorderWebAPP \media\recordings\upload_test_2.wav'

标签: python audio ffmpeg


【解决方案1】:

aergistal 建议的正确答案。我需要设置输出文件的绝对路径!

更改命令

ffmpeg -i C:\Users\Nathan\Desktop\MeetingRecorderWebAPP\media\recordings\upload_sample.MOV -ab 160k -ac 2 -ar 44100 -vn upload_sample.wav

到新命令

ffmpeg -y -i C:\Users\Nathan\Desktop\MeetingRecorderWebAPP\media\recordings\upload_test_2.MOV -ab 160k -ac 2 -ar 44100 -vn C:\Users\Nathan\Desktop\MeetingRecorderWebAPP\media\recordings\upload_test_2.wav

【讨论】:

  • 您可以删除-ab 160k,因为它不会对 wav 输出做任何事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-29
  • 1970-01-01
  • 1970-01-01
  • 2022-11-08
  • 2019-11-20
  • 2012-05-24
  • 1970-01-01
相关资源
最近更新 更多