无法使用OpenCV打开视频 [英] Can't open video using opencv
问题描述
在执行其他操作时,opencv可以正常工作.它可以打开图像并显示图像.但是它无法打开视频.
我用来打开视频的代码如下
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()
但是执行时,它会输出如下错误消息
[h264 @ 0x1053ba0] AVC: nal size 554779904
[h264 @ 0x1053ba0] AVC: nal size 554779904
[h264 @ 0x1053ba0] no frame!
我的vlc
和mplayer
可以播放此视频,但opencv无法播放.
我已经安装了x264
和libx264-142
编解码器软件包. (使用sudo apt-get install
)
我的ubuntu版本是14.04 trusty
.
我不确定是否是编解码器问题?
我已经使用WITH_UNICAP=ON
或WITH_UNICAP=OFF
重建了opencv,但它根本不影响该问题.错误消息永远不会改变.
这是编解码器问题
我用ffmpeg
将该mp4
文件转换为avi
文件.然后,上面的opencv代码可以很好地播放该avi
文件.
因此,我确定这是一个编解码器问题.
(然后,我使用ffmpeg
将该mp4
文件转换为另一个mp4
文件,认为ffmpeg
有助于将原始的不可读.mp4
编解码器转换为可读的.mp4
编解码器,但生成的.mp4
文件最终损坏了.这个事实可能与这个问题有关,也可能与这个问题无关,以防万一,有人需要此信息.)
问题的答案-重建FFmpeg,然后重建Opencv
尽管知道这是一个编解码器问题,但我尝试了许多其他方法,但仍然无法解决.最后我尝试重建ffmpeg和opencv,然后问题解决了!
以下是我详细的重建过程.
(1)构建ffmpeg
- 下载ffmpeg-2.7.1.tar.bz2
FFmpeg网站: https://www.ffmpeg.org/download.html >
ffmpeg-2.7.1.tar.bz2链接: 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
来自 http://www.ffmpeg.org/platform.html#Advanced -linking-configuration
如果您静态地编译FFmpeg库,并希望使用它们来构建自己的共享库,则可能需要强制PIC支持(在FFmpeg配置期间使用
--enable-pic
).如果目标平台需要位置无关的二进制文件,则应将正确的链接标志(例如
-pie
)传递给--extra-ldexeflags
.
如果遇到错误:
yasm/nasm not found or too old. Use --disable-yasm for a crippled build.
只需
sudo apt-get install yasm
其他构建选项: https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
例如添加选项
--enable-libmp3lame
启用png
编码器. (在./configure
之前,您需要sudo apt-get install libmp3lame-dev
版本≥3.98.3) -
make -j5
(在ffmpeg文件夹下) -
sudo make install
(2)构建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 ..
您可以根据需要更改这些选项.关键选项只有最后一个
-D CMAKE_SHARED_LINKER_FLAGS=-Wl,-Bsymbolic
.如果您忽略此选项,则make
会跳出错误.这也来自 http://www.ffmpeg.org/platform. html#Advanced-linking-configuration (与上面第4步的链接相同)
如果您静态地编译FFmpeg库,并且想使用它们来构建自己的共享库,则可能需要...并将以下选项添加到项目
LDFLAGS
:-Wl,-Bsymbolic
-
make -j5
-
sudo make install
-
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
-
sudo ldconfig
现在,opencv代码应该可以很好地播放mp4
文件了!
我尝试过但没用的方法
- 在
cmake
opencv时尝试添加WITH_UNICAP=ON
WITH_V4L=ON
.但是根本没用. - 尝试在python opencv代码中更改编解码器.但是徒劳.
cap = cv2.VideoCapture("MOV_0006.mp4")
print cap.get(cv2.cv.CV_CAP_PROP_FOURCC)
我在两个环境中对此进行了测试.在第一种环境中,opencv可以工作,而在另一种环境中,opencv无法播放视频.但是两者都打印出相同的编解码器
828601953.0
.我尝试通过
cap.set(cv2.cv.CV_CAP_PROP_FOURCC, cv2.cv.CV_FOURCC(*'H264'))
更改其编解码器,但根本不起作用. - 尝试在我的可行环境中将
opencv-2.4.8/3rdparty/lib/
下的库更改为库.但是甚至无法成功构建.我grep
AVC: nal size
并发现包含此错误消息的库是opencv-2.4.8/3rdparty/lib/libavcodec.a
等.这就是为什么我尝试替换它们的原因.但是事实证明,这是一个坏主意. -
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
尝试安装一些东西和一些东西.但这完全没用.
相关问题
我已经在网上搜索了很多类似的问题,但是没有一个有解决方案!
以下是我认为与我相同的问题.
-
OpenCV/ffmpeg无法播放我的mp4视频. — 来自 opencv-users.nabble.com
-
VideoCapture在OpenCV 2.4.2中不起作用 — 来自 answers.opencv.org
此人提到重建
ffmpeg
!但是论点对我来说还不够. -
MP4读取问题-我安装了OpenCV 2.4.1和python 2.7,并制作了一个简短的程序,成功地读取了avi文件.但是,它无法读取mp4文件. — 来自 answers.opencv.org
-
无法在_Windows 7计算机中使用OpenCV 2.4.3,Python 2.7打开".mp4"视频文件 — 来自堆栈溢出
-
OpenCV 2.4 VideoCapture在Windows 上不起作用 — 来自堆栈溢出 p>
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 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.com
VideoCapture is not working in OpenCV 2.4.2 — from answers.opencv.org
This 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.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
这篇关于无法使用OpenCV打开视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!