ffmpeg源码编译环境搭建

ffmpeg是视频开发最常用到的开源软件,ffmpeg功能强大,用途广泛,提供几乎所有你能够想到的与视频开发相关的操作,许多商业软件都以ffmpeg为基础进行开发定制。

FFmpeg: FFmpeg is a free and open-source project consisting of a vast software suite of libraries and programs for handling video, audio, and other multimedia files and streams. At its core is the FFmpeg program itself, designed for command-line-based processing of video and audio files, and widely used for format transcoding, basic editing (trimming and concatenation), video scaling, video post-production effects, and standards compliance (SMPTE, ITU) . 摘自wikipedia

ffmpeg功能庞大,向开发者提供了丰富的函数库调用,常用的函数库包括:

  • libavcodec - 各种格式的音视频编解码
  • libavformat - 各种音视频封装格式的生成和解析,包括获取解码所需信息、读取音视频数据等功能
  • libswscale - 视频图像缩放的库,提供颜色空间转换、图像格式转换的功能
  • libavutil - 工具库,包括算数运算,字符操作等
  • libpostproc - 对视频做预处理的库
  • libpostproc - 滤镜功能库
  • libavdevice - 提供了访问捕获设备和回放设备的接口

此外,ffmpeg还向开发者提供了丰富的命令行工具,对于不熟悉ffmpeg的开发者来说,可以先通过这些命令行工具,对ffmpeg的基本功能有个大致的了解,下面我们就开始正式讨论。


1、ffmpeg安装

这里以Ubuntu 16 LTS版为例介绍FFmpeg的安装。

添加PPA源

ffmpeg 4是目前最新的发行版本,如果要安装该版本,需要添加PPA源,执行以下命令添加PPA仓库到Ubuntu系统

sudo add-apt-repository ppa:jonathonf/ffmpeg-4

安装ffmpeg

通过以下命令安装ffmpeg

sudo apt-get update
sudo apt-get install ffmpeg

​可以看到x264等相关的动态库文件也会一同安装

sudo apt-get install ffmpeg
[sudo] password for ron: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libaom0 libavcodec58 libavdevice58 libavfilter7 libavformat58 libavresample4 libavutil56 libbs2b0 libchromaprint1 libcodec2-0.4
  libflite1 liblilv-0-0 libmysofa1 libnorm1 libopenjp2-7 libopenmpt0 libpgm-5.2-0 libpocketsphinx3 libpostproc55 librabbitmq4
  librubberband2v5 libserd-0-0 libsodium18 libsord-0-0 libsphinxbase3 libsratom-0-0 libsrt1-gnutls libswresample3 libswscale5
  libvdpau1 libvidstab1.1 libx264-155 libx265-192 libzmq5 mesa-vdpau-drivers vdpau-driver-all
Suggested packages:
  ffmpeg-doc serdi sordi libvdpau-va-gl1 nvidia-vdpau-driver nvidia-legacy-340xx-vdpau-driver
Recommended packages:
  pocketsphinx-hmm-en-hub4wsj | pocketsphinx-hmm-zh-tdt | pocketsphinx-hmm-en-tidigits pocketsphinx-lm-en-hub4
  | pocketsphinx-lm-zh-hans-gigatdt | pocketsphinx-lm-zh-hant-gigatdt
The following NEW packages will be installed:
  ffmpeg libaom0 libavcodec58 libavdevice58 libavfilter7 libavformat58 libavresample4 libavutil56 libbs2b0 libchromaprint1
  libcodec2-0.4 libflite1 liblilv-0-0 libmysofa1 libnorm1 libopenjp2-7 libopenmpt0 libpgm-5.2-0 libpocketsphinx3 libpostproc55
  librabbitmq4 librubberband2v5 libserd-0-0 libsodium18 libsord-0-0 libsphinxbase3 libsratom-0-0 libsrt1-gnutls libswresample3
  libswscale5 libvdpau1 libvidstab1.1 libx264-155 libx265-192 libzmq5 mesa-vdpau-drivers vdpau-driver-all
0 upgraded, 37 newly installed, 0 to remove and 233 not upgraded.
Need to get 29.2 MB of archives.
After this operation, 106 MB of additional disk space will be used.
Do you want to continue? [Y/n]

确认ffmpeg安装成功

ffmpeg安装完成后,可以通过ffmpeg -version命令打印FFmpeg的版本号,能够成功打印即可确认ffmpeg安装成功。

ffmpeg -version

2、ffmpeg源码编译

ffmpeg源码编译并不需要多么复杂的编译环境,只需要基本的编译器及依赖库即可。ffmpeg支持多个主流平台的开发,这里主要讲述如何在linux环境下进行编译。

ffmpeg编译环境准备

这里以ubuntu系统为例进行讲述,其他linux发行版原理类似。
在linux系统上编译需要安装gcc make等组件,可以用下命令进行安装

sudo apt-get install build-essential

输入gcc -v命令即可查询当前的gcc版本号

gcc -v

ffmpeg依赖库准备

ffmpeg源码编译主要依赖x264、yasm这两个组件,在搜索引擎中可以非常容易到找到它们的源码包,通过源码包编译安装的方式还可用于嵌入式环境

x264: x264 is a free software library and application for encoding video streams into the H.264/MPEG-4 AVC compression format, and is released under the terms of the GNU GPL.

Yasm: Yasm is a complete rewrite of the NASM assembler under the “new” BSD License,Yasm currently supports the x86 and AMD64 instruction sets, accepts NASM and GAS assembler syntaxes, outputs binary, ELF32, ELF64, 32 and 64-bit Mach-O, RDOFF2, COFF, Win32, and Win64 object formats, and generates source debugging information in STABS, DWARF 2, and CodeView 8 formats.

下面给出源码包的链接地址

https://ffmpeg.org/download.html#get-sources
http://download.videolan.org/pub/videolan/x264/snapshots/
http://yasm.tortall.net/Download.html

这里实测过的源码版本分别是

x264-0.148 (x264 snapshot-20170307-2245)
yasm-1.3.0
ffmpeg-3.2.4

其中,x264提供对h.264编码器的支持,yasm用于对汇编优化的支持,若不需要汇编优化的支持,可在编译选项中关闭yasm即可(–disable-yasm)

在Linux下可采用以下方式配置编译选项:

yasm configure配置

./configure --prefix=/usr/local/3rdparty/yasm

x264 configure配置

./configure --prefix=/usr/local/3rdparty/x264 --enable-shared --enable-static --enable-yasm

生成Makefile文件后,输入make命令即可开始编译过程,编译完成后,执行make install命令进行安装

make 
sudo make install

x264、yasm编译完成后,还需要让系统能够找到对应的安装位置。打开/etc/profile配置文件,在该文件底部添加各组件的环境变量

# YASM
export PATH="$PATH:/usr/local/3rdparty/yasm/bin/"
export LD_LIBRARY_PATH=/usr/local/3rdparty/yasm/lib:$LD_LIBRARY_PATH

# X264
export PATH="$PATH:/usr/local/3rdparty/x264/bin/"
export LD_LIBRARY_PATH=/usr/local/3rdparty/x264/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/usr/local/3rdparty/x264/lib/pkgconfig:$PKG_CONFIG_PATH

使用source /etc/profile命令刷新环境变量

source /etc/profile

环境变量配置完成后,可以通过下面的命令确认x264等依赖组件是否编译安装成功

x264 --version
x264 0.148.x
built on May 22 2019, gcc: 5.4.0 20160609
x264 configuration: --bit-depth=8 --chroma-format=all
libx264 configuration: --bit-depth=8 --chroma-format=all
x264 license: GPL version 2 or later

yasm --version
yasm 1.3.0
Compiled on May  6 2015.
Copyright (c) 2001-2014 Peter Johnson and other Yasm developers.
Run yasm --license for licensing overview and summary.

ffmpeg源码编译

在Linux下可采用以下方式配置编译选项:

ffmpeg configure配置

./configure --prefix=/usr/local/3rdparty/ffmpeg --enable-shared --enable-yasm --enable-libx264 --enable-gpl --enable-pthreads --extra-cflags=-I/usr/local/3rdparty/x264/include --extra-ldflags=-L/usr/local/3rdparty/x264/lib

生成Makefile文件后,输入make命令即可开始编译过程,编译完成后,执行make install命令进行安装

make 
sudo make install

编译完成后,在源码目录生成ffmpeg、ffserver、ffprobe等命令行工具,其中

  • ffmpeg - 命令行工具支持视频编解码、视频转码、视频格式转换、视频推流等功能
  • ffserver - 命令行工具与ffmpeg配合,负责响应客户端的流媒体请求,把流媒体数据发送给客户端
  • ffprobe - 命令行工具用来查看多媒体文件的描述信息

ffmpeg环境变量配置

在ffmpeg及其依赖环境编译完成后,还需要让系统能够找到对应的安装位置。打开/etc/profile配置文件,在该文件底部添加各组件的环境变量

# FFMPEG
export PATH="$PATH:/usr/local/3rdparty/ffmpeg/bin/"
export LD_LIBRARY_PATH=/usr/local/3rdparty/ffmpeg/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/usr/local/3rdparty/ffmpeg/lib/pkgconfig:$PKG_CONFIG_PATH

使用source /etc/profile命令刷新环境变量

source /etc/profile

使用ffmpeg -version命令打印版本号,这里采用的ffmpeg是3.2.4版本

ffmpeg -version
ffmpeg version 3.2.4 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.12) 20160609
configuration: --prefix=/usr/local/3rdparty/ffmpeg --enable-shared --enable-yasm --enable-libx264 --enable-gpl --enable-pthreads --extra-cflags=-I/usr/local/3rdparty/x264/include --extra-ldflags=-L/usr/local/3rdparty/x264/lib
libavutil      55. 34.101 / 55. 34.101
libavcodec     57. 64.101 / 57. 64.101
libavformat    57. 56.101 / 57. 56.101
libavdevice    57.  1.100 / 57.  1.100
libavfilter     6. 65.100 /  6. 65.100
libswscale      4.  2.100 /  4.  2.100
libswresample   2.  3.100 /  2.  3.100
libpostproc    54.  1.100 / 54.  1.100

在ffmpeg源码路径下,可以通过ldd命令查询ffmpeg依赖的所有组件,若其中的某些组件无法找到,则需要对照本文查询是否有依赖的组件未配置环境变量。类似的,也可通过ldd命令在对应的路径下查询x264等组件的依赖项。

ldd ffmpeg
	linux-vdso.so.1 =>  (0x00007ffc24f84000)
	libavdevice.so.57 => /usr/local/3rdparty/ffmpeg/lib/libavdevice.so.57 (0x00007fc17da42000)
	libavfilter.so.6 => /usr/local/3rdparty/ffmpeg/lib/libavfilter.so.6 (0x00007fc17d613000)
	libavformat.so.57 => /usr/local/3rdparty/ffmpeg/lib/libavformat.so.57 (0x00007fc17d1f3000)
	libavcodec.so.57 => /usr/local/3rdparty/ffmpeg/lib/libavcodec.so.57 (0x00007fc17bcbf000)
	libpostproc.so.54 => /usr/local/3rdparty/ffmpeg/lib/libpostproc.so.54 (0x00007fc17baa3000)
	libswresample.so.2 => /usr/local/3rdparty/ffmpeg/lib/libswresample.so.2 (0x00007fc17b887000)
	libswscale.so.4 => /usr/local/3rdparty/ffmpeg/lib/libswscale.so.4 (0x00007fc17b5ff000)
	libavutil.so.55 => /usr/local/3rdparty/ffmpeg/lib/libavutil.so.55 (0x00007fc17b385000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fc17b07c000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc17ae5f000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc17aa95000)
	libXv.so.1 => /usr/lib/x86_64-linux-gnu/libXv.so.1 (0x00007fc17a890000)
	libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007fc17a556000)
	libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007fc17a344000)
	libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007fc17a122000)
	libxcb-shm.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-shm.so.0 (0x00007fc179f1e000)
	libxcb-xfixes.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-xfixes.so.0 (0x00007fc179d16000)
	libxcb-shape.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-shape.so.0 (0x00007fc179b12000)
	libasound.so.2 => /usr/lib/x86_64-linux-gnu/libasound.so.2 (0x00007fc179812000)
	libSDL2-2.0.so.0 => /usr/local/3rdparty/sdl2/lib/libSDL2-2.0.so.0 (0x00007fc1794df000)
	libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fc1792c5000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc1790c1000)
	libx264.so.148 => /usr/local/3rdparty/x264/lib/libx264.so.148 (0x00007fc178d1b000)
	liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007fc178af9000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fc17dc5a000)
	libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007fc1788f5000)
	libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007fc1786ef000)
	librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fc1784e7000)

// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
// 公众号:断点实验室
// 扫描二维码,关注更多优质原创,内容包括:音视频开发、图像处理、网络、
// Linux,Windows、Android、嵌入式开发等

ffmpeg源码编译环境搭建_第1张图片

你可能感兴趣的:(FFmpeg视频开发入门教程)