本文最后更新于:2015-05-25 13:12:00
原文:http://www.yaosansi.com/post/ffmpeg-on-centos

在CentOS编译安装ffmpeg比较麻烦,以前也搞过多次,由于博客调整,特重新记录一下,以备不时之需。

使用yum安装ffmpeg和相关软件

快速安装 ffmpeg

直接安装:

1
yum install ffmpeg ffmpeg-devel

若没有找到相应包,需要先加入要应的包。

将下列代码命名为 dag.repo 放在 /etc/yum.repos.d 目录 下。

1
2
3
4
5
[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=1
enabled=1

然后再执行上方的安装命令。

安装 ffmpeg-php 扩展

1
2
3
4
5
6
wget /path/to/this/file/ffmpeg-php-0.5.2.1.tbz2
tar -xjf ffmpeg-0.5.2.1.tbz2
phpize
./configure
make
make install

修改 php.ini 文件

安装完成后,将会在/usr/local/lib/php/extensions/no-debug-non-zts-20060613/ffmpeg.so 看到php扩展文件。

打开php.ini文件,加入下面两行

1
2
[ffmpeg]
extension=ffmpeg.so

重启WEB服务器。使用phpinfo即可看到ffmpeg相关信息。

1
2
3
4
5
6
7
// #test.php

<?php

phpinfo()

?>

php 使用方法

1
2
3
4
5
6
7
8
9
10
<?php
$extension = "ffmpeg";
$extension_soname = $extension . "." . PHP_SHLIB_SUFFIX;
$extension_fullname = PHP_EXTENSION_DIR . "/" . $extension_soname;

// load extension
if(!extension_loaded($extension)) {
dl($extension_soname) or die("Can't load extension $extension_fullname\n");
}
?>

安装 Mplayer 和 Mencoder

1
yum install mplayer mencoder

常见问题

常见问题及解决方案请参考这里:http://www.mysql-apache-php.com/ffmpeg-install.htm

自行编译安装 ffmpeg

安装依赖项

1
yum install autoconf automake gcc gcc-c++ git libtool make nasm pkgconfig zlib-devel

安装相应软件包

可以建立目录存放软件包

1
mkdir ~/ffmpeg_sources

注意:以下软件包请有选择的安装,如果不需要请在./configure时移除或关闭相关选项

安装 yasm

Yasm是一个完全重写的NASM汇编。目前,它支持x86和AMD64指令集。 x264 和 FFmpeg都使用Yasm.

1
2
3
4
5
6
7
8
cd ~/ffmpeg_sources
git clone --depth 1 git://github.com/yasm/yasm.git
cd yasm
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make
make install
make distclean


1
2
3
4
5
6
7
8
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar zxf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make
make install
make distclean
. ~/.bash_profile

安装 x264

H.264 编码器。http://trac.ffmpeg.org/wiki/Encode/H.264

1
2
3
4
5
6
7
cd ~/ffmpeg_sources
git clone --depth 1 git://git.videolan.org/x264
cd x264
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static
make
make install
make distclean


1
2
3
4
5
6
7
wget ftp://ftp.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
tar jxf last_x264.tar.bz2
cd x264-snapshot-20130311-2245/
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-shared
make
make install
make distclean

需要ffmpeg配置./configure --enable-gpl --enable-libx264

注:由于libx264的code有GPL信息,所以,支持libx264时,需要–enable-gpl,ffmpeg在license方面还是比我们国内的人更重视

安装 x265

H.265/HEVC编码器.http://trac.ffmpeg.org/wiki/Encode/H.265

1
2
3
4
5
6
cd ~/ffmpeg_sources
hg clone https://bitbucket.org/multicoreware/x265
cd ~/ffmpeg_sources/x265/build/linux
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source
make
make install

需要ffmpeg配置./configure --enable-gpl --enable-libx265

安装 libfdk_aac

AAC音频编码器,Fraunhofer AAC library

1
2
3
4
5
6
7
8
cd ~/ffmpeg_sources
git clone --depth 1 git://git.code.sf.net/p/opencore-amr/fdk-aac
cd fdk-aac
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --enable-shared
make
make install
make distclean

需要ffmpeg配置--enable-libfdk_aac ,如果指定了--enable-gpl还要配置--enable-nonfree

安装 libmp3lame

Mp3音频编码器

1
2
3
4
5
6
7
wget http://iweb.dl.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
tar zxf lame-3.99.5.tar.gz
cd lame-3.99.5
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-shared --enable-nasm
make
make install
make distclean

需要ffmpeg配置--enable-libmp3lame

安装 libopus

Opus 音频编解码器

1
2
3
4
5
6
7
8
cd ~/ffmpeg_sources
git clone git://git.opus-codec.org/opus.git
cd opus
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install
make distclean


1
2
3
4
5
6
7
wget http://downloads.xiph.org/releases/opus/opus-1.0.3.tar.gz
tar zxf opus-1.0.3.tar.gz
cd opus-1.0.3
./configure --prefix="$HOME/ffmpeg_build" --enable-shared
make
make install
make distclean

需要ffmpeg配置--enable-libopus

安装 libogg库

libogg是Ogg流库,libtheora、libvorbis需要libogg库,speex需要它。

1
2
3
4
5
6
7
8
cd ~/ffmpeg_sources
curl -O http://downloads.xiph.org/releases/ogg/libogg-1.3.2.tar.gz
tar xzvf libogg-1.3.2.tar.gz
cd libogg-1.3.2
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install
make distclean

安装 libvorbis库

Vorbis音频编码器,需要libogg库。http://trac.ffmpeg.org/wiki/CompilationGuide/Centos#libogg

1
2
3
4
5
6
7
8
cd ~/ffmpeg_sources
curl -O http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.4.tar.gz
tar xzvf libvorbis-1.3.4.tar.gz
cd libvorbis-1.3.4
LDFLAGS="-L$HOME/ffmeg_build/lib" CPPFLAGS="-I$HOME/ffmpeg_build/include" ./configure --prefix="$HOME/ffmpeg_build" --with-ogg="$HOME/ffmpeg_build" --disable-shared
make
make install
make distclean

需要ffmpeg配置--enable-libvorbis

安装 libvpx

VP8/VP9视频编码器

1
2
3
4
5
6
7
cd ~/ffmpeg_sources
git clone --depth 1 http://git.chromium.org/webm/libvpx.git
cd libvpx
./configure --prefix="$HOME/ffmpeg_build" --enable-examples
make
make install
make clean

需要ffmpeg配置--enable-libvpx

编译安装 ffmpeg

1
2
3
4
5
6
7
8
cd ~/ffmpeg_sources
git clone --depth 1 git://source.ffmpeg.org/ffmpeg
cd ffmpeg
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --extra-cflags="-I$HOME/ffmpeg_build/include" --extra-ldflags="-L$HOME/ffmpeg_build/lib" --bindir="$HOME/bin" --pkg-config-flags="--static" --enable-gpl --enable-nonfree --enable-libfdk_aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265
make
make install
make distclean
hash -r

注:

  • 编译完成后ffmpeg,ffprobe,ffserver,lame,x264都可以使用。
  • 编译安装时可以参考Windows Build:http://ffmpeg.zeranoe.com/builds/
  • 如果还需要持续更新请保留ffmpeg_sources目录。

##其它可选软件包

安装 OpenCORE AMR

1
2
3
4
5
wget http://iweb.dl.sourceforge.net/project/opencore-amr/opencore-amr/opencore-amr-0.1.3.tar.gz
tar zxf opencore-amr-0.1.3.tar.gz
./configure --prefix="$HOME/ffmpeg_build" –enable-shared
make
make install

需要ffmpeg配置--enable-libopencore-amrnb 和/或 --enable-libopencore-amrwb

安装 VisualOn AAC

注:github的tag和master版均无法autoconf,用zeranoe版代替

1
2
3
4
5
6
7
wget wget http://ffmpeg.zeranoe.com/builds/source/external_libraries/vo-aacenc-0.1.3.tar.xz
xz -d vo-aacenc-0.1.3.tar.xz
tar xf vo-aacenc-0.1.3.tar
cd vo-aacenc-0.1.3
./configure --prefix="$HOME/ffmpeg_build"
make
make install

也可

1
wget http://iweb.dl.sourceforge.net/project/opencore-amr/vo-aacenc/vo-aacenc-0.1.3.tar.gz

需要ffmpeg配置--enable-libvo-aacenc

安装 VisualOn AMR-WB

注:github的tag和master版均无法autoconf,用zeranoe版代替

1
2
3
4
5
wget http://ffmpeg.zeranoe.com/builds/source/external_libraries/vo-amrwbenc-0.1.2.tar.xz
xz -d vo-amrwbenc-0.1.2.tar.xz
tar xf vo-amrwbenc-0.1.2.tar
cd vo-amrwbenc-0.1.2
./configure --prefix="$HOME/ffmpeg_build"

需要ffmpeg配置--enable-libvo-amrwbenc

安装 frei0r

先安装新版autoconf,这会覆盖掉系统中的旧版本哦

1
2
3
4
5
6
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
tar zxf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure --prefix="$HOME/ffmpeg_build"
make
make install

1
2
3
4
5
6
wget http://files.dyne.org/frei0r/releases/frei0r-plugins-1.4.tar.gz
tar zxf frei0r-plugins-1.4.tar.gz
cd frei0r-plugins-1.4
./configure --prefix="$HOME/ffmpeg_build"
make
make install

安装 gnutls

注意:/etc/ld.so.conf要加上lib64。并且不要使用yum remove gnutls gnutls-devel

首先安装nettle:

1
2
3
4
5
6
wget http://www.lysator.liu.se/~nisse/archive/nettle-2.5.tar.gz
tar zxf nettle-2.5.tar.gz
cd nettle-2.5
./configure --prefix="$HOME/ffmpeg_build" –enable-shared
make
make install

然后安装gnutls新版:

1
2
3
4
5
6
7
wget ftp://ftp.gnutls.org/gcrypt/gnutls/v3.1/gnutls-3.1.9.1.tar.xz
xz -d gnutls-3.1.9.1.tar.xz
tar xf gnutls-3.1.9.1.tar
cd gnutls-3.1.9.1
PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig:/usr/lib64/pkgconfig:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig ./configure –enable-shared
make
make install

安装 rtmpdump

1
2
3
4
5
wget http://rtmpdump.mplayerhq.hu/download/rtmpdump-2.3.tgz
tar zxf rtmpdump-2.3.tgz
cd rtmpdump-2.3
make
make install

安装 schroedinger

注:先安装好orc库

1
2
3
4
5
6
7
wget http://code.entropywave.com/download/orc/orc-0.4.17.tar.gz
tar zxf orc-0.4.17.tar.gz
cd orc-0.4.17
./configure --prefix="$HOME/ffmpeg_build"
make
make install
/sbin/ldconfig
1
2
3
4
5
6
wget http://diracvideo.org/download/schroedinger/schroedinger-1.0.11.tar.gz
tar zxf schroedinger-1.0.11.tar.gz
cd schroedinger-1.0.11
PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig:/usr/lib64/pkgconfig:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig ./configure
make
make install

安装 soxr

注:新版libsoxr貌似与当前ffmpeg不兼容,用旧版

1
2
3
4
5
6
7
8
9
10
yum install -y xz
wget http://hivelocity.dl.sourceforge.net/project/soxr/soxr-0.1.0-Source.tar.xz
xz -d soxr-0.1.0-Source.tar.xz
tar xf soxr-0.1.0-Source.tar
cd soxr-0.1.0-Source
./go
cd Release/
make
make install
/sbin/ldconfig

安装 speex

音频编码解码器。需要安装libogg库

1
yum install speex-devel


1
2
3
4
5
6
wget http://downloads.xiph.org/releases/speex/speex-1.2rc1.tar.gz
tar zxf speex-1.2rc1.tar.gz
cd speex-1.2rc1
./configure --prefix="$HOME/ffmpeg_build"
make
make install

ffmpeg ./configure 添加 --enable-libspeex

安装 libfreetype

字体呈现库,使用drawtext http://ffmpeg.org/ffmpeg-filters.html#drawtext-1

1
yum install freetype-devel

ffmpeg ./configure 添加 --enable-libfreetype

安装 libsdl库

1
2
3
4
5
6
7
wget http://www.libsdl.org/release/SDL-1.2.15.tar.gz
tar zxf SDL-1.2.15.tar.gz
cd SDL-1.2.15
./configure --prefix="$HOME/ffmpeg_build"
make
make install
/sbin/ldconfig

安装 theora

Theora视频编码器,需要需要libogg,libvorbis和libsdl库

1
2
3
4
5
6
wget http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.bz2
tar zjf libtheora-1.1.1.tar.bz2
./configure --prefix="$HOME/ffmpeg_build" --with-ogg="$HOME/ffmpeg_build" --disable-examples --enable-shared --disable-sdltest --disable-vorbistest
make
make install
make distclean

ffmpeg ./configure 添加 --enable-libtheora

安装 twolame

1
2
3
4
5
6
7
8
wget http://downloads.sourceforge.net/twolame/twolame-0.3.13.tar.gz
tar zxf twolame-0.3.13
.tar.gz
cd twolame-0.3.13
./configure --prefix="$HOME/ffmpeg_build"
make
make install
/sbin/ldconfig

安装 xvid

1
2
3
4
5
6
7
wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
tar zxf xvidcore-1.3.2.tar.gz
cd xvidcore
cd build/generic
./configure --prefix="$HOME/ffmpeg_build"
make
make install

更新FFmpeg及相关组件

更新依赖项

删除旧文件,更新依赖项

1
2
rm -rf ~/ffmpeg_build ~/bin/{ffmpeg,ffprobe,ffserver,lame,vsyasm,x264,yasm,ytasm}
# yum install autoconf automake cmake gcc gcc-c++ git libtool make mercurial nasm pkgconfig zlib-devel

更新 x264

1
2
3
cd ~/ffmpeg_sources/x264
make distclean
git pull

然后运行

1
2
3
4
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static
make
make install
make distclean

更新 x265

1
2
3
4
cd ~/ffmpeg_sources/x265
rm -rf ~/ffmpeg_sources/x265/build/linux/*
hg update
cd ~/ffmpeg_sources/x265/build/linux

然后运行

1
2
3
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source
make
make install

更新 libfdk_aac

1
2
3
cd ~/ffmpeg_sources/libfdk_aac
make distclean
git pull

然后运行

1
2
3
4
5
6
cd fdk-aac
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install
make distclean

更新 libvpx

1
2
3
cd ~/ffmpeg_sources/libvpx
make clean
git pull

然后运行

1
2
3
4
5
cd libvpx
./configure --prefix="$HOME/ffmpeg_build" --disable-examples
make
make install
make clean

更新FFmpeg

1
2
3
cd ~/ffmpeg_sources/ffmpeg
make distclean
git pull

然后运行

1
2
3
4
5
6
cd ffmpeg
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --extra-cflags="-I$HOME/ffmpeg_build/include" --extra-ldflags="-L$HOME/ffmpeg_build/lib" --bindir="$HOME/bin" --pkg-config-flags="--static" --enable-gpl --enable-nonfree --enable-libfdk_aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265
make
make install
make distclean
hash -r

还原所有本文安装内容

1
2
3
rm -rf ~/ffmpeg_build ~/ffmpeg_sources ~/bin/{ffmpeg,ffprobe,ffserver,lame,vsyasm,x264,yasm,ytasm}
# yum erase autoconf automake cmake gcc gcc-c++ git libtool mercurial nasm pkgconfig zlib-devel
hash -r

注:如安装其它软件包需要自行删除

自动安装shell

https://gist.github.com/gboudreau/f24aed76b4cc91bfb2c1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/sh

# Based on instructions found here: http://wiki.razuna.com/display/ecp/FFMpeg+Installation+on+CentOS+and+RedHat#FFMpegInstallationonCentOSandRedHat-InstallX264

if [ "`/usr/bin/whoami`" != "root" ]; then
echo "You need to execute this script as root."
exit 1
fi

cat > /etc/yum.repos.d/centos.repo<<EOF
[centos]
name=CentOS-6 – Base
baseurl=http://mirror.centos.org/centos/6/os/x86_64/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
enabled=1
priority=1
protect=1
includepkgs=SDL SDL-devel gsm gsm-devel libtheora theora-tools libdc1394 libdc1394-devel libraw1394-devel
EOF
rpm --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

rpm -Uhv http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
yum -y update

yum -y install glibc gcc gcc-c++ autoconf automake libtool git make nasm pkgconfig
yum -y install SDL-devel a52dec a52dec-devel alsa-lib-devel faac faac-devel faad2 faad2-devel
yum -y install freetype-devel giflib gsm gsm-devel imlib2 imlib2-devel lame lame-devel libICE-devel libSM-devel libX11-devel
yum -y install libXau-devel libXdmcp-devel libXext-devel libXrandr-devel libXrender-devel libXt-devel
yum -y install libogg libvorbis vorbis-tools mesa-libGL-devel mesa-libGLU-devel xorg-x11-proto-devel zlib-devel
yum -y install libtheora theora-tools
yum -y install ncurses-devel
yum -y install libdc1394 libdc1394-devel
yum -y install amrnb-devel amrwb-devel opencore-amr-devel

cd /opt
wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
tar xzvf xvidcore-1.3.2.tar.gz && rm -f xvidcore-1.3.2.tar.gz
cd xvidcore/build/generic
./configure --prefix="$HOME/ffmpeg_build" && make && make install

cd /opt
wget http://downloads.xiph.org/releases/ogg/libogg-1.3.1.tar.gz
tar xzvf libogg-1.3.1.tar.gz && rm -f libogg-1.3.1.tar.gz
cd libogg-1.3.1
./configure --prefix="$HOME/ffmpeg_build" --disable-shared && make && make install

cd /opt
wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.4.tar.gz
tar xzvf libvorbis-1.3.4.tar.gz && rm -f libvorbis-1.3.4.tar.gz
cd libvorbis-1.3.4
./configure --prefix="$HOME/ffmpeg_build" --with-ogg="$HOME/ffmpeg_build" --disable-shared && make && make install

cd /opt
wget http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.gz
tar xzvf libtheora-1.1.1.tar.gz && rm -f libtheora-1.1.1.tar.gz
cd libtheora-1.1.1
./configure --prefix="$HOME/ffmpeg_build" --with-ogg="$HOME/ffmpeg_build" --disable-examples --disable-shared --disable-sdltest --disable-vorbistest && make && make install

cd /opt
wget http://downloads.sourceforge.net/opencore-amr/vo-aacenc-0.1.2.tar.gz
tar xzvf vo-aacenc-0.1.2.tar.gz && rm -f vo-aacenc-0.1.2.tar.gz
cd vo-aacenc-0.1.2
./configure --prefix="$HOME/ffmpeg_build" --disable-shared && make install

yum -y remove yasm
cd /opt
wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
tar xzfv yasm-1.2.0.tar.gz && rm -f yasm-1.2.0.tar.gz
cd yasm-1.2.0
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" && make install
export "PATH=$PATH:$HOME/bin"

cd /opt
git clone http://git.chromium.org/webm/libvpx.git
cd libvpx
git checkout tags/v1.3.0
./configure --prefix="$HOME/ffmpeg_build" --disable-examples && make && make install

cd /opt
git clone git://git.videolan.org/x264.git
cd x264
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static && make install

export LD_LIBRARY_PATH=/usr/local/lib/:$HOME/ffmpeg_build/lib/
echo /usr/local/lib >> /etc/ld.so.conf.d/custom-libs.conf
echo $HOME/ffmpeg_build/lib/ >> /etc/ld.so.conf.d/custom-libs.conf
ldconfig

cd /opt
git clone git://source.ffmpeg.org/ffmpeg.git
cd ffmpeg
git checkout release/2.2
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig"
export PKG_CONFIG_PATH
./configure --prefix="$HOME/ffmpeg_build" --extra-cflags="-I$HOME/ffmpeg_build/include" --extra-ldflags="-L$HOME/ffmpeg_build/lib" --bindir="$HOME/bin" \
--extra-libs=-ldl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvpx --enable-libfaac \
--enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libvo-aacenc --enable-libxvid --disable-ffplay \
--enable-gpl --enable-postproc --enable-nonfree --enable-avfilter --enable-pthreads --arch=x86_64 && make install

# Test the resulting ffmpeg binary
cp $HOME/bin/ffmpeg /usr/bin/
ffmpeg -v

参考