Amazon Linuxにffmpegを入れて動画エンコーディングする

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

ffmpeg

動画のエンコーディングってコンピュータリソースを多く使う作業なことは知られています。少し前までは自作でハイスペックマシンを構築して、最速エンコーディング自慢するという散財が流行っていましたが、今はクラウドを活用しましょう。Amazon Elastic Transcoderというサービスもありますが、細かい設定をしたいといった場合にはffmpeg等を使うのもアリです。今回は、Amazon Linuxにffmpegの環境を構築したいと思います。

yumにリポジトリを追加

Amazon Linuxでは、便利なリポジトリが標準で用意されていますが、ffmpegは入っていませんので、リポジトリを追加します。

rpmforgeの追加

$ rpm -Uhv http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm

さらに、以下のようにcontos6のリポジトリを追加してyumからffmpegをインストールすることもできるのですが、インストールされるバージョンが最新版ではないことと、ffmpegの仕様が頻繁に変わるため、ソースから最新版をビルドしたいと思います。

$ sudo vi /etc/yum.repos.d/centos.repo
[base]
name=CentOS-6 - Base
mirrorlist=http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os
enabled=0
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

古いバージョンで良ければ以下のコマンドで終わりですが、今回はこれはやりません。インストール済みでしたらeraseしてください。

$ sudo yum install --enablerepo=base ffmpeg-devel

必要なライブラリをインストール

必要そうなライブラリをインストールします。

$ sudo yum update -y
$ sudo yum install SDL-devel a52dec a52dec-devel alsa-lib-devel faac faac-devel faad2 faad2-devel -y
$ sudo yum install freetype-devel giflib imlib2 imlib2-devel lame lame-devel libICE-devel libSM-devel libX11-devel -y
$ sudo yum install libXau-devel libXdmcp-devel libXext-devel libXrandr-devel libXrender-devel libXt-devel -y
$ sudo yum install id3tag-devel libogg libvorbis vorbis-tools mesa-libGL-devel mesa-libGLU-devel xorg-x11-proto-devel xvidcore xvidcore-devel zlib-devel -y
$ sudo yum install amrnb-devel amrwb-devel libtheora theora-tools -y
$ sudo yum install glibc gcc gcc-c++ autoconf automake libtool make git-core -y
$ sudo yum install ncurses-devel nasm libvpx* opencore-amr-devel  -y
$ sudo yum install yasm --enablerepo=epel

コーデックをインストール

ffmpegが利用するコーデックをインストールします。コーデックとは、エンコードやデコードをするソフトウェアのことです。

基本的なコーデック

wget http://www8.mplayerhq.hu/MPlayer/releases/codecs/all-20110131.tar.bz2
bunzip2 all-20110131.tar.bz2; tar xvf all-20110131.tar
 
sudo mkdir /usr/local/lib/codecs/
sudo mkdir /usr/local/lib64/codecs/
sudo cp all-20110131/* /usr/local/lib/codecs/
sudo cp all-20110131/* /usr/local/lib64/codecs/
sudo chmod -R 755 /usr/local/lib/codecs/
sudo chmod -R 755 /usr/local/lib64/codecs/

LibOgg

wget http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
tar xzvf libogg-1.3.0.tar.gz
cd libogg-1.3.0
./configure
make
sudo make install

Libvorbis

wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
tar xzvf libvorbis-1.3.3.tar.gz
cd libvorbis-1.3.3
./configure
make
sudo make install

Libtheora

wget http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.gz
tar xzvf libtheora-1.1.1.tar.gz
cd libtheora-1.1.1
./configure
make
sudo make install

Libvpx

git clone http://git.chromium.org/webm/libvpx.git
cd libvpx
./configure --enable-shared --extra-cflags=-fPIC
make
sudo make install

Aacenc

wget http://downloads.sourceforge.net/opencore-amr/vo-aacenc-0.1.2.tar.gz
tar xzvf vo-aacenc-0.1.2.tar.gz
cd vo-aacenc-0.1.2
./configure --enable-shared
make
sudo make install

X264

git clone git://git.videolan.org/x264.git
cd x264
./configure --enable-shared --extra-cflags=-fPIC --extra-asflags=-D__PIC__
make
sudo make install

ライブラリの登録

export LD_LIBRARY_PATH=/usr/local/lib/
sudo sh -c "echo /usr/local/lib > /etc/ld.so.conf.d/custom-libs.conf"
sudo ldconfig

ffmpegのコンパイル

準備が整いましたので、コンパイルしてインストールします。今回は、バージョン1.1.2を指定しました。必要に合わせてコンフィグのオプションを調整してください。

git clone git://source.ffmpeg.org/ffmpeg.git
cd ffmpeg
git checkout n1.1.2
./configure --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-shared --enable-gpl --enable-postproc --enable-nonfree --enable-avfilter --enable-pthreads --extra-cflags=-fPIC
make
sudo make install

ffmpegの動作確認

早速起動してみましょう。

$ ffmpeg 
ffmpeg version 1.1.2 Copyright (c) 2000-2013 the FFmpeg developers
  built on Feb 15 2013 17:53:25 with gcc 4.6.2 (GCC) 20111027 (Red Hat 4.6.2-2)
  configuration: --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-shared --enable-gpl --enable-postproc --enable-nonfree --enable-avfilter --enable-pthreads --extra-cflags=-fPIC
  libavutil      52. 13.100 / 52. 13.100
  libavcodec     54. 86.100 / 54. 86.100
  libavformat    54. 59.106 / 54. 59.106
  libavdevice    54.  3.102 / 54.  3.102
  libavfilter     3. 32.100 /  3. 32.100
  libswscale      2.  1.103 /  2.  1.103
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.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'

まとめ

今回は環境構築だけでしたが、次回は色々とエンコーディングをしてみたいと思います。あ、そうそう、Amazon Elastic Transcoderとの比較ですが、ざっくり費用感を考えると自前で環境作るよりもだいぶ安く済みますねwww。スポットインスタンスと組み合わせれば同額ぐらいかなぁ〜。ということで、AETはお得です!!

参考資料

Install FFmpeg on CentOS or RedHat EL 6.x

Compile FFmpeg on CentOS 6.0