OpenCV 2.4 VideoCapture not working on Windows
Add C:\OpenCV\3rdparty\ffmpeg\
to the Windows PATH environment variable or copy opencv_ffmpeg.dll
from that directory to C:\Python27\
or to a directory that is in the PATH. Alternatively, use the OpenCV binaries from http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv.
Renaming the opencv_ffmpeg.dll
file may also be necessary.
For OpenCV version X.Y.Zopencv_ffmpeg.dll
==> opencv_ffmpegXYZ.dll
For 64-bit OpenCV version X.Y.Zopencv_ffmpeg.dll
==> opencv_ffmpegXYZ_64.dll
VideoCapture OpenCV 2.4.2 error in windows
In my case, the same problem was resolved after deleting all opencv_***.dll
files in C:\Windows\System32
. So, I use the dll files just through the path like "%PATH%;C: \Program Files \OpenCV2.4.2\build\x86\vc10/bin"
. Please try it.
OpenCV VideoCapture not read any video File in Windows 7 32bit
For some unknown reason in a system 32bit the building created a dll called opencv_ffmpeg248_64.dll and the installation of opencv was in a folder "x64", so i think that dll was for 64bit system. I download the pre built version of openCV and i use the dll included in that package.
VideoCapture is not working in OpenCV 2.4.2
So, I realized there was some problem with ffmpeg. and I rebuilt and installed ffmpeg and then changed flags for cmake to make opencv.
For ffmpeg,
./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab
make
sudo make install
For OpenCV,
cmake -D CMAKE_BUILD_TYPE=RELEASE ..
make
sudo make install
Here's the complete script to install OpenCV 2.4.2 https://github.com/jayrambhia/Install-OpenCV/blob/master/Ubuntu/2.4/opencv2_4_2.sh
and here's my blogpost about OpenCV 2.4.2 installation in detail http://jayrambhia.com/blog/install-opencv-2-4-in-ubuntu-12-04-precise-pangolin/
Can't open video using opencv
It's a codec problem
I converted that mp4
file to an avi
file with ffmpeg
. Then the above opencv code can play that avi
file well.
Therefore I am sure that this is a codec problem.
(I then converted that mp4
file to another mp4
file using ffmpeg
, thinking maybe ffmpeg
would help turning that original unreadable .mp4
codec into a readable .mp4
codec, but the resulting .mp4
file ended up broken. This fact may or may not relate to this problem, just mentioning, in case anybody needs this information.)
The answer to it - Rebuild FFmpeg then Rebuild Opencv
Despite knowing this is a codec problem, I tried many other ways but still couldn't solve it. At last I tried rebuilding ffmpeg and opencv, then the problem was solved!
Following is my detailed rebuilding procedure.
(1) Build ffmpeg
- Download ffmpeg-2.7.1.tar.bz2
FFmpeg website: https://www.ffmpeg.org/download.html
ffmpeg-2.7.1.tar.bz2 link: http://ffmpeg.org/releases/ffmpeg-2.7.1.tar.bz2
tar -xvf ffmpeg-2.7.1.tar.bz2
cd ffmpeg-2.7.1
./configure --enable-pic --extra-ldexeflags=-pie
From http://www.ffmpeg.org/platform.html#Advanced-linking-configuration
If you compiled FFmpeg libraries statically and you want to use them to build your own shared library, you may need to force PIC support (with
--enable-pic
during FFmpeg configure).If your target platform requires position independent binaries, you should pass the correct linking flag (e.g.
-pie
) to--extra-ldexeflags
.
If you encounter error:
yasm/nasm not found or too old. Use --disable-yasm for a crippled build.
Just
sudo apt-get install yasm
Further building options: https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
e.g. Adding option
--enable-libmp3lame
enablespng
encoder. (Before./configure
you need tosudo apt-get install libmp3lame-dev
with version ≥ 3.98.3)make -j5
(under ffmpeg folder)sudo make install
(2) Build Opencv
wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.9/opencv-2.4.9.zip
unzip opencv-2.4.9.zip
cd opencv-2.4.9
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_QT=OFF -D WITH_V4L=ON -D CMAKE_SHARED_LINKER_FLAGS=-Wl,-Bsymbolic ..
You can change those options depend on your needs. Only the last one
-D CMAKE_SHARED_LINKER_FLAGS=-Wl,-Bsymbolic
is the key option. If you omit this one then themake
will jump out errors.This is also from http://www.ffmpeg.org/platform.html#Advanced-linking-configuration (the same link of step 4 above)
If you compiled FFmpeg libraries statically and you want to use them to build your own shared library, you may need to ... and add the following option to your project
LDFLAGS
:-Wl,-Bsymbolic
make -j5
sudo make install
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
Now the opencv code should play a mp4
file well!
Methods I tried but didn't work
- Try add
WITH_UNICAP=ON
WITH_V4L=ON
whencmake
opencv. But didn't work at all. - Try changing codec inside the python opencv code. But in vain.
cap = cv2.VideoCapture("MOV_0006.mp4")
print cap.get(cv2.cv.CV_CAP_PROP_FOURCC)
I tested this in two environment. In the first environment the opencv works, and in the other the opencv fails to play a video. But both printed out same codec
828601953.0
.I tried to change their codec by
cap.set(cv2.cv.CV_CAP_PROP_FOURCC, cv2.cv.CV_FOURCC(*'H264'))
but didn't work at all. - Try changing the libraries under
opencv-2.4.8/3rdparty/lib/
into libraries in my workable environment. But couldn't even successfully build.I grep
AVC: nal size
and find the libraries contain this error message areopencv-2.4.8/3rdparty/lib/libavcodec.a
etc. That's why I tried to replace them. But it turns out that this is a bad idea. sudo apt-get -y install libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff4-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip
Try to install some that thing and some this thing. But it was totally useless.
Problems Related
I've searched the web and there are many similar problems, but NONE of them had a solution!
Below are problems I think the same as mine.
OpenCV/ffmpeg does not play my mp4 video. —
from opencv-users.nabble.comVideoCapture is not working in OpenCV 2.4.2 —
from answers.opencv.orgThis one mentions rebuilding
ffmpeg
! But the arguments weren't enough for me.Mp4 reading problem - I installed OpenCV 2.4.1 and python 2.7 and made a short program that reads the avi file successfully. However it fails to read mp4 file. —
from answers.opencv.orgCannot open “.mp4” video files using OpenCV 2.4.3, Python 2.7 in _Windows 7 machine —
from Stack OverflowOpenCV 2.4 VideoCapture not working on Windows —
from Stack Overflow
Related Topics
Create Nice Column Output in Python
Error Unicodedecodeerror: 'Utf-8' Codec Can't Decode Byte 0Xff in Position 0: Invalid Start Byte
What's the Simplest Way of Detecting Keyboard Input in a Script from the Terminal
Access a Function Variable Outside the Function Without Using "Global"
Understanding Nested List Comprehension
Opencv 2.4 Videocapture Not Working on Windows
How to Get If a Key Is Pressed Pygame
Showing the Stack Trace from a Running Python Application
What Soap Client Libraries Exist for Python, and Where Is the Documentation for Them
How to Access the Ith Column of a Numpy Multidimensional Array
How to Get the Path of the Current Executed File in Python
What Does %S Mean in a Python Format String
How to Compare Version Numbers in Python
How to Create a Custom String Representation for a Class Object