Wednesday, November 30, 2011

Installing OpenCV 2.3.1 with FFmpeg on 64-bit Ubuntu

There are many good tutorials out there which describe the installation procedure of OpenCV with FFmpeg. However few of them are comprehensive and even fewer explain the proper setup procedure for a 64-bit machine. Below are the steps that I have been following while setting up OpenCV with FFmpeg on a couple of new 64-bit systems in our lab (with Ubuntu 11.10). OK, here we go:

Create a directory where the installation files will be downloaded:

$ mkdir ~/Tools
$ cd ~/Tools

Update your package index:

sudo apt-get update

Install the dependencies for x264 and FFmpeg:

$ sudo apt-get install build-essential checkinstall git cmake libfaac-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev libvorbis-dev libx11-dev libxfixes-dev libxvidcore-dev texi2html yasm zlib1g-dev

Get gstreamer:

$ 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

Get yasm:

$ sudo apt-get install yasm

Download x264 and install it:

$ git clone git://git.videolan.org/x264
$ cd x264
$ ./configure --enable-static --enable-shared
$ make
$ sudo make install
$ cd ..

Get the libs necessary for FFmpeg installation:

$ sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev

Download FFmpeg and install it:

$ git clone --depth 1 git://git.videolan.org/ffmpeg
$ cd ffmpeg
$ ./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb \
 --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 \
 --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab --enable-shared
$ make
$ sudo make install
$ cd ..

Install gtk:

$ sudo apt-get install libgtk2.0-0 libgtk2.0-dev

Install libjpeg:

$ sudo apt-get install libjpeg62 libjpeg62-dev

Install v4l:

Get the latest version of v4l from here. The version used in this installation is 0.8.5

$ wget http://www.linuxtv.org/downloads/v4l-utils/v4l-utils-0.8.5.tar.bz2
$ tar -xvf v4l-utils-0.8.5.tar.bz2 
$ cd v4l-utils-0.8.5
$ make
$ sudo make install
$ cd ..

Get the python libraries:

$ sudo apt-get install python-numpy python-sphinx python-dev

Finally install OpenCV:

Get the latest version from here. The version used in this installation is 2.3.1

$ wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.3.1/OpenCV-2.3.1a.tar.bz2
$ tar -xvf OpenCV-2.3.1a.tar.bz2 
$ cd OpenCV-2.3.1
$ mkdir Build
$ cd Build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON ..
$ make
$ sudo make install

If you want to see all the options while building OpenCV, use "ccmake .." instead of the above mentioned cmake command. Done! Hope that helped.

30 comments:

  1. Hi Vinay, thanks for this useful guide.

    Cheers,

    Camilo

    ReplyDelete
  2. hey vinay..nice post..
    but can u help me out in installing openCV 2.3.1..i would b using MS Visual Studio 2008 Express Edition

    if u can...please mail me the steps..or any link wer i can find the steps to install..
    my email address is: yashreads@yahoo.com
    thanks.. :)

    ReplyDelete
    Replies
    1. Sorry, I have no experience with Windows based installations.

      Delete
    2. I used this video.
      http://www.youtube.com/watch?v=kZvjTTK9zTw

      Delete
  3. This is an awesome post. Thank you so much. This works perfect!!!

    installing opencv is not a joke at all if you don't know what to do. This post explains what and how to install very straight forward.

    5 stars on this post!!!

    ReplyDelete
  4. Hi,

    I have installed it following your instruction. Nevertheless, right now, my java program cannot recognize it. Do you know how to configure opencv libraries with my java program?? Thanks.

    ReplyDelete
    Replies
    1. Sorry, I have no experience with OpenCV + Java. I have always only worked on OpenCV with C++.

      Delete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Very useful post Vinay. Thanks.

    -Ravi

    ReplyDelete
  7. I have ubuntu 10.10….
    I want ffmpeg with Opencv2.3.1 support….
    I did the installation many times before….with building ffmpeg manually as you said….
    Its working fine also its showing me ffmpeg support ON at the time of configuring OpenCv 2.3.1…
    But when I use CV_FOURCC(‘F’,'L’,'V’,’1′) to create .flv file through cvCreateVideoWriter I am getting this error [flv @ 0x9bf5000] Tag FLV1/0x31564c46 incompatible with output codec id ’22′….
    Is their any thing wrong I had done while installation….

    ReplyDelete
  8. An easy way...

    https://launchpad.net/~gijzelaar/+archive/opencv2.3

    ReplyDelete
  9. Ffmpeg has to be version of 7.11 or earlier because OpenCV hasn't managed to keep up with the update of FFMPEG. If it's later than 7.11,then cap_ffmpeg_impl.cpp may give error while building at 33%.

    ReplyDelete
  10. More over, if one wants include tbb and enable other options in cmake I think it's better to build by cmake from the cmake-gui rather than the terminal.

    ReplyDelete
    Replies
    1. That's true. I edited the post to mention this. Thanks for the comment.

      Delete
  11. Mr. Vinay, will the steps be the same for 32bit linux?

    ReplyDelete
    Replies
    1. No. Some steps are different. There are several other excellent tutorials out there for a 32-bit build.

      Delete
  12. + BUILD_NEW_PYTHON_SUPPORT instead of BUILD_PYTHON_SUPPORT
    + opencv 2.3.1 requires ffmpeg 0.8.x if you do it according to your guide you download 1.0

    ReplyDelete
    Replies
    1. Thanks, I edited the post to reflect this.

      Delete
  13. In case you want to make a package out of your OpenCV compilation (and maybe hand it to other ppl), replace sudo make install with this:
    sudo checkinstall --pkgname=opencv-svn --pkgversion="2:$(date +%Y%m%d%H%M)-opencv" --default --backup=no

    ReplyDelete
    Replies
    1. Thanks for the info. I did not know about this.

      Delete
  14. Hi Vinay...........Thanks for this nice and useful post.
    I am getting error at 14% after using make command.
    I dont know what is the problem.....

    pls help ...

    thanks

    Avinash

    ReplyDelete
  15. As mentioned in one of the replies, the instructions download the latest version of FFmpeg which is not supported in OpenCV 2.3.1.

    SOLUTION: Download FFmpeg 0.10 (from http://ffmpeg.org/releases/), untar it and continue from line 2 of the ffmpeg section (ignore the git-related line).

    I'd add one more line to this instruction set as a sanity check. Run the OpenCV test suite: ./opencv_test_core (located in OpenCV-2.3.1/Build/bin)

    ReplyDelete
  16. About the FFmpeg support : following the instructions "git clone --depth 1 git://git.videolan.org/ffmpeg" generate an error when i build openCV. (about KGD post : the last version when i tried was the 0.10.3, so i guess 0.10 won't work either)
    An anonymous said "FFmpeg has to be version of 7.11 or earlier because OpenCV hasn't managed to keep up with the update of FFMPEG"
    So i randomly tried with 0.6.5, then the build is ok but not the bin/opencv_test_xxx which failed on many different points, and so in my owns programs the VideoCapture failed at the opening.
    Do you know a version of FFmpeg that work (for ubuntu 12.04 64b) ?
    Thanks in advance.

    ReplyDelete
    Replies
    1. I just installed it on 12.04 64bit with the provided instructions,

      just before building and deploying OpenCV after the instruction above circa june1st2012, I also installed the openCL cuda libs and sdk from
      http://developer.nvidia.com/cuda-downloads
      currently 4.2,
      and followed the instructions here: http://berenger.eu/blog/2011/11/25/gpu-install-cuda-opencl-on-ubuntu/
      and installed tbb with
      sudo apt-get install libtbb-dev
      I used ccmake to set the cuda and tbb options on. I just tested facedetect and it works in real time (the moment a face in in view) I'm stoked.

      Delete
  17. Thanks so much for such an informative and well maintained site Vinay, you bring honor to the craft.

    ReplyDelete
  18. if you are using CUDA and nvidia current you need to create a link : su -c 'ln -s /usr/lib/nvidia-current/libnvcuvid.so /usr/lib/libnvcuvid.so && ln -s /usr/lib/nvidia-current/libnvcuvid.so.1 /usr/lib/libnvcuvid.so.1'

    Regards

    ReplyDelete
  19. Haan..Finally stumbled upon it. FFMpeg is a Bitch!

    -Jigar

    ReplyDelete
  20. To install OpenCV using the terminal on Ubuntu:
    $ su -
    # apt-get update
    # apt-get install build-essential
    # apt-get install libavformat-dev
    # apt-get install x264 v4l-utils ffmpeg
    # apt-get install libcv2.3 libcvaux2.3 libhighgui2.3 python-opencv opencv-doc libcv-dev libcvaux-dev libhighgui-dev

    http://namhuy.net/1205/how-to-install-opencv-on-ubuntu.html

    ReplyDelete