无法使用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 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
enables png
encoder. (Before ./configure
you need to sudo apt-get install libmp3lame-dev
with version ≥ 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 ..
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 the make
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
现在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)
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.
- 尝试将
opencv-2.4.8/3rdparty/lib/
下的库更改为我可工作环境中的库。但甚至无法成功构建。
I grep AVC: nal size
and find the libraries contain this error message are opencv-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.
相关问题
我在网上搜索了很多类似的问题,但是 NONE 个有解决方案!
下面是我认为和我一样的问题
OpenCV/ffmpeg 无法播放我的 mp4 视频。 —
来自opencv-users.nabble.com
VideoCapture 在 OpenCV 2.4.2 中不工作 —
来自 answers.opencv.org
This one mentions rebuilding ffmpeg
! But the arguments weren't enough for me.
Mp4 读取问题 - 我安装了 OpenCV 2.4.1 和 python 2.7 并制作了一个 成功读取 avi 文件的小程序。但是它无法读取 mp4 文件。 —
从answers.opencv.org
无法使用 OpenCV 2.4.3 打开“.mp4”视频文件,Python2.7 in _Windows7 机器 —
从Stack Overflow
OpenCV 2.4 VideoCapture 在 Windows 上 不工作 —
从Stack Overflow
对于 ubuntu 14.04,我就是这样修复它的。
安装OpenCV所需的依赖项
sudo apt-get 删除 ffmpeg x264 libx264-dev
sudo apt-get 更新
sudo apt-get install libgstreamer0.10-0 libgstreamer0.10-dev gstreamer0.10-tools gstreamer0.10-plugins-base libgstreamer-plugins-base0.10-dev gstreamer0.10-plugins-good gstreamer0。 10-plugins-ugly gstreamer0.10-plugins-bad gstreamer0.10-ffmpeg
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
sudo add-apt-repository ppa:mc3man/gstffmpeg-keep
sudo apt-get 更新
sudo apt-get install gstreamer0.10-ffmpeg
sudo apt-get install gstreamer0.10-plugins-bad
sudo apt-get 更新
在anaconda3中安装ffmpeg
conda 安装-c menpo ffmpeg=2.7.0
下载 Opencv 3.1.0"
wget https://github.com/Itseez/opencv/archive/3.1.0.zip
解压 3.1.0.zip
mkdir 构建
光盘构建
cmake -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DWITH_CUDA=OFF -DWITH_FFMPEG=OFF -DENABLE_AVX=ON -DWITH_OPENGL=ON -DWITH_OPENCL=ON -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_V4L=ON -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=释放 -DCMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") -DPYTHON3_EXECUTABLE=$(其中 python3.5) -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())") ..
制作
须藤安装
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 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
(在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 ..
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
现在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)
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. - 尝试将
opencv-2.4.8/3rdparty/lib/
下的库更改为我可工作环境中的库。但甚至无法成功构建。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.
相关问题
我在网上搜索了很多类似的问题,但是 NONE 个有解决方案!
下面是我认为和我一样的问题
OpenCV/ffmpeg 无法播放我的 mp4 视频。 — 来自opencv-users.nabble.com
VideoCapture 在 OpenCV 2.4.2 中不工作 — 来自 answers.opencv.org
This one mentions rebuilding
ffmpeg
! But the arguments weren't enough for me.Mp4 读取问题 - 我安装了 OpenCV 2.4.1 和 python 2.7 并制作了一个 成功读取 avi 文件的小程序。但是它无法读取 mp4 文件。 — 从answers.opencv.org
无法使用 OpenCV 2.4.3 打开“.mp4”视频文件,Python2.7 in _Windows7 机器 — 从Stack Overflow
OpenCV 2.4 VideoCapture 在 Windows 上 不工作 — 从Stack Overflow
对于 ubuntu 14.04,我就是这样修复它的。
安装OpenCV所需的依赖项
sudo apt-get 删除 ffmpeg x264 libx264-dev
sudo apt-get 更新
sudo apt-get install libgstreamer0.10-0 libgstreamer0.10-dev gstreamer0.10-tools gstreamer0.10-plugins-base libgstreamer-plugins-base0.10-dev gstreamer0.10-plugins-good gstreamer0。 10-plugins-ugly gstreamer0.10-plugins-bad gstreamer0.10-ffmpeg
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
sudo add-apt-repository ppa:mc3man/gstffmpeg-keep
sudo apt-get 更新
sudo apt-get install gstreamer0.10-ffmpeg
sudo apt-get install gstreamer0.10-plugins-bad
sudo apt-get 更新
在anaconda3中安装ffmpeg
conda 安装-c menpo ffmpeg=2.7.0
下载 Opencv 3.1.0"
wget https://github.com/Itseez/opencv/archive/3.1.0.zip 解压 3.1.0.zip
mkdir 构建
光盘构建
cmake -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DWITH_CUDA=OFF -DWITH_FFMPEG=OFF -DENABLE_AVX=ON -DWITH_OPENGL=ON -DWITH_OPENCL=ON -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_V4L=ON -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=释放 -DCMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") -DPYTHON3_EXECUTABLE=$(其中 python3.5) -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())") ..
制作
须藤安装