无法使用OpenCV打开视频 [英] Can't open video using opencv

查看:637
本文介绍了无法使用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!

我的vlcmplayer可以播放此视频,但opencv无法播放.

我已经安装了x264libx264-142编解码器软件包. (使用sudo apt-get install)

我的ubuntu版本是14.04 trusty.

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

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

解决方案

这是编解码器问题

我用ffmpeg将该mp4文件转换为avi文件.然后,上面的opencv代码可以很好地播放该avi文件.

因此,我确定这是一个编解码器问题.

(然后,我使用ffmpeg将该mp4文件转换为另一个mp4文件,认为ffmpeg有助于将原始的不可读.mp4编解码器转换为可读的.mp4编解码器,但生成的.mp4文件最终损坏了.这个事实可能与这个问题有关,也可能与这个问题无关,以防万一,有人需要此信息.)

问题的答案-重建FFmpeg,然后重建Opencv

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

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

(1)构建ffmpeg

  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支持(在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)

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

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

    您可以根据需要更改这些选项.关键选项只有最后一个-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

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

我尝试过但没用的方法

  1. cmake opencv时尝试添加WITH_UNICAP=ON WITH_V4L=ON.但是根本没用.
  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/下的库更改为库.但是甚至无法成功构建.

    我grep AVC: 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

    尝试安装一些东西和一些东西.但这完全没用.

相关问题

我已经在网上搜索了很多类似的问题,但是没有一个有解决方案!

以下是我认为与我相同的问题.

  • 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

  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-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)

  5. make -j5 (under ffmpeg folder)
  6. sudo make install

(2) Build 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,-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

  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 mp4 file well!

Methods I tried but didn't work

  1. Try add WITH_UNICAP=ON WITH_V4L=ON when cmake opencv. 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 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.

  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.

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打开视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆