CPU版:
目前opencv中做h264编码实际上是向下调用ffmpeg的,而ffmpeg中则又是调用libx264,于是这也就涉及到了libx264,ffmpeg和opencv三者之间的联合编译
一、libx264
我用的系统是centos6.8,系统内默认安装的有libx264的,但是版本比较低,与ffmpeg不兼容,这里需要先卸载掉原先的x264再安装较新的版本,我用的版本是0.148
二、ffmpeg
系统同样默认安装了低版本的ffmpeg,这里也需要提前卸载掉,然后安装新版本,我用的版本是3.1.3
configuration: --prefix=/home/softwares/ffmpeg-3.1.3 --disable-yasm --enable-shared --enable-pic --enable-vaapi --enable-gpl --enable-libx264 --extra-cflags=-I/usr/local/x264/include --extra-ldflags=-L/usr/local/x264/lib
这个--enable-libx264很重要!!!
三、opencv
opencv我用的版本是2.4.13,别的版本(差的不多的话)应该都是好用的
configuration:
cmake -D CMAKE_BUILD_TYPE=release -D CMAKE_INSTALL_PREFIX=/home/softwares/opencv-2.4.13 -D WITH_CUDA=ON -D WITH_NVCUVID=ON ..
我们一般在调用VideoWriter的时候,都会用X264的FOURCC
writer.open("output_cpu.mp4", CV_FOURCC('X', '2', '6', '4'), FPS, frame.size());
但是会有如下错误
[libx264 @ 0x8d6220] broken ffmpeg default settings detected
[libx264 @ 0x8d6220] use an encoding preset (e.g. -vpre medium)
[libx264 @ 0x8d6220] preset usage: -vpre <speed> -vpre <profile>
[libx264 @ 0x8d6220] speed presets are listed in x264 --help
[libx264 @ 0x8d6220] profile is optional; x264 defaults to high
去网上搜索相关问题的话,基本给出的答案是这样的,在x264中源码如下
/* Detect default ffmpeg settings and terminate with an error. */
{
int score = 0;
score += h->param.analyse.i_me_range == 0;
score += h->param.rc.i_qp_step == 3;
score += h->param.i_keyint_max == 12;
score += h->param.rc.i_qp_min == 2;
score += h->param.rc.i_qp_max == 31;
score += h->param.rc.f_qcompress == 0.5;
score += fabs(h->param.rc.f_ip_factor - 1.25) < 0.01;
score += fabs(h->param.rc.f_pb_factor - 1.25) < 0.01;
score += h->param.analyse.inter == 0 && h->param.analyse.i_subpel_refine == 8;
if( score >= 5 )
{
x264_log( h, X264_LOG_ERROR, "bro