王老头

导航

[转]opencv无法打开video(.mp4格式)

原文链接:https://stackoverflow.com/questions/31040746/cant-open-video-using-opencv

最近做实验的时候,也将一段手机拍摄的视频按帧读取保存为图片,视频格式为.mp4。ubuntu系统中python2.7和python3.5都有,一开始使用3.5版本读取视频,总是返回false,检查路径没问题,代码没问题,opencv也安装正确,能够正常读取和显示图片,但是就是不能读取.mp4的视频。于是我换回python2.7版本后,居然又可以了(python2.7版本也安装了opencv的)。查阅资料中,发现可能是.mp4格式编码的问题,按道理读取.avi格式的视频应该是都没有问题,但是.mp4格式的会有编码格式不同。具体情况如下:

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.

 

Solutions:

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!

posted on 2019-02-24 20:06  王老头  阅读(3347)  评论(0)    收藏  举报