Search code examples
macosopencvffmpegh.264enthought

Read h.264 video frames with opencv in python Enthough (mac Yosemite)


I'm using the Enthought distribution (Canopy) to do some data analysis and computer vision in the IPython notebook. I want to read the frames of several .avi files that use the h.264 codec and make some annotations on those images.

if you're using the Canopy distribution, you know that you can install opencv through the package manager (just launch the Canopy application, click on package manager, search for opencv and install the package). The issue though is that the following code

import cv2
f = "/Volumes/DATA/temp.avi"
cap = cv2.VideoCapture(f)

flag,frame = cap.read()

print flag,frame

always returns (None,None) because opencv can't read the video. So it seems like ffmpeg is not enabled by default in the Enthought package manager.

I've been losing a lot of time on this problem, so I'll post the solution below. Hopefully it will help some other folks out there!


Solution

  • Follow those steps (partially from this source):

    1) install mp3lame

    curl -L -o lame-3.99.5.tar.gz http://sourceforge.net/projects/lame/files/lame/3.99/lame-3.99.5.tar.gz/download
    tar xzvf lame-3.99.5.tar.gz
    cd lame-3.99.5
    ./configure --disable-dependency-tracking CFLAGS="-arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64"
    make
    sudo make install
    cd ..
    

    2) install faac

    curl -L -o faac-1.28.tar.gz http://sourceforge.net/projects/faac/files/faac-src/faac-1.28/faac-1.28.tar.gz/download
    tar xzvf faac-1.28.tar.gz
    cd faac-1.28
    ./configure --disable-dependency-tracking CFLAGS="-arch x86_64" LDFLAGS="-arch x86_64"
    make
    sudo make install
    cd ..
    

    3) install faad

    curl -L -o faad2-2.7.tar.gz http://sourceforge.net/projects/faac/files/faad2-src/faad2-2.7/faad2-2.7.tar.gz/download
    tar xvzf faad2-2.7.tar.gz
    cd faad2-2.7
    ./configure --disable-dependency-tracking CFLAGS="-arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64"
    make
    sudo make install
    cd ..
    

    4) install ffmpeg

    curl -O http://ffmpeg.org/releases/ffmpeg-0.11.5.tar.gz
    tar xzvf ffmpeg-0.11.5.tar.gz
    cd ffmpeg-0.11.5
    ./configure --enable-libmp3lame --enable-libfaac --enable-nonfree --enable-shared --enable-pic --disable-mmx --arch=x86_64
    make
    sudo make install
    cd ..
    

    5) download opencv 2.4 from http://opencv.org/downloads.html and unzip the archive somewhere on your hard drive

    6) Launch the Canopy terminal (start the canopy application > Tools > Canopy Terminal)

    7) navigate to your opencv folder and edit the modules/highgui/CMakeLists.txt file and add those lines just before "if(HAVE_FFMPEG)":

    if(APPLE)
      list(APPEND HIGHGUI_LIBRARIES ${BZIP2_LIBRARIES} -lmp3lame -lfaac -lbz2)
    endif(APPLE)
    

    Otherwise the compilation process will fail at 34%.

    8) then run (modified from here)

    mkdir build
    cd build
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D PYTHON_EXECUTABLE=~/Library/Enthought/Canopy_64bit/User/bin/python -D PYTHON_PACKAGES_PATH=~/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/ -D PYTHON_LIBRARY=~/Library/Enthought/Canopy_64bit/User/lib/libpython2.7.dylib -D INSTALL_PYTHON_EXAMPLES=ON WITH_QUICKTIME=ON -D WITH_FFMPEG=ON -D WITH_AVFOUNDATION=ON ..
    make -j8 
    sudo make install
    

    IMPORTANT: make sure that the paths on the cmake line match those on your system!

    That's it. It's a lot of steps, but at the end of it you'll have opencv working within your canopy distribution and you'll be able to read h.264 .avi videos!