Permanent fix for Opencv videocapture

2018-12-31 20:42发布

问题:

This question has been posed numerous times on many websites, but not definitive solution. I am trying to run Opencv with a video using function:

import cv2
cap = cv2.VideoCapture(\'video.mp4\')
if(cap.isOpened()==False):
print \"Error opening camera\"

But it fails every time. I have tried almost all steps from various sites, but couldn\'t get it to work (including rebuilding ffmpeg separately).

Any help would be much appreciated.

My platform is Ubuntu17 and Python3.

回答1:

This answer is written with Linux and Python in mind, but the general idea can be applied to any OS and language supported by OpenCV.

The VideoCapture class not opening the video file can have many causes, but the following three applies to most cases.

OpenCV FFMPEG support:

By default OpenCV uses ffmpeg to read video files. OpenCV may not have been built with FFMPEG support. To find out if OpenCV was built with FFMPEG support, in terminal enter:

python -c \"import cv2; print(cv2.getBuildInformation())\" | grep -i ffmpeg

The output should be something like:

FFMPEG: YES

If the output is No then follow an online guide to build OpenCV from source with ffmpeg support.

FFMPEG Codec:

It\'s possible that FFMPEG does not have codec for your specific file. We are going to use this video as an example, to show if FFMPEG has decoding capability for this file.

First, we need to find the encoding format used in this video file. We will be using the mediainfo program. In terminal, enter:

mediainfo video_file.mp4

In the output, under the video heading, look for format. In this case the video encoding used is AVC, which is another name for H264.

\"pic\"

Now, we try to find if FFMPEG supports codec for decoding AVC encoded files. In terminal:

ffmpeg -codecs | grep -i avc

On my machine, the output is:

DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_crystalhd h264_vdpau ) (encoders: libx264 libx264rgb )

We are interested in DEV, which stands for Decoding, Encoding and Video. This means that AVC is a video encoding format and FFMPEG supports both encoding and decoding capabilities for this format.

File PATH

Lastly, check if the file path is correct or even if the file exists.



回答2:

I followed Steps from Link

and

This Cmake command in above Link is as below which worked

cmake -DWITH_CUDA=OFF -DBUILD_TIFF=ON -DBUILD_opencv_java=ON -DWITH_FFMPEG=ON -DBUILD_opencv_python3=ON -DENABLE_AVX=ON -DWITH_OPENGL=ON -DWITH_OPENCL=ON -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_V4L=ON -DWITH_VTK=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=RELEASE -DBUILD_opencv_python2=OFF -DCMAKE_INSTALL_PREFIX=$(python3 -c \"import sys; print(sys.prefix)\") -DPYTHON3_EXECUTABLE=$(which python3) -DPYTHON3_INCLUDE_DIR=$(python3 -c \"from distutils.sysconfig import get_python_inc; print(get_python_inc())\") -DPYTHON3_PACKAGES_PATH=$(python3 -c \"from distutils.sysconfig import get_python_lib; print(get_python_lib())\") -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D PYTHON_EXECUTABLE=/home/user/anaconda3/bin/python -D BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=RELEASE ..

I am not well verse of OpenCV build but exact PYTHON_EXECUTABLE=/home/user/anaconda3/bin/python path is which points I think the OpenCV bindings for particular python to get updated