Python check for corrupted video file (catch OpenCV error)

允我心安 提交于 2020-12-08 06:02:47

问题


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

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.

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

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

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!


回答1:


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.

Is it possible to also catch those cpp errors?

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 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.

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


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).

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")

File does not exist:

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

Zero-byte file:

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

Valid file

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

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).

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

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).


API doc for Python: cv2.VideoCapture()



来源:https://stackoverflow.com/questions/49750691/python-check-for-corrupted-video-file-catch-opencv-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!