FFmpeg与OpenCV的源码编译

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
2
3
4
5
6
7
8
cd ~/ffmpeg_sources
curl -O -L http://www.nasm.us/pub/nasm/releasebuilds/2.13.02/nasm-2.13.02.tar.bz2
tar xjvf nasm-2.13.02.tar.bz2
cd nasm-2.13.02
./autogen.sh
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make
make install

####Yasm

An assembler used by some libraries. Highly recommended or your resulting build may be very slow.

1
2
3
4
5
6
7
cd ~/ffmpeg_sources
curl -O -L http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xzvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make
make install

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
2
3
4
5
6
cd ~/ffmpeg_sources
git clone --depth 1 http://git.videolan.org/git/x264
cd x264
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static
make
make install

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
2
3
4
5
6
cd ~/ffmpeg_sources
hg clone https://bitbucket.org/multicoreware/x265
cd ~/ffmpeg_sources/x265/build/linux
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source
make
make install

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
2
3
4
5
6
7
cd ~/ffmpeg_sources
git clone --depth 1 https://github.com/mstorsjo/fdk-aac
cd fdk-aac
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install

libmp3lame

MP3 audio encoder.

Requires ffmpeg to be configured with --enable-libmp3lame.

1
2
3
4
5
6
7
cd ~/ffmpeg_sources
curl -O -L http://downloads.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz
tar xzvf lame-3.100.tar.gz
cd lame-3.100
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --disable-shared --enable-nasm
make
make install

libopus

Opus audio decoder and encoder.

Requires ffmpeg to be configured with --enable-libopus.

1
2
3
4
5
6
7
cd ~/ffmpeg_sources
curl -O -L https://archive.mozilla.org/pub/opus/opus-1.2.1.tar.gz
tar xzvf opus-1.2.1.tar.gz
cd opus-1.2.1
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install

libogg

Ogg bitstream library. Required by libtheora and libvorbis.

1
2
3
4
5
6
7
cd ~/ffmpeg_sources
curl -O -L http://downloads.xiph.org/releases/ogg/libogg-1.3.3.tar.gz
tar xzvf libogg-1.3.3.tar.gz
cd libogg-1.3.3
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install

libvorbis

Vorbis audio encoder. Requires libogg.

Requires ffmpeg to be configured with --enable-libvorbis.

1
2
3
4
5
6
7
cd ~/ffmpeg_sources
curl -O -L http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.5.tar.gz
tar xzvf libvorbis-1.3.5.tar.gz
cd libvorbis-1.3.5
./configure --prefix="$HOME/ffmpeg_build" --with-ogg="$HOME/ffmpeg_build" --disable-shared
make
make install

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
2
3
4
5
6
cd ~/ffmpeg_sources
git clone --depth 1 https://chromium.googlesource.com/webm/libvpx.git
cd libvpx
./configure --prefix="$HOME/ffmpeg_build" --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=yasm
make
make install

FFmpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
cd ~/ffmpeg_sources
curl -O -L https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
tar xjvf ffmpeg-snapshot.tar.bz2
cd ffmpeg
PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig"
./configure \
--prefix="$HOME/ffmpeg_build" \
--pkg-config-flags="--static" \
--extra-cflags="-I$HOME/ffmpeg_build/include" \
--extra-ldflags="-L$HOME/ffmpeg_build/lib" \
--extra-libs=-lpthread \
--extra-libs=-lm \
--bindir="$HOME/bin" \
--enable-shared \
--enable-gpl \
--enable-libfdk_aac \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libopus \
--enable-libvorbis \
--enable-libvpx \
--enable-libx264 \
--enable-libx265 \
--enable-nonfree
make
make install
hash -r

可直接跳过扩展库的安装,去除–enable 后的可选包,直接用–enable-shared及以上的命令即可

opencv的源码编译

安装依赖包

1
yum install gtk2-devel

gtk2-devel不安装,之后会报错无法显示窗口 (对imshow函数的支持)

注意事项

将ffmpeg的包放入环境变量

1
2
3
4
5
gedit ~/.bashrc
export FFMPEG_PATH=<path-to-ffmpeg_build>
export C_INCLUDE_PATH=$FFMPEG_PATH/include:$C_INCLUDE_PATH
export PKG_CONFIG_PATH=$FFMPEG_PATH/lib/pkgconfig:$PKG_CONFIG_PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$FFMPEG_PATH/lib

更新环境变量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
2
3
4
5
mkdir release
cd release
cmake -D OPENCV_EXTRA_MODULES_PATH=<>/opencv_contrib-3.2.0/modules/ -DBUILD_SHARED_LIBS=NO ..
make
make install

读取视频测试

  • DisplayVideo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 ){
printf("usage: DisplayImage.out <Video_Path>\n");
return -1;
}
VideoCapture capture(argv[1]);//打开视频文件
Mat frame;
if (!capture.isOpened()){
printf("open capture fail!\n");
return -1;
}
namedWindow("Display Video", 0 );
int auto_play_flag = 0;
int decay_time;
auto_play_flag? decay_time=1: decay_time=0;
while (1){
capture >> frame;//读取当前帧到frame矩阵中
if (frame.empty()){
capture.set(CV_CAP_PROP_POS_FRAMES, 0);
return -1;
}
imshow("Display Video", frame);
int key = waitKey(decay_time);
if (key == 27){
break;
}
if (key == 32){
auto_play_flag = !auto_play_flag;
auto_play_flag ? decay_time = 1 : decay_time = 0;
continue;
}
}
return 0;
}
  • CMakeLists.txt
1
2
3
4
5
cmake_minimum_required(VERSION 2.8)
project( DisplayVideo )
find_package( OpenCV REQUIRED )
add_executable( DisplayVideo DisplayVideo.cpp )
target_link_libraries( DisplayVideo ${OpenCV_LIBS} )
  • 编译及测试
1
2
3
4
5
mkdir build
cd build
cmake ..
make
./DisplayVideo test.avi

###opencv的卸载

1)进入opencv的源代码文件夹下的release(这是你在安装opencv时候自己命名的,cmake时候所在的目录)

2)依次执行下面的代码

1
2
3
4
make uninstall
cd ..
rm -rf release
sudo rm -rf /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*

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/151805e03568c9f490a5e3a872777b75

boostdesc_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源码中存在不支持的问题。