乐趣区

关于c++: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.h…
http://download.videolan.org/…
http://yasm.tortall.net/Downl…

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

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、嵌入式开发等

退出移动版