一、介绍

  FFmpeg 是Fast Forward Mpeg的简写,是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多code都是从头开发的。

  FFmpeg在Linux平台下开发,但它同样也可以在其它操作系统环境中编译运行,包括Windows、Mac OS X等。这个项目最早由Fabrice Bellard发起,2004年至2015年间由Michael Niedermayer主要负责维护。许多FFmpeg的开发人员都来自MPlayer项目,而且当前FFmpeg也是放在MPlayer项目组的服务器上。项目的名称来自MPEG视频编码标准,前面的"FF"代表"Fast Forward"。  ---摘自百度

二、安装

ffmpeg要实现的功能:对短视频进行转码、打水印、去除logo等

1、安装yasm
yasm 是x86平台的一个汇编优化器, 可以加快ffmpeg x264的编译
下载地址:http://yasm.tortall.net/releases/Release1.3.0.html
#tar xf yasm-1.3.0.tar.gz
#cd yasm-1.3.0
#./configure
#make && make install

2、安装依赖包
#yum install libart_lgpl libass-devel freetype-devel opus-devel libvorbis-devel libvpx-devel -y

3、安装x264
H.264是ITU(International Telecommunication Union,国际通信联盟)和MPEG(Motion Picture Experts Group,运动图像专家组)
联合制定的视频编码标准。而x264是一个开源的H.264/MPEG-4 AVC视频编码函数库[1]  ,是最好的有损视频编码器

#wget http://download.videolan.org/x264/snapshots/last_stable_x264.tar.bz2
#tar xvf last_stable_x264.tar.bz2 
#cd x264-snapshot-20171114-2245-stable/
#./configure --enable-shared
#make && make install

执行完make install 之后的输出
install -d /usr/local/bin
install x264 /usr/local/bin
install -d /usr/local/include
install -d /usr/local/lib
install -d /usr/local/lib/pkgconfig
install -m 644 ./x264.h /usr/local/include
install -m 644 x264_config.h /usr/local/include
install -m 644 x264.pc /usr/local/lib/pkgconfig
ln -f -s libx264.so.148 /usr/local/lib/libx264.so
install -m 755 libx264.so.148 /usr/local/lib

4、安装x265
x265是一个用于编码符合高效率视频编码(HEVC/H.265)标准的视频的开源自由软件及函数库。x265与x264项目类似
x265使用GNU通用公共许可证(GPL)2授权或商业许可证授权提供

下载地址:http://ftp.videolan.org/pub/videolan/x265/
#tar xf x265_2.5.tar.gz
#cd x265_2.5/build/linux
#bash make-Makefiles.bash   #等出现提示界面,输入g 退出
#make   #执行完成之后,库便在当前目录下生成了
#ll 
total 10288
drwxr-xr-x. 2 root root    4096 Nov 16 10:59 cmake
-rw-r--r--. 1 root root   16987 Nov 16 10:58 CMakeCache.txt
drwxr-xr-x. 9 root root    4096 Nov 16 11:01 CMakeFiles
-rw-r--r--. 1 root root    4747 Nov 16 10:58 cmake_install.cmake
drwxr-xr-x. 3 root root    4096 Nov 16 10:59 common
drwxr-xr-x. 3 root root    4096 Nov 16 10:59 encoder
-rw-r--r--. 1 root root 5523420 Nov 16 11:01 libx265.a
lrwxrwxrwx. 1 root root      14 Nov 16 11:01 libx265.so -> libx265.so.130
-rwxr-xr-x. 1 root root 4820615 Nov 16 11:01 libx265.so.130
-rw-r--r--. 1 root root   14807 Nov 16 10:59 Makefile
-rwxr-xr-x. 1 root root     110 Jul 13 19:20 make-Makefiles.bash
-rwxr-xr-x. 1 root root     982 Jul 13 19:20 multilib.sh
-rwxr-xr-x. 1 root root  102954 Nov 16 11:01 x265
-rw-r--r--. 1 root root    1426 Nov 16 10:58 x265_config.h
-rw-r--r--. 1 root root     479 Nov 16 10:58 x265.def
-rw-r--r--. 1 root root     244 Nov 16 10:58 x265.pc

#make install
[ 64%] Built target common
[ 88%] Built target encoder
[ 88%] Built target x265-shared
[100%] Built target cli
[100%] Built target x265-static
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/lib/libx265.a
-- Installing: /usr/local/include/x265.h
-- Installing: /usr/local/include/x265_config.h
-- Installing: /usr/local/lib/libx265.so.130
-- Installing: /usr/local/lib/libx265.so
-- Installing: /usr/local/lib/pkgconfig/x265.pc
-- Installing: /usr/local/bin/x265
-- Removed runtime path from "/usr/local/bin/x265"

补充:x264和x265的对比
265相当于对图像进行了有重点的编码,从而降低了整体的码率,编码效率就相应提高了。
画质对比:265比264皮肤更细腻;
压缩比对比:压缩率更高,节省三分之一;
硬件支持:瑞芯微 RK3188 支持265的快播盒子已经上市;
同等画质体积仅为三分之二、带宽节省三分之一、画质更细腻等诸多优势;
解码难度是264的3倍。
H.265的帧内预测模式支持33种方向(H.264只支持8种),并且提供了更好的运动补偿处理和矢量预测方法。
大有H.264当时的风采,压缩效率依然提升了一倍,解码难度轻描淡写地增加了3倍

5、开始编译安装ffmpeg
下载地址:http://ffmpeg.org/download.html
#tar xf ffmpeg-3.4.tar.bz2
#cd ffmpeg-3.4
#./configure --prefix=/opt/app/ffmpeg3 \
  --enable-shared \
  --enable-filter=delogo \
  --enable-gpl \
  --enable-libass \
  --enable-libfreetype \
  --enable-libopus \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libx264 \
  --enable-libx265 \
  --enable-nonfree
  
如果出现类似error如下:
WARNING: using libfdk without pkg-config
WARNING: using libx264 without pkg-config
ERROR: x265 not found using pkg-config
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.

原因是需要设置PKG_CONFIG_PATH,通过pkg-config去指定路径自动寻找需要链接的依赖库,解决方法如下:
#export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH  (此路径为.pc文件所在路径),可使用
#echo $PKG_CONFIG_PATH  查看
/usr/local/lib/pkgconfig
然后重新编译就ok了!

继续执行
#make           #需要较长的时间,30分钟左右
#make install 

6、查看版本
#/opt/app/ffmpeg3/bin/ffmpeg 
/opt/app/ffmpeg3/bin/ffmpeg: error while loading shared libraries: libavdevice.so.57: cannot open shared object file: No such file or directory

原因是lib目录未加载到链接到系统库中,系统ld目录列表在/etc/ld.so.conf中,打开文件会发现,里面引用了/etc/ld.so.conf.d/下面所有的.conf文件
第一种解决方法:
#vim /etc/ld.so.conf.d/ffmpeg.conf 然后添加一行内容: 
/opt/app/ffmpeg3/lib    保存并退出,执行 #ldconfig 使配置生效,再次执行 ./ffmpeg -version 显示就正常了
如果再提示error while loading shared libraries: libx265.so.130: cannot open shared object file: No such file or directory
解决方法和上面一样:
#vim /etc/ld.so.conf.d/ffmpeg.conf
/opt/app/ffmpeg3/lib/
/usr/local/lib/          #添加一行,因为x265的lib库是在/usr/local/lib目录

#ldconfig   

第二种解决方法:
#vim /etc/profile
export LD_LIBRARY_PATH=/opt/app/ffmpeg/lib/:/usr/local/lib/:$LD_LIBRARY_PATH
#. /etc/profile

再次查看ffmpeg 版本就正常了。

#/opt/app/ffmpeg3/bin/ffmpeg -version
ffmpeg version 3.4 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-18)
configuration: --prefix=/opt/app/ffmpeg3 --enable-shared --enable-filter=delogo --enable-gpl --enable-libass --enable-libfreetype --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree
libavutil      55. 78.100 / 55. 78.100
libavcodec     57.107.100 / 57.107.100
libavformat    57. 83.100 / 57. 83.100
libavdevice    57. 10.100 / 57. 10.100
libavfilter     6.107.100 /  6.107.100
libswscale      4.  8.100 /  4.  8.100
libswresample   2.  9.100 /  2.  9.100
libpostproc    54.  7.100 / 54.  7.100

7、其它模块
ffmpeg 可以编译进很多模块,根据自己的需要来添加,比如安装lame
下载地址: https://sourceforge.net/projects/lame/files/lame/3.100/
#tar xf  lame-3.100.tar.gz
#cd lame
#./configure
#make
#make install

然后编译ffmpeg的时候添加--enable-libmp3lame即可。

8、后期在用到ffmpeg 的时候,发现某些模块没有编译进去,只能重新编译
比如需要将--enable-openssl 模块编译到ffmpeg中
#rm -rf /opt/app/ffmpeg3
#tar xf ffmpeg-3.4.tar.bz2
#cd ffmpeg-3.4
#export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/   加载x264等的pkgconfig文件
#./configure --prefix=/opt/app/ffmpeg3 \
  --enable-shared \
  --enable-filter=delogo \
  --enable-gpl \
  --enable-libass \
  --enable-libfreetype \
  --enable-libopus \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libx264 \
  --enable-libx265 \
  --enable-nonfree \
  --enable-openssl
 #make && make install

三、ffmpeg的用法

#./ffmpeg --help    #查看ffmpeg的用法
一个例子:
#ffmpeg -i logo.mp4 -vf delogo=1:1:20:20:1 a.mp4 -y  #按照一定尺寸调整mp4视频

网上参考链接:
http://coders.blog.51cto.com/3203119/1566314
https://www.cnblogs.com/wainiwann/p/4128154.html


其他参考链接:

https://www.cnblogs.com/freeweb/p/6897907.html

https://bitbucket.org/multicoreware/x265/wiki/Home