Ubuntu16.04编译ffmpeg4.2.2

简述

Ubuntu16.04下安装ffmpeg很简单,只要使用sudo apt-get install ffmpeg即可。会自动安装ffmpeg,但是这个ffmpeg的版本可能比较旧。使用ffmpge -version可以查看当前系统下所使用ffmpeg的版本号,如:

ffmpeg version 2.8.15-0ubuntu0.16.04.1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.10) 20160609
configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
libavutil      54. 31.100 / 54. 31.100
libavcodec     56. 60.100 / 56. 60.100
libavformat    56. 40.101 / 56. 40.101
libavdevice    56.  4.100 / 56.  4.100
libavfilter     5. 40.101 /  5. 40.101
libavresample   2.  1.  0 /  2.  1.  0
libswscale      3.  1.101 /  3.  1.101
libswresample   1.  2.101 /  1.  2.101
libpostproc    53.  3.100 / 53.  3.100

ffmpeg源码下载

ffmpeg源码可从此处下载。根据版本下载,此处使用的是4.2.2版本。
Ubuntu16.04编译ffmpeg4.2.2_第1张图片
点击中间的“Download Source Code”即可下载tar包到本地。

编译ffmpeg

由于在Ubuntu直接编译,所以这里编译前的configure十分简单,也可以参考上面查询版本号的时候的打印信息对每一想进行enable或disable。
步骤:

  1. 解压并进入tar包;
  2. 创建一个存放编译输出的目录,如MyBuild;
  3. ./configure --enable-shared --prefix=./MyBuild/
  4. make
  5. make install

编译结束后,可以在MyBuild目录下生成bin、include、lib、share 四个目录。如:Ubuntu16.04编译ffmpeg4.2.2_第2张图片
include和lib即可被程序开发所使用。

修改本地的库链接

虽然编译完成了,但是进入bin运行生成的ffmpeg,并不一定可以成功。如遇到错误指出ffmpeg: error while loading shared libraries: libavdevice.so.58: cannot open。这里需要修改一下本地的链接库路径。
参考链接:
https://blog.csdn.net/weixin_43166958/article/details/89335679
https://blog.csdn.net/baidu_38172402/article/details/80875074

步骤:

  1. /usr/local/目录下创建一个ffmpeg4.2.2的目录;
  2. 将上面编译完成的bin和lib目录拷贝至/usr/local/ffmpeg4.2.2目录下;
  3. 在全局环境变量中加入库的路径:sudo vim /etc/profile,在后面加入export PATH="/usr/local/ffmpeg4.2.2/bin:$PATH",路径根据自己的实际路径修改;
  4. 运行source /etc/profile,环境变量在当前终端下即可生效;
  5. 再添加ffmpeg这些库的路径,sudo vim /etc/ld.so.conf,在文件后添加/usr/local/ffmpeg4.2.2/lib,路径根据实际路径修改;
  6. 运行sudo ldconfig
  7. 重新运行ffmpeg即可获得版本信息。

尝试调用ffmpeg的api

编写一个最简单的例子,尝试调用ffmpeg的api,看是否可以正常调用成功。

  1. 创建一个FFmpeg_Learning目录,将前面编译生成的include和lib复制到该目录下;
  2. 创建一个ffmpegConfiguration.c,代码如下:
#include 
#include "include/libavcodec/avcodec.h"
#include "include/libavformat/avformat.h"
#include "include/libavutil/avutil.h"

int main(int argc, char *agrv[])
{
    printf("%s\n", avcodec_configuration());

    return 0;
}

代码十分简单,只是打印编译前的配置信息而已。
3. 编译时记得指定-I、-L参数和-l参数,否则可能会报错。如gcc ffmpegConfiguration.c -o ffmpegConfiguration -I./include/ -L./lib/ -lavcodec
4. 执行后会打印--enable-shared --prefix=./MyBuild/

你可能感兴趣的:(ffmpeg)