【问题标题】:It's possible to catch ffmpeg errors with python?可以用 python 捕获 ffmpeg 错误吗?
【发布时间】:2019-08-25 17:04:03
【问题描述】:

您好,我正在尝试使用 python 为 django 制作视频转换器,我分叉了 django-ffmpeg 模块,它几乎可以完成我想要的所有操作,除非转换失败时不会捕获错误。

基本上模块将ffmpeg命令传递给命令行界面以进行如下转换:

/usr/bin/ffmpeg -hide_banner -nostats -i %(input_file)s -target 电影-dvd %(output_file)

Module 使用这种方法将 ffmpeg 命令传递给 cli 并获取输出:

def _cli(self, cmd, without_output=False):
    print 'cli'
    if os.name == 'posix':
        import commands
        return commands.getoutput(cmd)
    else:
        import subprocess
        if without_output:
            DEVNULL = open(os.devnull, 'wb')
            subprocess.Popen(cmd, stdout=DEVNULL, stderr=DEVNULL)
        else:
            p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
            return p.stdout.read()

但是例如,我上传了一个损坏的视频文件,它只返回打印在 cli 上的 ffmpeg 消息,但没有任何东西触发知道某事失败

这是转换失败时的 ffmpeg 示例输出:

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x237d500] 格式 mov,mp4,m4a,3gp,3g2,mj2 仅以 1 的低分检测,可能误检测! [mov,mp4,m4a,3gp,3g2,mj2 @ 0x237d500] moov atom not found /home/user/PycharmProjects/videotest/media/videos/orig/270f412927f3405aba041265725cdf6b.mp4: 处理输入时发现无效数据

我想知道是否有任何方法可以使它成为异常以及如何处理,以便我可以轻松处理。

我想到的唯一选择是在 cli 输出消息字符串中搜索:“处理输入时发现无效数据”,但我不确定这是否是最佳方法。任何人都可以帮助我并指导我。

【问题讨论】:

    标签: python video ffmpeg


    【解决方案1】:

    您需要检查您正在创建的 Popen 对象的returncode。 查看文档:https://docs.python.org/3/library/subprocess.html#subprocess.Popen

    您的代码应该等待子进程完成(使用wait)然后检查returncode。如果returncode 是!= 0,那么您可以提出任何您想要的异常。

    【讨论】:

      【解决方案2】:

      这是我实现它的方式,以防它对其他人有用:

      def _cli(self, cmd):
              errors = False
              import subprocess
              try:
                  p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
                  stdoutdata, stderrdata = p.communicate()
                  if p.wait() != 0:
                      # Handle error / raise exception
                      errors = True
                      print "There were some errors"
                      return stderrdata, errors
                  print 'conversion success '
                  return stderrdata, errors
              except OSError as e:
                  errors = True
                  return e.strerror, errors
      

      【讨论】:

        猜你喜欢
        • 2012-01-11
        • 2015-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-20
        • 2015-04-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多