11 June 2010

OpenCV 2.1.0 and FFMPEG compilation solution

I had been recently struggling to compile OpenCV 2.1.0 with FFMPEG support on GNU/Linux. The problem was that the OpenCV CMake script fails to detect the important component of ffmpeg it needs; like avcodec, swscale, avformat. The more idiotic thing is that if one uses ccmake, the log is not visible. It is only after building that one sees that ffmpeg support is not there. One has to use cmake. Anyways, after doing cmake ../OpenCV-2.1.0, noted that config log said “ffmpeg 0″ and also put zeros against the above mentioned components of ffmpeg. Well, I knew that ffmpeg existed in all its components. I had also made a directory called ffmpeg in /usr/local/include and made symbolic links for the respective header files. Now it was a clear case of cmake being unable to find these, either because of poor CMakeLists.txt or the cmake itself. At this point, I did not want to dig into the innards of CMake; unlike autoconf it has an annoying tendency to hide information; for example where it searches for ffmpeg and such things. Such blackbox approach suits Ubuntu users no doubt. I tried parsing through the CMakeLists.txt and could not find anything that I could rectify. However, I did know that CMakeCache.txt contained the configured values. After that it was a matter of putting manually the include, lib paths and the linker flags in this for each of the ffmpeg components. After running cmake again, I could immediately see that ffmpeg components were recognised and ffmpeg was turned to 1. make happily started building cvcap_ffmpeg which meant that ffmpeg was actually being used. However, the build process croaked at 99% when trying to link to the actual libavcodec/libavformat and libswscale. Clearly, the linker links I had passed as a guess had not worked. Now, I did cmake again and passed -L/usr/local/lib and -Wl, -rpath,/usr/local/lib. Voila! the compilation was complete. I build a test program to read and write videos. All worked ok. This was a rough sketch of the compilation solution. I will post the details of the CMakeCache.txt in case someone is interested.

7 comments:

Sean said...

Hi Krishnan, Thank you for your post. It has shed a lot of light on why OpenCV is not detecting FFMPEG. I was wondering if you would be kind enough to post the CMakeCache.txt that you used in your solution? At the moment, I'm struggling to piece together the exact syntax for the missing FFMPEG components in this file. Many thanks in advance for your help. Cheers, Sean

Josh said...

Krishnan, your CMake changes would be helpful. Could you please post specifics. Thanks, Josh.

Josh said...

Turns out, you just need to install pkg-config to get it to work. Pretty lame.

dev said...

after doing a
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/

and adding the following line in cmake

if(NOT PKG_CONFIG_FOUND)
INCLUDE(FindPkgConfig)
endif()

(seems the problem was with the macro CHECK_MODULE, since it was not detecting the packages) the configuration was done with ffmpeg support. i also added xine support. but still am not able to read videos. cvCaptureFromAVI and cvCaptureFromFile() both return NULL. any clue why it would be?

Krishnan said...

Hi dev,
thanks for the tip. I modified the frikking cmakecache manually. This seems more logical and easier.

Anonymous said...

Hi Krishnan.
I found this topic and I'm trying to resurrect it after two years, because I need to compile OpenCV 2.1 linked to ffmpeg libs for my bachelor thesis.
I know that the OpenCV 2.1 version is very old, and my aim is to use at last the latest version, but first I need this one because the C++ program which I'm developing is based on a two years old code which uses OpenCV 2.1. From OpenCV 2.2 ongoing, the includes are different, and I've to modify the program in the future, thus now I want to try with version 2.1. Can you please exactly point me which modifications to the cmake files have you made to link it with ffmpeg libs?
Thanks a lot!
Stefano

Krishnan said...

Hi stefano,
Can you please look at dev's solution above? It is cleaner than editing cmakecache files.