FFmpeg与OpenCV的源码编译
本示例在centos7下编译ffmpeg2.8.0及opencv3.2.0+opencv_contrib
FFmpeg的源码编译
安装依赖包
1 | yum install autoconf automake bzip2 cmake freetype-devel gcc gcc-c++ git libtool make mercurial pkgconfig zlib-devel |
安装可选扩展库
将可选扩展库下载于如下文件夹
1 | mkdir ~/ffmpeg_sources |
####NASM
An assembler used by some libraries. Highly recommended or your resulting build may be very slow.
1 | cd ~/ffmpeg_sources |
####Yasm
An assembler used by some libraries. Highly recommended or your resulting build may be very slow.
1 | cd ~/ffmpeg_sources |
libx264
H.264 video encoder. See the H.264 Encoding Guide for more information and usage examples.
Requires ffmpeg
to be configured with --enable-gpl
--enable-libx264
.
1 | cd ~/ffmpeg_sources |
Warning: If you get Found no assembler. Minimum version is nasm-2.13
or similar after running ./configure
then the outdated nasm package from the repo is installed. Run yum remove nasm && hash -r
and x264 will then use your newly compiled nasm instead. Ensure environment is able to resolve path to nasm binary.
libx265
H.265/HEVC video encoder. See the H.265 Encoding Guide for more information and usage examples.
Requires ffmpeg
to be configured with --enable-gpl
--enable-libx265
.
1 | cd ~/ffmpeg_sources |
libfdk_aac
AAC audio encoder. See the AAC Audio Encoding Guide for more information and usage examples.
Requires ffmpeg
to be configured with --enable-libfdk_aac
(and --enable-nonfree
if you also included --enable-gpl
).
1 | cd ~/ffmpeg_sources |
libmp3lame
MP3 audio encoder.
Requires ffmpeg
to be configured with --enable-libmp3lame
.
1 | cd ~/ffmpeg_sources |
libopus
Opus audio decoder and encoder.
Requires ffmpeg
to be configured with --enable-libopus
.
1 | cd ~/ffmpeg_sources |
libogg
Ogg bitstream library. Required by libtheora and libvorbis.
1 | cd ~/ffmpeg_sources |
libvorbis
Vorbis audio encoder. Requires libogg.
Requires ffmpeg
to be configured with --enable-libvorbis
.
1 | cd ~/ffmpeg_sources |
libvpx
VP8/VP9 video encoder and decoder. See the VP9 Video Encoding Guide for more information and usage examples.
Requires ffmpeg
to be configured with --enable-libvpx
.
1 | cd ~/ffmpeg_sources |
FFmpeg
1 | cd ~/ffmpeg_sources |
可直接跳过扩展库的安装,去除–enable 后的可选包,直接用–enable-shared及以上的命令即可
opencv的源码编译
安装依赖包
1 | yum install gtk2-devel |
gtk2-devel不安装,之后会报错无法显示窗口 (对imshow函数的支持)
注意事项
将ffmpeg的包放入环境变量
1 | gedit ~/.bashrc |
更新环境变量source ~/.bashrc
如未进行上述环境变量的修改,可按以下步骤代替(推荐直接修改环境变量)
OpenCV的CMakeLists.txt中关于检测是否有安装ffmpeg时,默认是到/usr 下去检测相应的头文件是否存在的,所以OpenCV使用ffmpeg时需要将相应的库和头文件放到/usr下(由于需要root权限, $HOME需要根据情况修改)
1
2
3 cp -a $HOME/ffmpeg_build/lib/* /usr/lib/*
cp -a $HOME/ffmpeg_build/include/* /usr/include/*
添加contrib模块的编译
1 | mkdir release |
读取视频测试
- DisplayVideo.cpp
1 |
|
- CMakeLists.txt
1 | cmake_minimum_required(VERSION 2.8) |
- 编译及测试
1 | mkdir build |
###opencv的卸载
1)进入opencv的源代码文件夹下的release(这是你在安装opencv时候自己命名的,cmake时候所在的目录)
2)依次执行下面的代码
1 | make uninstall |
cmake时额外文件放置位置
ippicv_linux_20151201.tgz放在
opencv/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e
protobuf-cpp-3.1.0.tar.gz放在opencv_contrib/modules/dnn/.download/bd5e3eed635a8d32e2b99658633815ef/v3.1.0
vgg_generated_48.i放在opencv_contrib/modules/xfeatures2d/cmake/.download/e8d0dcd54d1bcfdc29203d011a797179
vgg_generated_64.i放在opencv_contrib/modules/xfeatures2d/cmake/.download/7126a5d9a8884ebca5aea5d63d677225
vgg_generated_80.i放在opencv_contrib/modules/xfeatures2d/cmake/.download/7cd47228edec52b6d82f46511af325c5
vgg_generated_120.i放在opencv_contrib/modules/xfeatures2d/cmake/.download/151805e03568c9f490a5e3a872777b75boostdesc_binboost_064.i放在opencv_contrib/modules/xfeatures2d/cmake/.download/202e1b3e9fec871b04da31f7f016679f
boostdesc_binboost_128.i放在opencv_contrib/modules/xfeatures2d/cmake/.download/98ea99d399965c03d555cef3ea502a0b
boostdesc_binboost_256.i放在opencv_contrib/modules/xfeatures2d/cmake/.download/e6dcfa9f647779eb1ce446a8d759b6ea
boostdesc_bgm.i放在opencv_contrib/modules/xfeatures2d/cmake/.download/0ea90e7a8f3f7876d450e4149c97c74f
boostdesc_bgm_bi.i放在opencv_contrib/modules/xfeatures2d/cmake/.download/232c966b13651bd0e46a1497b0852191
boostdesc_bgm_hd.i 放在opencv_contrib/modules/xfeatures2d/cmake/.download/324426a24fa56ad9c5b8e3e0b3e5303e
boostdesc_lbgm.i放在opencv_contrib/modules/xfeatures2d/cmake/.download/0ae0675534aa318d9668f2a179c2a05
其他编译实验问题
在centos6.5上编译opencv2.4.11及2.4.9时,匹配ffmpeg的2.8.0,2.3.1,2.6.4等版本时均编译失败。单独编译ffmpeg源码和opencv源码时不存在问题,怀疑是opencv低版本对ffmpeg源码中存在不支持的问题。