1. 安装 yasm
ffmpeg编译中为了提高编译速度,使用了汇编指令,于是需要使用 yasm 这个工具。 当然,如果实在不想要这个功能,可以在下一步的配置中使用./configure –disable-yasm选项。
(1)下载
Download - The Yasm Modular Assembler Project
(2)安装
[root@localhost whkt]# tar zxvf yasm-1.3.0.tar.gz
[root@localhost yasm-1.3.0]# ./configure
[root@localhost yasm-1.3.0]# make && make install
2. 下载 ffmpeg 安装包
Download FFmpeg 因为通过添加yum源方式下载的rpm包版本较低,故使用 到官网自行下载最新版rpm包的方式安装。
3. 解压 ffmpeg 安装包
[root@localhost whkt]# tar -zxvf ffmpeg-4.3.4.tar.gz
4. ffmpeg 配置必要选项
这一步根据自己需要开启或关闭某些选项,具体可以使用./configure –help查看,或者直接查看configure文件。本文使用如下简单配置。
[root@localhost whkt]# cd ffmpeg-4.3.4
[root@localhost ffmpeg-4.3.4]# ./configure --enable-shared --enable-swscale --enable-gpl --enable-nonfree --enable-pic --prefix=/usr/local/whkt/ffmpeg --enable-postproc --enable-pthreads --enable-static --enable-libx264 --enable-libfdk-aac
–enable-shared 表示生成动态链接库,可以供以后编程使用,同时生成的可执行程序也依赖这些动态库。如果不加上 –enable-shared 选项则使用静态链接的方式编译,此时不会生成动态库,同时生成的ffmpeg等的可执行文件也比较大,但他们不需要动态库就可以直接运行。 –prefix 表示程序安装的目录,这里设为/usr/local/whkt/ffmpeg。 opencv会使用到libswscale,所以要在编译的时候把swscale这个库也编译出来;而libswscale是遵守gpl的,不enable gpl的话就无法编译。
报错解决
(1)aac
ERROR: libfdk_aac not found
If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.
fdk-acc下载
wget https://sourceforge.net/projects/opencore-amr/files/fdk-aac/fdk-aac-2.0.2.tar.gz
如果是离线的话先下载后放到服务器上。
tar xzf fdk-aac-2.0.2.tar.gz
cd fdk-aac-2.0.2
./configure
make && make install
(2)libx264
ERROR: libx264 not found
If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.
libx264 下载
wget http://download.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20191217-2245-stable.tar.bz2
tar xjf x264-snapshot-20191217-2245-stable.tar.bz2
cd x264-snapshot-20191217-2245-stable
./configure --prefix=/usr/local/whkt/x264 --enable-shared --enable-static --disable-asm
make && make install
vim /etc/profile //文件末尾加入下面内容
export PATH=/usr/local/whkt/x264/bin:$PATH
export PATH=/usr/local/whkt/x264/include:$PATH
export PATH=/usr/local/whkt/x264/lib:$PATH
vim /etc/ld.so.conf //增加以下内容
/usr/local/whkt/x264/lib
ldconfig //使配置生效
vim /etc/profile //增加以下内容
export PKG_CONFIG_PATH=/usr/local/whkt/lib/pkgconfig:$PKG_CONFIG_PATH //(此路径为.pc文件所在路径),可使用
echo $PKG_CONFIG_PATH //查看有没设置生效
5. 编译安装
编译,需要较长时间:
[root@localhost ffmpeg-4.3.4]# make && make install
6. 路径处理
[root@localhost ffmpeg-4.3.4]# cd /usr/local/whkt/ffmpeg
[root@localhost ffmpeg]# ls
bin include lib share123
安装完成后在/usr/local/whkt/ffmpeg出现三个目录: ● bin:可执行文件目录 ● lib:动态链接库目录 ● include:编程用到的头文件目录
(1)添加动态库路径
不管是编程还是可执行程序的执行都需要依赖lib下面的动态库,可以把里面的so文件拷贝到/usr/lib下,但可以直接修改配置文件。 通过查看/etc/ld.so.conf文件,发现里面只有一句话:
include ld.so.conf.d/*.conf
表明其包含了ld.so.conf.d下所有的conf文件,于是可以在/etc/ld.so.conf.d/创建一个新的文件ffmpeg.conf,其中包含一句话,即为ffmpeg的lib目录:
[root@localhost ffmpeg]# cd /etc/ld.so.conf.d
[root@localhost ld.so.conf.d]# pwd
/etc/ld.so.conf.d
[root@localhost ld.so.conf.d]# vim ffmpeg.conf
添加:
/usr/local/whkt/ffmpeg/lib
执行ldconfig,更新ld.so.cache,使修改生效:
[root@localhost ld.so.conf.d]# ldconfig
(2)创建软链接
为了在任何地方能够直接用ffmpeg运行,而不用使用如./ffmpeg或者/usr/local/whkt/ffmpeg/bin/ffmpeg的方式运行程序,可以把可执行程序复制到bin目录下,这里选择在bin目录下创建软链接。软链接类似于Windows下的快捷方式,如果原可执行程序被删除了,软链接也不能继续使用,而硬链接则可以继续使用。创建软连接如下:
[root@localhost ld.so.conf.d]# ln -s /usr/local/whkt/ffmpeg/bin/ffmpeg /usr/local/bin/
[root@localhost ld.so.conf.d]# ln -s /usr/local/whkt/ffmpeg/bin/ffprobe /usr/local/bin/
[root@localhost ld.so.conf.d]# ln -s /usr/local/whkt/ffmpeg/bin/ffserver /usr/local/bin/ (这里我本地并不存在ffserver)
7.环境变量设置
[root@localhost pkgconfig]# vim /etc/profile
添加:
# ffmpeg视频处理
export PATH=/usr/local/whkt/ffmpeg/bin:$PATH
使配置立即生效:
[root@localhost pkgconfig]# source /etc/profile
8. 测试
报错
[root@localhost ffmpeg]# ffmpeg
./bin/ffmpeg: error while loading shared libraries: libavdevice.so.58: cannot open shared object file: No such file or directory
查看缺少依赖
[root@localhost ffmpeg]# cd ..
[root@localhost whkt]# cd ffmpeg-4.3.4
[root@localhost ffmpeg-4.3.4]# ldd ffmpeg
linux-vdso.so.1 => (0x00007ffcc7bdb000)
libavdevice.so.58 => not found
libavfilter.so.7 => not found
libavformat.so.58 => not found
libavcodec.so.58 => not found
libpostproc.so.55 => not found
libswresample.so.3 => not found
libswscale.so.5 => not found
libavutil.so.56 => not found
libm.so.6 => /lib64/libm.so.6 (0x00007f041f1c4000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f041efa8000)
libc.so.6 => /lib64/libc.so.6 (0x00007f041ebda000)
/lib64/ld-linux-x86-64.so.2 (0x00007f041f4c6000)
然后查询所缺依赖的位置
[root@localhost ffmpeg-4.3.4]# find /usr -name 'libavdevice.so.58'
/usr/local/whkt/ffmpeg-4.3.4/libavdevice/libavdevice.so.58
/usr/local/whkt/ffmpeg/lib/libavdevice.so.58
多查看几个发现都在/usr/local/whkt/ffmpeg/lib下 进入/etc/profile 增加内容
[root@localhost ffmpeg-4.3.4]# vi /etc/profile
export LD_LIBRARY_PATH=/usr/local/whkt/ffmpeg/lib/
[root@localhost ffmpeg-4.3.4]# source /etc/profile
测试ffmpeg发现又有问题
ffmpeg: error while loading shared libraries: libfdk-aac.so.2: cannot open shared object file: No such file or directory
[root@localhost whkt]# find /usr -name 'libfdk-aac.so.2'
/usr/local/lib/libfdk-aac.so.2
/usr/local/whkt/fdk-aac-2.0.2/.libs/libfdk-aac.so.2
[root@localhost whkt]# vi /etc/ld.so.conf
/usr/local/lib //增加
[root@localhost whkt]# ldconfig
再次测试成功。
[root@localhost /]# ffmpeg
ffmpeg version 4.3.4 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-36)
configuration: --enable-shared --enable-swscale --enable-gpl --enable-nonfree --enable-pic --prefix=/usr/local/whkt/ffmpeg --enable-postproc --enable-pthreads --enable-static --enable-libx264 --enable-libfdk-aac
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
Use -h to get full help or, even better, run 'man ffmpeg'
以上文章采取各位大佬的文档亲自实现解决。