LinuxでFFmpegをほぼ全自動でビルドする(CentOS、Ubuntu等に対応)
最終更新日:2019/03/04

LinuxでFFmpegをほぼ全自動でビルドする方法をまとめました。開発環境のパッケージをインストールして、後はスクリプトを実行するだけでビルド&インストールまで行うことができます。
ビルドするFFmpegは、libavcodecでサポートされている標準エンコーダの他に以下のエンコーダを追加します。
- ビデオエンコーダ(Video Encoder)
- オーディオエンコーダ(Audio Encoder)
- その他(etc)
申し訳ありませんが、CentOS 6.xは本記事では非対応とします。gccなどのバージョンが古く、最新のソースコードのビルドが通らなくなったためです。古いディストリビューションに関しては、「古いLinuxでFFmpegをほぼ全自動でビルドする」の記事をご参照下さい。
スポンサーリンク
目次
前置き(Introduction)
この情報は以下のFFmpegのWikiの情報を元にまとめました。
本記事を最後まで読んで、なおかつ上記Wikiのページを見ると分かりますが、スクリプトの内容はほぼ同じです。では、Wikiを見れば良いではないかと思われるでしょうが、Wikiの手順通りに実行すると少し問題が発生します。手順通りだとFFmpegを自分のホームディレクトリにインストールすることになり、自分以外使えないものになってしまうのです。
そこで、その困った状況を解消して誰でも使えるようにFFmpegをビルドしようと言うのが本記事の内容です。なお、複数のライブラリをインストールしますが、インストール済みのライブラリのパッケージとは競合しないので既存環境を汚しません。
本記事の内容は64bit版(amd64)のLinux OSで実行して下さい。32bit版(i386)ではビルドに失敗します。
開発環境構築(Development environment)
開発環境を構築するために必要なパッケージをインストールします。以下のコマンドを実行して下さい。UbuntuとCentoOSの場合で異なります。試してはいませんが、DebianとMintでもUbuntuと同じ方法でインストールできると思います。
・CentOS # yum -y install autoconf automake bzip2 cmake freetype-devel gcc gcc-c++ \ git wget libtool make mercurial nasm pkgconfig zlib-devel fribidi-devel \ fontconfig-devel libpng-devel bzip2-devel ・Ubuntu # sudo apt-get -y install autoconf automake build-essential \ cmake git 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 mercurial pkg-config \ texi2html zlib1g-dev
FFmpegビルド(FFmpeg build)
以下の手順でコマンドを実行します。スクリプトの内容は本記事の最後に掲載しています。
スクリプトはrootユーザーで実行して下さい(sudoでも可)。ユーザーIDをチェックしているので、root以外で実行すると失敗します。なお、デフォルトのインストール先は”/usr/local/ffmpeg_build”ディレクトリになります。
# vi ffmpeg_build.sh ・FFmpegビルドスクリプトの内容をコピペします。 ・ソースコードを展開するディレクトリは変数src_dirにて変更可能 ・実行ファイル、ライブラリのインストール先は変数prefix_dirにて変更可能 # chmod +x ffmpeg_build.sh # ./ffmpeg_build.sh
スクリプトの実行が完了したら、PATH環境変数にFFmpegのインストール先を追加して下さい。インストール先を変更していない場合、以下の手順内容になります。
・共通 $ export PATH=$PATH:/usr/local/ffmpeg_build/bin ・CentOS $ vi .bash_profile 変更前) PATH=$PATH:$HOME/.local/bin:$HOME/bin 変更後) PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/ffmpeg_build/bin ・Ubuntu $ vi .bashrc 以下をファイル末尾に追記 export PATH=$PATH:/usr/local/ffmpeg_build/bin
FFmpegビルドスクリプト(Build script)
FFmpegも関連するエンコーダも日々更新されるため、ビルドに失敗することがあります。その際はコメント欄から遠慮なくご連絡下さい。その際は必ずLinuxの種類とバージョンの情報、コンソールに表示されたエラーの内容をお知らせ頂くようお願いいたします。調査の上、回答いたします(ソースコードに不具合がある場合、スクリプトの修正で対処出来ないため、”ソースコードの不具合でビルドに失敗する場合の対処方法”を試してみて下さい)。
並列コンパイルに対応しているので、3行目の”cpu_core=2”となっている箇所をビルド環境のCPUコア数に応じて変更して下さい。例えば、CPUコア数が4の時は”cpu_core=4”として下さい。
ビルドする機能を選択できるようにしています。4行目以降の”enable_xxx=1”の箇所を”=0”にするとビルド対象外になります。例えばVP8が不要であれば、6行目を”enable_vp8=0”としてください。
以下がスクリプトです。ダブルクリックするとスクリプト全体を選択した状態になるのでCtrl+Cでコピーして下さい。
#!/bin/sh
cpu_core=2
enable_h264=1
enable_h265=1
enable_vp8=1
enable_aac=1
enable_mp3=1
enable_ogg=1
enable_opus=1
enable_ass=1
src_dir="$HOME/ffmpeg_sources"
prefix_dir="/usr/local/ffmpeg_build"
export PATH=$prefix_dir/bin:$PATH
export PKG_CONFIG_PATH="$prefix_dir/lib/pkgconfig"
enable_option=""
repo_yasm="git://github.com/yasm/yasm.git"
repo_x264="https://git.videolan.org/git/x264.git"
repo_x265="https://github.com/videolan/x265.git"
repo_aac="git://github.com/mstorsjo/fdk-aac"
repo_opus="git://github.com/xiph/opus.git"
repo_libvpx="https://chromium.googlesource.com/webm/libvpx.git"
repo_libass="https://github.com/libass/libass.git"
repo_ffmpeg="git://github.com/FFmpeg/FFmpeg"
url_nasm="https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/nasm-2.14.02.tar.bz2"
url_autoconf="http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz"
url_lame="https://sourceforge.net/projects/lame/files/lame/3.100/lame-3.100.tar.gz"
url_ogg="https://ftp.osuosl.org/pub/xiph/releases/ogg/libogg-1.3.3.tar.gz"
url_theora="https://ftp.osuosl.org/pub/xiph/releases/theora/libtheora-1.1.1.tar.bz2"
url_vorbis="https://ftp.osuosl.org/pub/xiph/releases/vorbis/libvorbis-1.3.6.tar.gz"
gcc_ver=`gcc -dumpversion | awk -F. '{print $1}'`
if [ ${gcc_ver} -ge 6 ]; then
# for x264, libvpx, ffmpeg
pic_enable="--enable-pic"
enable_option=${pic_enable}
fi
print_error()
{
echo "error: $1"
}
run_git()
{
repo="$1"
opt="$2"
dir=${repo##*/}
dir=${dir%.git}
if [ -d $dir ]; then
cd $dir && git pull
if [ $? -ne 0 ]; then
print_error "git pull $dir" && exit 1
fi
else
git clone $opt $repo
if [ $? -ne 0 ]; then
print_error "git clone $dir" && exit 1
fi
cd $dir
fi
}
run_wget()
{
url="$1"
file=${url##*/}
dir=${file%.tar.*}
if [ ! -e $file ]; then
wget $url
if [ $? -ne 0 ]; then
print_error "wget $file" && exit 1
fi
fi
case $file in
*.gz) tar xvzf $file ;;
*.bz2) tar xvjf $file ;;
esac
cd $dir
}
uid=`id | sed 's/uid=\([0-9]\+\)(.\+/\1/'`
if [ $uid -ne 0 ];then
print_error "not root user"
exit 1
fi
mkdir -p $src_dir
mkdir -p $prefix_dir
aconf_ver=`LANG=C autoconf -V | head -n 1 | sed -e "s/autoconf (GNU Autoconf) \([0-9]*\)\.\([0-9]*\)/\1\2/"`
if [ $aconf_ver -lt 269 ]; then
echo "---------- build autoconf ----------"
run_wget $url_autoconf
./configure --prefix="$prefix_dir" --bindir="$prefix_dir/bin"
make -j${cpu_core}
make install
make distclean
fi
echo "---------- build Yasm ----------"
cd $src_dir
run_git $repo_yasm "--depth 1"
autoreconf -fiv
./configure --prefix="$prefix_dir" --bindir="$prefix_dir/bin"
make -j${cpu_core}
if [ $? -ne 0 ]; then
print_error "make yasm" && exit 1
fi
make install
make distclean
echo "---------- build NASM ----------"
run_wget $url_nasm
./autogen.sh
./configure --prefix="$prefix_dir" --bindir="$prefix_dir/bin"
make -j${cpu_core}
if [ $? -ne 0 ]; then
print_error "make nasm" && exit 1
fi
make install
make distclean
if [ $enable_h264 -eq 1 ]; then
echo "---------- build libx264 ----------"
cd $src_dir
run_git $repo_x264 ""
./configure --prefix="$prefix_dir" --bindir="$prefix_dir/bin" --enable-static ${pic_enable}
make -j${cpu_core}
if [ $? -ne 0 ]; then
print_error "make libx264" && exit 1
fi
make install
make distclean
enable_option="${enable_option} --enable-libx264"
fi
if [ $enable_h265 -eq 1 ]; then
echo "---------- build libx265 ----------"
cd $src_dir
if [ -e "/etc/centos-release" ]; then
if [ -d "x265" ]; then
cd x265 && hg update
if [ $? -ne 0 ]; then
print_error "hg clone x265" && exit 1
fi
else
hg clone https://bitbucket.org/multicoreware/x265
if [ $? -ne 0 ]; then
print_error "hg update x265" && exit 1
fi
cd x265
fi
else
run_git $repo_x265 ""
fi
cd build/linux
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$prefix_dir" -DENABLE_SHARED:bool=off ../../source
make -j${cpu_core}
if [ $? -ne 0 ]; then
print_error "make libx265" && exit 1
fi
make install
make clean
enable_option="${enable_option} --enable-libx265"
fi
if [ $enable_aac -eq 1 ]; then
echo "---------- build libfdk_aac ----------"
cd $src_dir
run_git $repo_aac "--depth 1"
autoreconf -fiv
./configure --prefix="$prefix_dir" --disable-shared
make -j${cpu_core}
if [ $? -ne 0 ]; then
print_error "make libfdk_aac" && exit 1
fi
make install
make distclean
enable_option="${enable_option} --enable-libfdk-aac"
fi
if [ $enable_mp3 -eq 1 ]; then
echo "---------- build libmp3lame ----------"
cd $src_dir
run_wget $url_lame
./configure --prefix="$prefix_dir" --bindir="$prefix_dir/bin" --disable-shared --enable-nasm
make -j${cpu_core}
if [ $? -ne 0 ]; then
print_error "make libmp3lame" && exit 1
fi
make install
make distclean
enable_option="${enable_option} --enable-libmp3lame"
fi
if [ $enable_opus -eq 1 ]; then
echo "---------- build libopus ----------"
cd $src_dir
run_git $repo_opus ""
./autogen.sh
./configure --prefix="$prefix_dir" --disable-shared
make -j${cpu_core}
if [ $? -ne 0 ]; then
print_error "make libopus" && exit 1
fi
make install
make distclean
enable_option="${enable_option} --enable-libopus"
fi
if [ $enable_ogg -eq 1 ]; then
echo "---------- build libogg ----------"
cd $src_dir
run_wget $url_ogg
./configure --prefix="$prefix_dir" --disable-shared
make -j${cpu_core}
if [ $? -ne 0 ]; then
print_error "make libogg" && exit 1
fi
make install
make distclean
echo "---------- build libvorbis ----------"
cd $src_dir
run_wget $url_vorbis
LDFLAGS="-L$prefix_dir/lib" CPPFLAGS="-I$prefix_dir/include" ./configure --prefix="$prefix_dir" --with-ogg="$prefix_dir" --disable-shared
make -j${cpu_core}
if [ $? -ne 0 ]; then
print_error "make libvorbis" && exit 1
fi
make install
make distclean
enable_option="${enable_option} --enable-libvorbis"
echo "---------- build libtheora ----------"
cd $src_dir
run_wget $url_theora
./configure --prefix="$prefix_dir" --disable-shared --disable-examples
make -j${cpu_core}
if [ $? -ne 0 ]; then
print_error "make libtheora" && exit 1
fi
make install
make clean
enable_option="${enable_option} --enable-libtheora"
fi
if [ $enable_vp8 -eq 1 ]; then
echo "---------- build libvpx ----------"
cd $src_dir
run_git $repo_libvpx "--depth 1"
./configure --prefix="$prefix_dir" --disable-examples ${pic_enable}
make -j${cpu_core}
if [ $? -ne 0 ]; then
print_error "make libvpx" && exit 1
fi
make install
make clean
enable_option="${enable_option} --enable-libvpx"
fi
if [ $enable_ass -eq 1 ]; then
echo "---------- build libass ----------"
cd $src_dir
run_git $repo_libass "--depth 1"
autoreconf -fiv
./configure --prefix="$prefix_dir" --disable-shared
make -j${cpu_core}
if [ $? -ne 0 ]; then
print_error "make libass" && exit 1
fi
make install
make clean
enable_option="${enable_option} --enable-libass"
fi
echo "---------- build FFmpeg ----------"
cd $src_dir
run_git $repo_ffmpeg "--depth 1"
./configure \
--prefix="$prefix_dir" --extra-cflags="-I$prefix_dir/include" \
--extra-ldflags="-L$prefix_dir/lib" \
--extra-libs="-lm -lpthread" \
--bindir="$prefix_dir/bin" \
--pkg-config-flags="--static" \
--enable-gpl \
--enable-nonfree \
--enable-libfreetype \
$enable_option
make -j${cpu_core}
if [ $? -ne 0 ]; then
print_error "make ffmpeg" && exit 1
fi
make install
make distclean
hash -r
ソースコードの不具合でビルドに失敗する場合の対処方法
紹介しているスクリプトは最新のソースコードをダウンロードしています。そのため、ソースコードに不具合があり、ビルドに失敗することがあります。特にH.265はビルドに失敗する頻度が高いように思います。
1つ目の方法は、ビルドに失敗するエンコーダを除外する方法です。先程も説明しましたが、スクリプトの先頭の”enable_xxx=1”の箇所を”=0”としてビルドの対象から外して下さい。
enable_h265=0
2つ目の方法は、安定版のバージョンをダウンロードする方法です。ただし、以下の3つのエンコーダでのみ可能な方法です。
- H.265
- VP8
- Opus
以下はH.265のgitリポジトリのリリースバージョン(安定版)のタグを確認する方法です。
# cd ffmpeg_sources/x265/ # git tag -n 表示結果例) 2.6 release: Release notes for v2.6 2.7 release: Release notes for v2.7 2.8 release: Release notes for v2.8
上記例では”2.8”が最新の安定版のタグである事が分かります(実際にはもっと多くのタグが表示されます)。
次にスクリプトを以下の様に変更します(H.265の場合、170行目)。”-b”はgitのタグを指定するオプションです。
run_git $repo_x265 "-b 2.8"
後は全てのディレクトリを削除し、スクリプトを再実行します。
# rm -rf ~/ffmpeg_sources/ /usr/local/ffmpeg_build/ # ./ffmpeg_build.sh
各エンコーダのソースコードディレクトリとスクリプト変更行は以下の通りです。
| エンコーダ | ディレクトリ | 変更行 |
|---|---|---|
| H.265 | ffmpeg_sources/x265 | 170 |
| VP8 | ffmpeg_sources/libvpx | 273 |
| Opus | ffmpeg_sources/opus | 219 |
アンインストール(Uninstall)
アンインストールは以下のコマンドを実行して下さい。ディレクトリの削除のみでアンインストールが完了します。
# rm -rf /usr/local/ffmpeg_build # rm -rf ~/ffmpeg_sources
FFmpeg用エンコードスクリプト
当ブログでH.264のエンコードスクリプトを作成しました。良かったら使ってみて下さい。
スクリプトの更新履歴(ChangeLog)
- 2015年10月11日
- 並列コンパイルに対応
- 2015年10月14日
- 並列ビルドに対応
- ビルド対象のエンコーダを選択可能に変更
- エラー処理追加
- CentOS 6.xでopusのビルドに失敗する不具合を修正
- 2015年11月26日
- libass(字幕)に対応
- 2015年12月1日
- FFmpegのconfigureエラー対策:x265のダウンロードをstable対象に変更
- 2016年2月20日
- x264のリポジトリが変更されたため修正
- ソースコードの取得方法を全てgitに変更
- git clone済みの場合、git pullするように変更
- 2016年3月29日
- libtheoraに対応
- 関数処理を変更
- 2016年6月24日
- FFmpegとAACのレポジトリURLを変更
- CentOSのみx265のソースをMercurial(hg)による取得に戻した
- gitのバージョンが古く、一部コマンドの実行に失敗するため
- 2016年11月15日
- gcc 6.xに対応
- 2017年6月2日
- NASMのビルド処理を追加
- libx264のビルドにNASM最新バージョンが必要になったため
- 2017年10月19日
- FFmpeg本体のビルドエラーの修正
- lameのバージョンを3.100に更新
- 2017年11月30日
- nasmのパッケージファイルのバージョン修正
- 2018年2月12日
- nasmのパッケージファイルのバージョン指定修正
- liboggのパッケージファイルのバージョン指定更新
- libvorbisのパッケージファイルのバージョン指定更新
- 2019年3月4日
- nasmのパッケージバージョンを2.14.02に変更
- vorbisのパッケージバージョンを1.3.6に変更
- ogg、theora、vorbisのURLを変更
