Python检查损坏的视频文件(捕获OpenCV错误) [英] Python check for corrupted video file (catch OpenCV error)

查看:652
本文介绍了Python检查损坏的视频文件(捕获OpenCV错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种检查视频文件是否损坏的方法.

I'm searching for a way to check if a video file is corrupted.

我正在使用cv2(适用于python的OpenCV)加载视频.如果视频文件已损坏,我想跳过该文件,然后转到下一个文件.

I'm using cv2 (OpenCV for python) to load the video. If the video file is corrupt, I would like to skip the file and move on to the next one.

我发现了这个 stackoverflow问题,因此尝试了以下操作:

I found this stackoverflow question and therefore tried this:

try:
    vid = cv2.VideoCapture(corrupt_video_file)
except cv2.error as e:
    print(e)
except:
    print('error')

但是我仍然收到以下错误:

but I still receive the following error:

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x1b2d000] moov atom not found Unable to stop the stream: Inappropriate ioctl for device OpenCV Error: Unspecified error (GStreamer: unable to start pipeline ) in cvCaptureFromCAM_GStreamer, file /home/student/programs/opencv-3.4.0/modules/videoio/src/cap_gstreamer.cpp, line 890 VIDEOIO(cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename)): raised OpenCV exception:

/home/student/programs/opencv-3.4.0/modules/videoio/src/cap_gstreamer.cpp:890: error: (-2) GStreamer: unable to start pipeline  in function cvCaptureFromCAM_GStreamer

似乎引发了cpp错误,而不是python错误.(我从源代码编译了opencv,这是其他项目所需要的)

It appears that a cpp error is raised and not a python error. (I compiled opencv from source, was needed for an other project)

是否还可以捕获这些cpp错误?
还是有其他方法可以检查python中损坏的视频文件?
还是问题在于我自己编译了openCV,而不仅仅是安装了python-cv2?

Is it possible to also catch those cpp errors?
Or is there an other way to check for corrupted video files in python?
Or does the problem lie with that I compiled openCV myself, and not just installed the python-cv2?

任何帮助或建议都将受到欢迎!

Any help or suggestions would be welcome!

推荐答案

还是问题在于我自己编译了openCV,而不是仅仅安装了python-cv2?

Or does the problem lie with that I compiled openCV myself, and not just installed the python-cv2?

测试应该很简单:只需使用发行版运行脚本即可.

That should be simple to test: just run your script with a release version.

是否还可以捕获这些cpp错误?

Is it possible to also catch those cpp errors?

由于您能够自己编译OpenCV,因此您可能有机会找出 #define 来控制错误行为.我快速浏览了一下,但这对于未启动"很复杂,因此,我所提供的只是指向文档和源代码的一些链接,您可以从以下位置开始搜索:

Since you are able to compile OpenCV yourself, you might have a chance figuring out a #define to control the behaviour on error. I had a quick look but this quite involved for the "uninitiated", so all I can give are a few links to documentation and source code you might start your search at:

cap_gstreamer.cpp#L890 调用宏 CV_Error >(),其中包装 cv :: error() .

cap_gstreamer.cpp#L890 calls the macro CV_Error (source) which wraps cv::error().

库甚至可能使用环境变量或标志来实现抑制这些错误(无需重新编译).

There may even be environment variables or flags used by the library that could achieve suppressing these errors (without recompiling).

但是,由于您已经在进行编译,因此您当然可以尝试破解"源代码以忽略该错误.

But since you are compiling already, of course you could try to "hack" the source to ignore the error.

您可能可以在 http://answers.opencv.org/questions/上找到实际的帮助.,甚至可能来自开发人员.

You can probably find actual help at http://answers.opencv.org/questions/, maybe even from developers.

对不起,我没有看清楚问题.我通过 pip3.exe安装opencv-python (参见PyPi ,官方下载文件仅为您提供了一个zip文件,没有明显的Python使用说明.

Sorry, I misread the question. I installed OpenCV via pip3.exe install opencv-python (see PyPi, the official download just gives you a zip file with no apparent instructions for the use with Python).

只需测试 isOpened 似乎有效:

Simply testing isOpened seems to work:

#!python3
"""https://stackoverflow.com/a/49751695/1619432"""
import sys
import cv2  # https://pypi.python.org/pypi/opencv-python

filename = "big_buck_bunny.mp4"

# info
print("Python:", sys.version)
print("CV2:   ", cv2.__version__)
print(filename)

# create empty file
if False:
    with open(filename, "wb") as f:
        pass

# produce error
try:
    vid = cv2.VideoCapture(filename)
    if not vid.isOpened():
        raise NameError('Just a Dummy Exception, write your own')
except cv2.error as e:
    print("cv2.error:", e)
except Exception as e:
    print("Exception:", e)
else:
    print("no problem reported")

print("done")

文件不存在:

Python: 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
CV2:    3.4.0
does_not_exist.mp4
warning: Error opening file (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:808)
warning: does_not_exist.mp4 (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:809)
Exception: Just a Dummy Exception, write your own
done

零字节文件:

Python: 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
CV2:    3.4.0
does_exist.mp4
[mov,mp4,m4a,3gp,3g2,mj2 @ 000000a171d82880] moov atom not found
warning: Error opening file (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:808)
warning: does_exist.mp4 (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:809)
Exception: Just a Dummy Exception, write your own
done

有效文件

Python: 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
CV2:    3.4.0
big_buck_bunny.mp4
no problem reported
done

请参见错误上的Python教程,了解如何处理异常并编写您的自己的.

See the Python Tutorial on Errors on how to handle Exceptions and write your own.

要抑制错误消息,请检查上面的删除线部分,或者可以将其重定向(通过管道无处到达).

To supress the error messages, check out may strike-through part above, or maybe they can just be redirected (pipe to nowhere).

或者还有其他方法可以检查python中损坏的视频文件吗?

Or is there an other way to check for corrupted video files in python?

您可以尝试找到一个外部程序,该程序可以检查但不实际播放文件(也许 VLC ffmpeg ,..),然后从Python运行并与之通信(例如 subprocess ).

You could try to find an external program that has an option to inspect but not actually play the file (maybe VLC, ffmpeg, ..) and run and communicate with it from Python (e.g. subprocess).

适用于Python的API文档: cv2.VideoCapture()

API doc for Python: cv2.VideoCapture()

这篇关于Python检查损坏的视频文件(捕获OpenCV错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆