古いLinuxでFFmpegをほぼ全自動でビルドする
最終更新日:2017/02/01

古いLinuxでFFmpegをほぼ全自動でビルドする方法をまとめました。開発環境のパッケージをインストールして、後はスクリプトを実行するだけでビルド&インストールまで行うことができます。最新のLinux環境で行う場合、「LinuxでFFmpegをほぼ全自動でビルドする(CentOS、Ubuntu等に対応)」の記事をご参照下さい。
ビルドするFFmpegは、libavcodecでサポートされている標準エンコーダの他に以下のエンコーダを追加します。
スポンサーリンク
目次
前置き(Introduction)
当初、最新のLinux環境で最新のFFmpegをビルドするスクリプトを「LinuxでFFmpegをほぼ全自動でビルドする(CentOS、Ubuntu等に対応)」の記事で公開していました。
しかし、Linux環境によってはgccやアセンブラのバージョンが古すぎてビルドできない事が増えてきました(特にCentOS 6.8とかCentOS 6.8とか……)。CentOS 6.8の場合、完全更新期限が2017年第2四半期です。本記事投稿時点では、まだ機能追加サポートは行われていますが、gccなどは古いままなので最新のソースコードには対応できなくなっています。
スクリプト自体に問題がないのに開発環境が古い事が原因でビルドできないのは、率直に言って各自でどうにかして欲しいのが本音です。前述のCentOS 6.8を利用している方からすると古いOS扱いするとは何事だと不快に思われるかもしれませんが、ご理解ください。
ですが、それでは困る人がいるのも事実なので、多少古いLinux環境でもビルドできるようにしようと考えたのが本記事の内容です。
元記事ではビルドに失敗した場合、遠慮無くコメント欄にて報告して下さいと書いていました。大変申し訳ありませんが、こちらの記事に関してはほぼサポートはしません。明らかにスクリプトに問題がある場合のみサポート対応します。
その他、注意事項は次の通りです。
- 64bit版(amd64)のLinux OSにのみ対応します。
- FFmpeg本体と各種ライブラリは固定バージョンをインストールします(個別変更可能)。
- 独自のディレクトリにインストールするので、既存環境を汚しません。
- アンイストールは簡単にできます。
開発環境構築(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 ・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)
並列コンパイルに対応しているので、3行目の”cpu_core=2”となっている箇所をビルド環境のCPUコア数に応じて変更して下さい。例えば、CPUコア数が4の時は”cpu_core=4”として下さい。
ビルドする機能を選択できるようにしています。4行目以降の”enable_xxx=1”の箇所を”=0”にするとビルド対象外になります。例えばVP8が不要であれば、6行目を”enable_vp8=0”としてください。
もしビルドに失敗した場合、19行目以降に指定しているソースコードのパッケージを変更してみて下さい。古いものに変えればビルドに通る可能性があります。また、不要なパッケージなら無効にして試して下さい。
以下がスクリプトです。ダブルクリックするとスクリプト全体を選択した状態になるので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
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=""
url_yasm="http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz"
url_x264="https://ftp.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20160815-2245-stable.tar.bz2"
url_x265="http://ftp.videolan.org/pub/videolan/x265/x265_2.0.tar.gz"
url_aac="http://downloads.sourceforge.net/opencore-amr/fdk-aac-0.1.4.tar.gz"
url_opus="http://downloads.xiph.org/releases/opus/opus-1.1.3.tar.gz"
url_libvpx="https://github.com/webmproject/libvpx/archive/v1.6.0.tar.gz"
url_ffmpeg="http://ffmpeg.org/releases/ffmpeg-3.1.2.tar.bz2"
url_autoconf="http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz"
url_lame="http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz"
url_ogg="http://downloads.xiph.org/releases/ogg/libogg-1.3.2.tar.gz"
url_theora="http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.bz2"
url_vorbis="http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.4.tar.gz"
print_error()
{
echo "error: $1"
}
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
case $url in
*libvpx*) dir=libvpx-${dir#v}
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
make install
make distclean
fi
echo "---------- build Yasm ----------"
cd $src_dir
run_wget $url_yasm
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
if [ $enable_h264 -eq 1 ]; then
echo "---------- build libx264 ----------"
cd $src_dir
run_wget $url_x264
./configure --prefix="$prefix_dir" --bindir="$prefix_dir/bin" --enable-static
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
run_wget $url_x265
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_wget $url_aac
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_wget $url_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
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_wget $url_libvpx
./configure --prefix="$prefix_dir" --disable-examples
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_wget $url_libass
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_wget $url_ffmpeg
./configure \
--prefix="$prefix_dir" --extra-cflags="-I$prefix_dir/include" \
--extra-ldflags="-L$prefix_dir/lib" \
--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
アンインストール(Uninstall)
アンインストールは以下のコマンドを実行して下さい。ディレクトリの削除のみでアンインストールが完了します。
# rm -f /usr/local/ffmpeg_build # rm -f ~/ffmpeg_sources
FFmpeg用エンコードスクリプト
当ブログでH.264のエンコードスクリプトを作成しました。良かったら使ってみて下さい。
