【问题标题】:Extract WAV audio file from video after uploading on web, using FFMPEG in django在 django 中使用 FFMPEG 在网络上上传后从视频中提取 WAV 音频文件
【发布时间】:2019-07-31 10:03:09
【问题描述】:

问题

我正在尝试找到一种方法来从 mp4 视频文件中提取音频 wav 文件,该文件由网络用户使用 Django 使用ffmpeg 上传。

如果我发现要提取音频,那么我应该将它保存在我的项目中的哪个位置?

  1. 我尝试使用“Django-ffmpeg”,但没有转换并卡在“等待转换”消息中。
  2. 然后我尝试了:

    import subprocess
    
    subprocess.call('ffmpeg -i filename.mp4 filename.wav')
    

错误

脚本

def validate_file_extension(值): 导入操作系统 从 django.core.exceptions 导入 ValidationError ext = os.path.splitext(value.name)1 # [0] 返回路径+文件名 filename = os.path.splitext(value.name)1 # [0] 返回路径+文件名 valid_extensions = ['.mp4'] 如果在 valid_extensions 中没有 ext.lower(): raise ValidationError(u'不支持的文件扩展名。') 别的: 导入子流程 subprocess.call('ffmpeg -i filename.mp4 filename.wav')

【问题讨论】:

  • 使用可执行文件的完整路径
  • @szatmary 如果您一步一步指导如何使用 ffmpeg 命令来处理由网络用户以 mp4 格式上传的可变文件名,我们想从中提取 wav 音频文件。
  • @szatmary 使用 "inFile = videofile outFile = videofile[:-3] + "wav" cmd = "ffmpeg -i {} {}".format(inFile, outFile) os.popen(cmd )”它给出一个错误“'FieldFile'对象不可下标”。你知道吗?
  • 这行代码的错误“outFile = videofile[:-3] + "wav" ".
  • 我只是用这个 "outFile = os.path.splitext(value.name)[0] + '.wav' " 错误替换 "outFile = videofile[:-3] + "wav" "解决了。​​

标签: python django video ffmpeg


【解决方案1】:

解决方案: - 你可以在Django中这样提取wav音频文件:

def extract_audio(videofile,channels=1, rate=16000):

    your_media_root = settings.MEDIA_ROOT
    path_to_user_folder = your_media_root + videofile.name

    inFile = path_to_user_folder

    temp = tempfile.NamedTemporaryFile(suffix='.wav', delete=False)
    command = ["ffmpeg", "-y", "-i", inFile,
           "-ac", str(channels), "-ar", str(rate),
           "-loglevel", "error", temp.name]
    subprocess.check_output(command)
    print(temp.name)
    return temp.name
  • 其次使用“import tempfile”库来临时存储提取的文件。

【讨论】:

    猜你喜欢
    • 2011-01-08
    • 2012-12-06
    • 2018-10-16
    • 2012-04-12
    • 2015-08-25
    • 2018-05-09
    • 2017-04-23
    • 2019-12-24
    相关资源
    最近更新 更多