Install OpenCV 2.4.2 with ffmpeg support in Mac OS X 10.8

In order to install OpenCV 2.4.2 with ffmpeg support we have to first install lamemp3, faac, faad and of course ffmpeg. I’ve added a new script in my installation-scripts repository in Github but I’m going to write down here installing one by one all the libraries.

Also remember that you need to install the “Command Line Tools” in order to compile from the command line if you are using Xcode 4.4 or above. Have a look at “Can’t configure with cmake in Mac OS X 10.8” before continuing the installation process.

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

faac (notice that only compiles for x86_64):

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

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

ffmpeg:

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

OpenCV 2.4.2:

curl -L -o OpenCV-2.4.2.tar.bz2 http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.2/OpenCV-2.4.2.tar.bz2/download
tar xvzf OpenCV-2.4.2.tar.bz2
cd OpenCV-2.4.2

If we configure and compile OpenCV 2.4.2 as it comes we will get a linker error at about 34% of the compilation process like this:

Linking CXX shared library ../../lib/libopencv_highgui.dylib
Undefined symbols for architecture x86_64:
  "_faacEncClose", referenced from:
      _Faac_encode_close in libavcodec.a(libfaac.o)
      _Faac_encode_init in libavcodec.a(libfaac.o)
  "_faacEncEncode", referenced from:
      _Faac_encode_frame in libavcodec.a(libfaac.o)
  "_faacEncGetCurrentConfiguration", referenced from:
      _Faac_encode_init in libavcodec.a(libfaac.o)
  "_faacEncGetDecoderSpecificInfo", referenced from:
      _Faac_encode_init in libavcodec.a(libfaac.o)
  "_faacEncOpen", referenced from:
      _Faac_encode_init in libavcodec.a(libfaac.o)
  "_faacEncSetConfiguration", referenced from:
      _Faac_encode_init in libavcodec.a(libfaac.o)
  "_lame_close", referenced from:
      _mp3lame_encode_close in libavcodec.a(libmp3lame.o)
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_encode_buffer", referenced from:
      _mp3lame_encode_frame in libavcodec.a(libmp3lame.o)
  "_lame_encode_buffer_float", referenced from:
      _mp3lame_encode_frame in libavcodec.a(libmp3lame.o)
  "_lame_encode_buffer_int", referenced from:
      _mp3lame_encode_frame in libavcodec.a(libmp3lame.o)
  "_lame_encode_buffer_interleaved", referenced from:
      _mp3lame_encode_frame in libavcodec.a(libmp3lame.o)
  "_lame_encode_flush", referenced from:
      _mp3lame_encode_frame in libavcodec.a(libmp3lame.o)
  "_lame_get_encoder_delay", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_get_framesize", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_init", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_init_params", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_VBR", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_VBR_quality", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_bWriteVbrTag", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_brate", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_disable_reservoir", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_in_samplerate", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_mode", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_num_channels", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_out_samplerate", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_quality", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [lib/libopencv_highgui.2.4.2.dylib] Error 1
make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
make: *** [all] Error 2

To solve this problem we have to change modules/highgui/CMakeLists.txt and add just before if(HAVE_FFMPEG) (on line 158) the following text:

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

Or we could apply this patch to OpenCV 2.4.2:

--- modules/highgui/CMakeLists.txt.old  2012-07-27 15:46:52.000000000 +0200
+++ modules/highgui/CMakeLists.txt      2012-07-27 15:47:05.000000000 +0200
@@ -155,6 +155,10 @@
   list(APPEND HIGHGUI_LIBRARIES m3api)
 endif(HAVE_XIMEA)
 
+if(APPLE)
+  list(APPEND HIGHGUI_LIBRARIES ${BZIP2_LIBRARIES} -lmp3lame -lfaac -lbz2)
+endif(APPLE)
+
 if(HAVE_FFMPEG)
   if(UNIX AND BZIP2_LIBRARIES)
     list(APPEND HIGHGUI_LIBRARIES ${BZIP2_LIBRARIES})

Now we can configure, compile and install it as usual. If you want to know more configuration options of cmake for OpenCV you can have a look at “Install OpenCV 2.3.1a in Mac OS X 10.6“.

mkdir build
cmake ..
make
sudo make install
cd ..

Source: http://blog.yageek.net/2012/06/compile-opencv-241-on-mac-os-x-lion.html

You might also like

Install OpenCV 2.3.1a in Ubuntu 11.10
If you want to install OpenCV 2.1 you can just use apt-get to do the work: sudo apt-get install libcv-dev...
Install OpenSceneGraph 2.9.6 with Collada support in Ubuntu 12.04
Although I usually install OpenSceneGraph 2.8.3 to work with osgART 2.0 RC3 it is also posible to use...
Install OpenCV 2.3.1 in Ubuntu 12.04
OpenCV 2.3.1 is now the last version in repositories so you just need to run: sudo apt-get install...
Install OpenCV 2.4 in Ubuntu 12.04
Installing OpenCV 2.4 from source code as I did with OpenCV 2.3.1a in Ubuntu 10.10 went flawlessly, didn't...
Leave a comment ?

17 Comments.

  1. It helps a lot. Do you know how to utilize ffmpeg writing videos under XCode? I can’t make the VideoWriter work with the OpenCV 2.4.2. I hope you can help me out with this if you know the solution.

    Reply
    • Sorry but I’ve never used ffmpeg to write videos. You should look at the oficial documentation for an example or something like that.

      Reply
  2. FFMPEG on MAC patch outlined above is still required for Open CV 2.4.3 as well.

    Reply
  3. GREAT STUFF!

    Reply
  4. Thanks for the tutorial .. I have successfully installed and have a working simple opencv videocapture application. The capture happens from a file, but I have been unsuccessfully in deploying this application onto a different computer the one without opencv or ffmpeg. More info the question here: http://stackoverflow.com/questions/15299691/opencv-ffmpeg-video-capture-filename-avi-crashes-on-a-non-development-machi

    Reply
    • If it works in your development computer but does not in another computer it should be a problem in the libraries. You should try installing the exact same versions of OpenCV, ffmpeg and Qt in the other computer. I don’t know anything about otool and the deployment proccess of apps with it. Probably it’s a problem in the paths.

      By the way, in your code you resize the image to 604×480 and I’m pretty sure you want to resize it to 640×480.

      Reply
      • Hi enekochan,..Yes, if I install those libraries on the other system it works fine, its only when I try and package an application this happens. I have been going throught the app bundle for the last 8hours to make sure each of the dependency is correctly linked. Looks like a bug in the opencv ffmpeg, or the qt end.

        And yes it was suppose to 640 x 480…thanks for pointing it out.

        Reply
  5. Hey thanks for the tutorial, I’ve been looking for something like this. I’m having a problem when i make faad.

    ld: warning: ignoring file ../common/mp4ff/libmp4ff.a, file was built for archive which is not the architecture being linked (i386): ../common/mp4ff/libmp4ff.a
    Undefined symbols for architecture i386:
    “_main”, referenced from:
    start in crt1.10.6.o
    ld: symbol(s) not found for architecture i386
    collect2: ld returned 1 exit status
    lipo: can’t open input file: /var/tmp//ccJylCwD.out (No such file or directory)
    make[2]: *** [faad] Error 1
    make[1]: *** [all-recursive] Error 1

    Reply
  6. Thanks,

    Still usefull one year later :)

    Reply
  7. Thanks!
    I can’t believe that the more “official” opencv web pages don’t mention any of this. One has to google to find such information.

    You are a life saver man!

    cheers

    Reply
  8. I have changed modules/highgui/CMakeLists.txt. But I have a problem.
    ld: library not found for -lmp3lame
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make[2]: *** [lib/libopencv_highgui.2.9.0.dylib] Error 1
    make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
    make[1]: *** Waiting for unfinished jobs….
    Linking CXX shared library ../../lib/libopencv_photo.dylib

    Can you give me a favour?

    Reply
    • Look at “library not found for -lmp3lame”. It seems you didn’t install correctly the mp3lame library.

      Reply
      • I changed “-lmp3lame” into “/usr/local/lib/libmp3lame.dylib”. Then it can be made.

        Reply
        • Hey Sophia,

          I am also have the same issue,

          “Linking CXX shared library ../../lib/libopencv_highgui.dylib
          ld: library not found for -lmp3lame
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
          make[2]: *** [lib/libopencv_highgui.2.4.6.dylib] Error 1
          make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
          make: *** [all] Error 2″

          I couldn’t understand what you exactly did, can you please elaborate? Where do you find “-lmp3lame”? and How do you change it into “/usr/local/lib/libmp3lame.dylib” ?

          Your help is much appreciated.

          Thank you,
          Surya.

          Reply

Leave a Comment

Trackbacks and Pingbacks: