Python 无法使用opencv打开视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31040746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:23:06  来源:igfitidea点击:

Can't open video using opencv

pythonopencvvideocodec

提问by AnnieFromTaiwan

The opencv works fine when doing other things. It can open images and show images. But it can't open a video.

在做其他事情时,opencv 工作正常。它可以打开图像并显示图像。但是不能打开视频。

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 vlcand mplayercan play this video, but the opencv can't.

我的vlcmplayer可以播放这个视频,但是opencv不能。

I have installed x264and libx264-142codec package. (using sudo apt-get install)

我已经安装x264libx264-142编解码器包。(使用sudo apt-get install

My version of ubuntu is 14.04 trusty.

我的 ubuntu 版本是14.04 trusty.

I'm not sure is it a codec problem or not?

我不确定它是否是编解码器问题?

I have rebuilt opencv either with WITH_UNICAP=ONor with WITH_UNICAP=OFF, but it doesn't affect the problem at all. The error messages never change.

我已经用WITH_UNICAP=ON或重建了 opencv WITH_UNICAP=OFF,但它根本不影响问题。错误消息永远不会改变。

采纳答案by AnnieFromTaiwan

It's a codec problem

这是编解码器的问题

I converted that mp4file to an avifile with ffmpeg. Then the above opencv code can play that avifile well.

我将该mp4文件转换为avi带有ffmpeg. 那么上面的opencv代码就可以avi很好地播放那个文件了。

Therefore I am sure that this is a codec problem.

因此我确信这是一个编解码器问题。

(I then converted that mp4file to another mp4file using ffmpeg, thinking maybe ffmpegwould help turning that original unreadable .mp4codec into a readable .mp4codec, but the resulting .mp4file ended up broken. This fact may or may not relate to this problem, just mentioning, in case anybody needs this information.)

(然后我使用将该mp4文件转换为另一个mp4文件ffmpeg,认为可能ffmpeg有助于将原始不可读的.mp4编解码器转换为可读的.mp4编解码器,但结果.mp4文件最终被破坏。这个事实可能与这个问题有关,也可能与这个问题无关,只是提一下,以防万一需要这些信息。)

The answer to it - Rebuild FFmpeg then Rebuild Opencv

答案 - 重建 FFmpeg 然后重建 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!

尽管知道这是编解码器问题,但我尝试了许多其他方法,但仍然无法解决。最后尝试重建ffmpeg和opencv,问题解决了!

Following is my detailed rebuilding procedure.

以下是我详细的重建过程。

(1) Build ffmpeg

(1)构建ffmpeg

  1. 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

  2. tar -xvf ffmpeg-2.7.1.tar.bz2
  3. cd ffmpeg-2.7.1
  4. ./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-picduring 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-libmp3lameenables pngencoder. (Before ./configureyou need to sudo apt-get install libmp3lame-devwith version ≥ 3.98.3)

  5. make -j5(under ffmpeg folder)
  6. sudo make install
  1. 下载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

  2. tar -xvf ffmpeg-2.7.1.tar.bz2
  3. cd ffmpeg-2.7.1
  4. ./configure --enable-pic --extra-ldexeflags=-pie

    来自http://www.ffmpeg.org/platform.html#Advanced-linking-configuration

    如果您静态编译了 FFmpeg 库,并且希望使用它们来构建您自己的共享库,则可能需要强制 PIC 支持(--enable-pic在 FFmpeg 配置期间使用)。

    如果您的目标平台需要与位置无关的二进制文件,您应该将正确的链接标志(例如-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)

  5. make -j5(在 ffmpeg 文件夹下)
  6. sudo make install

(2) Build Opencv

(2) 搭建Opencv

  1. wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.9/opencv-2.4.9.zip
  2. unzip opencv-2.4.9.zip
  3. cd opencv-2.4.9
  4. mkdir build
  5. cd build
  6. 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,-Bsymbolicis the key option. If you omit this one then the makewill 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

  7. make -j5
  8. sudo make install
  9. sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
  10. sudo ldconfig
  1. wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.9/opencv-2.4.9.zip
  2. unzip opencv-2.4.9.zip
  3. cd opencv-2.4.9
  4. mkdir build
  5. cd build
  6. 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

  7. make -j5
  8. sudo make install
  9. sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
  10. sudo ldconfig

Now the opencv code should play a mp4file well!

现在opencv代码应该可以mp4很好地播放文件了!

Methods I tried but didn't work

我试过但没用的方法

  1. Try add WITH_UNICAP=ONWITH_V4L=ONwhen cmakeopencv. But didn't work at all.
  2. 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.

  3. 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 sizeand find the libraries contain this error message are opencv-2.4.8/3rdparty/lib/libavcodec.aetc. That's why I tried to replace them. But it turns out that this is a bad idea.

  4. 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.

  1. 尝试WITH_UNICAP=ONWITH_V4L=ONcmakeopencv时添加。但根本没有用。
  2. 尝试在 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'))但根本没有用。

  3. 尝试将下面的库更改为opencv-2.4.8/3rdparty/lib/我可用环境中的库。但甚至无法成功构建。

    我 grepAVC: nal size并找到包含此错误消息的库opencv-2.4.8/3rdparty/lib/libavcodec.a等等。这就是我尝试替换它们的原因。但事实证明这是一个坏主意。

  4. 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.2from 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 machinefrom Stack Overflow

  • OpenCV 2.4 VideoCapture not working on Windowsfrom Stack Overflow

  • 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”视频文件来自Stack 堆栈

  • OpenCV 2.4 VideoCapture 不适用于 Windows来自Stack 堆栈

回答by Jimmy

for ubuntu 14.04, this is how i fixed it.

对于 ubuntu 14.04,这就是我修复它的方式。

install the dependencies required for OpenCV"

安装 OpenCV 所需的依赖项”

sudo apt-get remove ffmpeg x264 libx264-dev

sudo apt-get 删除 ffmpeg x264 libx264-dev

sudo apt-get update

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 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 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 解压

sudo add-apt-repository ppa:mc3man/gstffmpeg-keep

须藤添加-apt-repository ppa:mc3man/gstffmpeg-keep

sudo apt-get update

sudo apt-get 更新

sudo apt-get install gstreamer0.10-ffmpeg

须藤 apt-get 安装 gstreamer0.10-ffmpeg

sudo apt-get install gstreamer0.10-plugins-bad

须藤 apt-get 安装 gstreamer0.10-plugins-bad

sudo apt-get update

sudo apt-get 更新

Install ffmpeg inside anaconda3"

在anaconda3内安装ffmpeg"

conda install -c menpo ffmpeg=2.7.0

conda install -c menpo ffmpeg=2.7.0

Dowload Opencv 3.1.0"

下载 Opencv 3.1.0"

wget https://github.com/Itseez/opencv/archive/3.1.0.zipunzip 3.1.0.zip

wget https://github.com/Itseez/opencv/archive/3.1.0.zip解压 3.1.0.zip

mkdir build

mkdir 构建

cd build

光盘构建

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=RELEASE -DCMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") -DPYTHON3_EXECUTABLE=$(which 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())") ..

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_EITHON_OFFS=OFF DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") -DPYTHON3_EXECUTABLE=$(which python3.5) -DPYTHON3_INCLUDE_DIR=$(python3 -ctsys) import get_python_inc; print(get_python_inc())") -DPYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") ..

make

制作

sudo make install

须藤制作安装