FFMPEG with OpenCV
FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video. It is also mainly used in other program to manipulate video for its library components:
- libavcodec is a library containing all the FFmpeg audio/video encoders and decoders.
- libavformat is a library containing demuxers and muxers for audio/video container formats.
- libavutil is a helper library containing routines common to different parts of FFmpeg.
- libpostproc is a library containing video postprocessing routines.
- libswscale is a library containing video image scaling routines.
- libavfilter is the substitute for vhook which allows the video to be modified or examined between the decoder and the encoder.
Linux distribution
You should normaly install the libavXXX-dev packages and libswscale-dev package.
- In Ubuntu 8.10 all the necessary ffmpeg files can be installed with:
sudo apt-get install libavformat-dev libavcodec-dev libavfilter-dev libswscale-dev
However some problem could occurs if you want the unstripped codec (codec remove from the distribution due to some legal issue), See the next part
OpenCV 2.0 + Unstripped FFMPEG
Under ubuntu 9.04 a conflict exist between the unstripped and the dev package of ffmpeg. If you want to use OpenCV 2.0 with the latest codec (Xvid, x264, ...) you should follow these steps.
1. Install and compile manually ffmpeg by following this tutorial (add ffmpeg configure option --enable-shared):
http://ubuntuforums.org/showthread.php?t=786095
2. Download code from OpenCV
3. Compil it using cmake.
4. Install it by using sudo make install
Windows
Since version 1.2.x the compilation of FFMPEG components is done internally as a third party library. You should not worry about it.
OpenCV 1.0.0
1. download and install FFMPEG and OpenCV. (I used version1.0.0 of OpenCV)
2. download and install msys and MinGW. Start msys and go to $FFMPEGHOME.
3. ./configure --enable-shared --disable-static --disable-vhook --enable-memalign-hack
4. make;make install
5. open highgui.vcproj in text editor
6. change all 'HAVE_PNG;' to 'HAVE_PNG;HAVE_FFMPEG;' and save. Could alternatively be done from within MSVC but left out for brevity.
7. open highgui.vcproj in MSVC(make sure highgui.sln does not exist)
8. change project properties
a. add include directories: $MSYSHOME\usr\local\include\ffmpeg and $MSYSHOME\usr\local\include <--- for ffmpeg/av*.h and inttypes.h and stdint.h
b. add library directory: $MSYSHOME\usr\local\lib
c. add linker input: avcodec-51.lib avformat-51.lib avutil-49.lib
9. Remove cvcap_vfw.cpp from project
10. Add cvcap_ffmpeg.cpp to project
11. modify cvcap.cpp
add '#ifdef 0' to line 233 and '#endif' to end
12. modify cvcap_ffmpeg.cpp
at lines 494 and 496, change the AVRational declarations to explicit
at line 578, change snprintf to _snprintf(alternatively, '#define snprintf _snprintf')
13. in _highgui.h, nest '#ifndef HAVE_FFMPEG/#endif' around lines 104 and 135-136
14. From http://code.google.com/p/msinttypes/downloads/list, get inttypes.h and stdint.h and put them in $MSYSHOME\usr\local\include
15. build configuration.Now, cvCaptureFromFile can open MPEG-2 streams, while cvCaptureFromAVI still works as expected.