本文为做者原创,转载请注明出处:http://www.javashuo.com/article/p-csvyvizq-er.htmlphp
本文主要讲述 linux 平台 x86(及x86-64) 架构下的 ffmpeg 编译安装过程。
其余嵌入式平台须要交叉编译,过程相似,不详述。
本实验在 opensuse 和 ubuntu 两个平台做了验证。使用lsb_release -a
命令查看系统发行版版本:
opensuse 平台版本:openSUSE Leap 42.3。
ubuntu 平台版本:Ubuntu 16.04.5 LTS。html
ffmpeg官网:https://www.ffmpeg.org/linux
SDL(Simple DirectMedia Layer) 是一套开源的跨平台多媒体开发库。SDL提供了数种控制图像、声音、输出输入的函数,封装了复杂的视音频底层操做,简化了视音频处理的难度。目前SDL多用于开发游戏、模拟器、媒体播放器等多媒体应用领域。
SDL 官网:https://www.libsdl.org/git
旧版 ffmpeg 及 x264/x265 使用 yasm 汇编器
Yasm 是英特尔x86架构下的一个汇编器和反汇编器。Yasm 是一个彻底重写的 Netwide 汇编器(NASM)。Yasm 一般能够与 NASM 互换使用,并支持 x86 和 x86-64 架构。其许可协议为修订过的 BSD 许可证。
此处 Yasm 用来编译 x86 平台下 ffmpeg 中部分汇编代码。
注意,Yasm 是 x86 平台汇编器,不须要交叉编译。如果 arm 等其余平台,交叉编译工具链中包含有对应的汇编器,则交叉编译 ffmpeg 时须要 --disable-yasm 选项。
Yasm 官网:http://yasm.tortall.net/github
新版 ffmpeg 及 x264/x265 改用 nasm 汇编器
Netwide Assembler (简称NASM) 是一款基于英特尔 x86 架构的汇编与反汇编工具。NASM 被认为是 Linux 平台上最受欢迎的汇编工具之一。
注意,NASM 是 x86 平台汇编器,不须要交叉编译。如果 arm 等其余平台,交叉编译工具链中包含有对应的汇编器,则交叉编译 ffmpeg 时须要 --disable-x86asm 选项。
NASM 官网:https://www.nasm.us/shell
x264 是开源的 h264 编码器,使用很是普遍,综合性能不比商业编解码器差。
x264 官网:https://www.videolan.org/developers/x264.html
ffmpeg 工程中实现了 h264 解码器,但无 h264 编码器。所以须要安装第三方编码器 x264ubuntu
x265 是开源的 h265 编码器。
x265 官网:http://www.x265.org/
下载地址一:https://bitbucket.org/multicoreware/x265/downloads/
下载地下二:https://www.videolan.org/developers/x265.html
ffmpeg 工程中实现了 h265 解码器,但无 h265 编码器。所以须要安装第三方编码器 x265bash
libmp3lame 是开源的 mp3 编码器。
libmp3lame 官网:http://lame.sourceforge.net/架构
librtmp: RTMPDump Real-Time Messaging Protocol API。
librtmp 又称 rtmpdump,是用于处理 RTMP 流的工具。支持全部形式的 RTMP,包括 rtmp://, rtmpt://, rtmpe://, rtmpte://, 和 rtmps://。
librtmp 文档:http://rtmpdump.mplayerhq.hu/librtmp.3.html
librtmp 官网:http://rtmpdump.mplayerhq.hu/ide
将编译源码获得的程序资源安装到用户目录 /home/think 下。则安装后,/home/think 目录下会多出 bin、include、lib、share 等目录
配置环境变量
编辑 /etc/profile,添加以下几行:
export PATH=/home/think/bin:$PATH export LIBRARY_PATH=/home/think/lib:/home/think/lib64:$LIBRARY_PATH export PKG_CONFIG_PATH=/home/think/lib/pkgconfig:$PKG_CONFIG_PATH export C_INCLUDE_PATH=/home/think/include:$C_INCLUDE_PATH
上述几个环境变量是程序编译时须要用到的库文件、头文件路径,以及可执行程序所在路径。
在命令行中运行以下命令,使新设置的环境变量当即生效:
source /etc/profile
配置动态库路径
编辑 /etc/ld.so.conf,添加以下两行:
/home/think/lib /home/think/lib64
ld.so.conf 中的内容是程序运行时须要搜索的动态库路径。
在命令行中运行以下命令,使新设置的动态库路径当即生效:
ldconfig
注意:应先安装 SDL,再安装 ffmpeg,不然 ffmpeg 编译时不会生成 ffplay
缘由如参考资料[3]所述。
两种安装方式,推荐第二种方式,可能遇到的问题比较少
编译源码安装(不推荐)
在 SD L官网 https://www.libsdl.org/ 下载最新源码包 SDL2-2.0.9.tar.gz
tar -zxvf SDL2-2.0.9.tar.gz cd SDL2-2.0.9/ ./configure --prefix=/home/think make make install
经过编译源码安装的方式,编译安装成功后运行 ffplay 可能会遇到挺多问题,参“4. 问题描述”
经过软件源在线安装(推荐)
opensuse 平台:
zypper install libSDL2-devel zypper install libSDL2_image-devel zypper install libSDL2_mixer-devel zypper install libSDL2_ttf-devel zypper install libSDL2_gfx-devel
ubuntu 平台:
apt-get install libsdl2-dev apt-get install libsdl2-image-dev apt-get install libsdl2-mixer-dev apt-get install libsdl2-ttf-dev apt-get install libsdl2-gfx-dev
旧版 ffmpeg 及 x264/x265 使用 yasm 汇编器
在官网下载页面 http://yasm.tortall.net/Download.html 下载最新版源码 yasm-1.3.0.tar.gz
tar -zxvf yasm-1.3.0.tar.gz cd yasm-1.3.0/ ./configure --prefix=/home/think make make install
新版 ffmpeg 及 x264/x265 改用 nasm 汇编器
具体从哪一版本开始改用nasm不太清楚,至今日 2018-11-20 获得的最新版本已改用 nasm
在官网https://www.nasm.us/下载最新版源码nasm-2.14.tar.bz2
tar -zxvf nasm-2.14.tar.gz cd nasm-2.14/ ./configure --prefix=/home/think make make install
在网址 https://www.videolan.org/developers/x264.html 下载源码包 last_x264.tar.bz2,这是 git 仓库的 master 分支源码。咱们直接输入如下地址下载 stable 分支:
http://download.videolan.org/x264/snapshots/last_stable_x264.tar.bz2
下载获得 last_stable_x264.tar.bz2 源码包。
tar -jxvf last_stable_x264.tar.bz2 cd x264-snapshot-20181119-2245-stable/ ./configure --prefix=/home/think --enable-shared --enable-static make make install
注意第 4 行配置选项中,未给出 --disable-asm 选项,则表示启用汇编选项
在网址 https://bitbucket.org/multicoreware/x265/downloads/ 下载源码包 x265_3.0.tar.gz
编译说明参照 https://bitbucket.org/multicoreware/x265/wiki/Home
tar -zxvf x265_3.0.tar.gz cd x265_3.0/build/linux/ ./make-Makefiles.bash 在上一行命令运行快结束时,出现 cmake 配置信息编辑界面,将 CMAKE_INSTALL_PREFIX 的值改为 /home/think make make install
在官网 http://lame.sourceforge.net/ 下载最新源码 lame-3.100.tar.gz
tar -zxvf lame-3.100.tar.gz cd lame-3.100 ./configure --prefix=/home/think make make install
根据官网 http://rtmpdump.mplayerhq.hu/ 说明,经过 git 下载源码。
编译安装方法参考源码目录下 README 文件和 Makefile 文件。
git clone git://git.ffmpeg.org/rtmpdump cd rtmpdump make SYS=posix make install prefix=/home/think
在官网 https://www.ffmpeg.org/ 下载最新版源码包。或者使用 git 克隆 ffmpeg 源码仓库。
ffmpeg 源码仓库地址 https://git.ffmpeg.org/ffmpeg.git,在 github 上镜像地址 https://github.com/FFmpeg/FFmpeg.git。
git clone https://git.ffmpeg.org/ffmpeg.git cd ffmpeg git tag git checkout -b n4.1.2 n4.1.2 ./configure --prefix=/home/think \ --enable-shared --enable-static --enable-gpl --enable-pthreads \ --enable-libx264 --enable-libx265 --enable-libmp3lame --enable-librtmp \ --extra-cflags=-I/home/think/include --extra-ldflags=-L/home/think/lib make make install
./configure
选项中--extra-cflags=-I/home/think/include --extra-ldflags=-L/home/think/lib
是指定 ffmpeg 编译时须要的第三方库 (libx264等) 的头文件目录和库文件目录。由于咱们前面将全部第三方库的安装路径都设置为/home/think/
目录。
若是机器上已经编译安装过 ffmpeg,须要再次编译安装时,须要先卸载旧版本的头文件和库文件,不然编译时可能优先使用已安装的旧的头文件或库文件,致使编译失败。
./configure --prefix=/home/think make uninstall
若是是全新安装,没必要执行上述卸载命令。
测试文件下载(右键另存为):huangh.flv
在命令行中运行以下测试命令:
ffmpeg -i huangh.flv -c copy huangh.ts ffplay huangh.flv ffprobe huangh.flv
注意:
远程终端处于控制台命令行模式(运行级别3),无权限调用 SDL,所以没法测试 ffplay,但能够测试 ffmpeg 和 ffprobe。测试 ffplay 须要 X11 控制台模式(运行级别5,即 GUI 图形模式)。
错误提示:
Could not initialize SDL - No available video device
(Did you set the DISPLAY variable?)
缘由分析:
参考资料[4]http://www.javashuo.com/article/p-rspnrgxd-na.html
解决方法:
a) 安装x11的库文件:
opensuse平台:
zypper install libX11-devel zypper install xorg-x11-devel
ubuntu平台:
apt-get install libx11-dev apt-get install xorg-dev
b) 从新编译安装SDL
错误提示:
Could not initialize SDL - Audio target 'pulse' not available
(Did you set the DISPLAY variable?)
缘由分析:
参考资料[5]http://forums.libsdl.org/viewtopic.php?t=7609
解决方法:
a) 安装缺乏的库
opensuse 平台:
zypper install libpulse-devel zypper install libasound2 zypper install libasound2-devel // 实测不安装此包也无问题,若软件源中无此包则没必要安装
ubuntu 平台:
apt-get install libpulse-dev apt-get install libasound2 apt-get install libasound2-dev
b) 从新编译安装 SDL
错误提示:
编译ffmpeg,运行./configure --enable-libx265 ...
出现以下错误提示:
x265 not found using pkg-config
缘由分析:
参考资料[6]https://stackoverflow.com/questions/51918409/compiling-ffmpeg-x265-not-found-using-pkg-config
解决方法:
export PKG_CONFIG_PATH=/home/think/lib/pkgconfig:$PKG_CONFIG_PATH
[1] “ffmpeg 编译”, https://blog.csdn.net/season_hangzhou/article/details/24399371
[2] “ffmpeg 编译”,http://www.javashuo.com/article/p-uhiuegyr-eh.html
[3] “ffmpeg 编译未生成ffplay”, http://blog.chinaunix.net/uid-11344913-id-3936227.html
[4] “SDL 失败:无有效视频设备”,http://www.javashuo.com/article/p-rspnrgxd-na.html
[5] “SDL 失败:无有效音频设备”,http://forums.libsdl.org/viewtopic.php?t=7609
[6] “x265 not found using pkg-config”,https://stackoverflow.com/questions/51918409/compiling-ffmpeg-x265-not-found-using-pkg-config
[7] “configure, pkg-config”,http://www.javashuo.com/article/p-gazyhatz-eg.html
[8] “Yasm”, https://zh.wikipedia.org/wiki/Yasm
[9] “NASM”, https://zh.wikipedia.org/wiki/Netwide_Assembler
2018-11-20 1.0 初稿 2019-03-26 1.1 增长 librtmp 库。ffmpeg 版本 4.1 升级至 4.1.2 2019-04-04 1.2 增长 libmp3lame 库