The opencv works fine when doing other things. It can open images and show images. But it can't open a video.
The code I'm using to open a video is as below
import cv2
cap = cv2.VideoCapture("MOV_0006.mp4")
while True:
ret, frame = cap.read()
cv2.imshow('video', frame)
if cv2.waitKey(1) & 0xff == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
But when executing, it outputs error messages like below
[h264 @ 0x1053ba0] AVC: nal size 554779904
[h264 @ 0x1053ba0] AVC: nal size 554779904
[h264 @ 0x1053ba0] no frame!
My vlc
and mplayer
can play this video, but the opencv can't.
I have installed x264
and libx264-142
codec package. (using sudo apt-get install
)
My version of ubuntu is 14.04 trusty
.
I'm not sure is it a codec problem or not?
I have rebuilt opencv either with WITH_UNICAP=ON
or with WITH_UNICAP=OFF
, but it doesn't affect the problem at all. The error messages never change.
It's a codec problem
I converted that
mp4
file to anavi
file withffmpeg
. Then the above opencv code can play thatavi
file well.Therefore I am sure that this is a codec problem.
(I then converted that
mp4
file to anothermp4
file usingffmpeg
, thinking maybeffmpeg
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
tar -xvf ffmpeg-2.7.1.tar.bz2
cd ffmpeg-2.7.1
./configure --enable-pic --extra-ldexeflags=-pie
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 ..
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
WITH_UNICAP=ON
WITH_V4L=ON
whencmake
opencv. But didn't work at all.opencv-2.4.8/3rdparty/lib/
into libraries in my workable environment. But couldn't even successfully build.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
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.com
VideoCapture is not working in OpenCV 2.4.2 — from answers.opencv.org
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.org
Cannot open “.mp4” video files using OpenCV 2.4.3, Python 2.7 in _Windows 7 machine — from Stack Overflow
OpenCV 2.4 VideoCapture not working on Windows — from Stack Overflow