1. 前言
本教程涉及的ffmpeg, x264, x265
2. 环境依赖
2.1 删除系统中安装的ffmpeg等库
sudo apt-get --purge remove ffmpeg mplayer x264 x265
2.2 安装依赖库
sudo apt-get update
sudo apt-get -y --force-yes install autoconf automake build-essential libass-dev libfreetype6-dev libgpac-dev \
libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev \
libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev
注: server版本号可忽略
libsdl1.2-dev libva-dev libvdpau-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev
2.3 下载源码
(1) ffmpeg
git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
(2) x264<pre name="code" class="plain"><pre name="code" class="plain">git clone git://git.videolan.org/x264.git
(3) x265hg clone https://bitbucket.org/multicoreware/x265
(4) yasm
yasm 是x86平台的一个汇编优化器, 可以加快ffmpeg x264的编译
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xzvf yasm-1.3.0.tar.gz
3. 编译
3.1 配置编译输出文件夹
export FFMPEG_PATH="YOUR_PATH"
export PATH="$FFMPEG_PATH/bin:$PATH"
比如我输出文件夹为$HOME/ffmpeg_2.6.3/build_out
那么:
export FFMPEG_PATH="$HOME/ffmpeg_2.6.3/build_out"
3.1 yasm
cd yasm-1.3.0
./configure --prefix="$HOME/"
make
make install
或者安装已有的库(yasm version ≥ 1.3.0)
sudo apt-get install yasm
3.2 x264
在配置ffmpeg config时须要增加--enable-gpl和--enable-libx264
cd x264*
./configure --prefix="$FFMPEG_PATH" --enable-shared --enable-static
make -j4
make install
make clean
或者安装已有的库(yasm version ≥ 1.3.0)sudo apt-get install libx264-dev
3.3 x265
cd x265/build/linux
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$FFMPEG_PATH" -DENABLE_SHARED:bool=off ../../source
make -j4
make install
make clean
</tt></span>
3.4 aac
在配置ffmpeg config时须要增加--enable-libfdk_aac(假设开启--enable-gpl同一时候须要开启--enable-nonfree )
PKG_CONFIG_PATH="$FFMPEG_PATH/lib/pkgconfig" ./configure \
--prefix="$FFMPEG_PATH" \
--pkg-config-flags="--static" \
--extra-cflags="-I$FFMPEG_PATH/include" \
--extra-ldflags="-L$FFMPEG_PATH/lib" \
--enable-gpl \
--enable-libass \
--enable-libfaac \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libtheora \
--enable-libvorbis \
--enable-libx264 \
--enable-libx265 \
--enable-nonfree \
--enable-shared \
--enable-postproc \
--enable-x11grab \
--enable-bzlib \
--enable-libxvid \
--enable-zlib \
--disable-debug
4. 将ffmpeg加入到环境变量
4.1 将ffmpeg加入到环境变量
vim ~/.bashrc
在最后加入
export FFMPEG_PATH="/home/guohe/workspace/ffmpeg_2.6.3/build_out"
export PATH="$FFMPEG_PATH/bin:$PATH"
4.2 加入链接库
编译完毕之后调用 ffmpeg 会出现 error while loading shared libraries: xxx
解决这样的问题有2中方法:
(1) 改动/etc/ld.so.conf
vim /etc/ld.so.conf
在最后加入$FFMPEG_PATH/lib
然后运行
sudo ldconfig
(2) 改动LD_LIBRARY_PATH
vim ~/.bashrc
在最后加入export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$FFMPEG_PATH/ib
========================================================================
二、问题:./usr/local/ffmpeg/bin/ffmpeg: error while loading shared libraries: libavdevice.so.58: cannot open shared object file: No such file or directory.
问题解决:
adminn@ubuntu:/usr/local/lib$ sudo vi /etc/ld.so.conf
1、修改如下
include ld.so.conf.d/*.conf
/usr/local/ffmpeg/lib ///usr/local/ffmpeg 目录是我ffmpeg安装目录,根据你的安装目录改吧
adminn@ubuntu:/usr/local/lib$
adminn@ubuntu:/usr/local/lib$
adminn@ubuntu:/usr/local/lib$
adminn@ubuntu:/usr/local/lib$ ldconfig
/sbin/ldconfig.real: Can't create temporary cache file /etc/ld.so.cache~: Permission denied
adminn@ubuntu:/usr/local/lib$ sudo ldconfig
2、为 Ffmpeg 加入环境变量
vi /etc/profile
加入以下内容:
export PATH="/usr/local/ffmpeg/bin:$PATH"
然后保存并运行source /etc/profile
adminn@ubuntu:/usr/local/lib$ ffmpeg -version
ffmpeg version 3.1.3 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
configuration: --enable-shared --disable-yasm --prefix=/usr/local/ffmpeg
libavutil 55. 28.100 / 55. 28.100
libavcodec 57. 48.101 / 57. 48.101
libavformat 57. 41.100 / 57. 41.100
libavdevice 57. 0.101 / 57. 0.101
libavfilter 6. 47.100 / 6. 47.100
libswscale 4. 1.100 / 4. 1.100
libswresample 2. 1.100 / 2. 1.100
adminn@ubuntu:/usr/local/lib$
================================
问题2:gcc log.c -lavutil
log.c:4:27: fatal error: libavutil/log.h: 没有那个文件或目录
问题解决:gcc -g -o ffmpeg_log log.c -I /usr/local/ffmpeg/include -L /usr/local/ffmpeg/lib -lavutil