Skip to content

Instantly share code, notes, and snippets.

@sfan5 sfan5/ffbuild.sh
Last active Nov 28, 2018

Embed
What would you like to do?
Builds ffmpeg with many libraries; easily configurable; can build mpv too
#!/bin/bash
set -e
ffbuild_conf=~/.config/ffbuild.conf
if [ ! -f $ffbuild_conf ]; then
mkdir -p $(dirname "$ffbuild_conf")
cat <<MEOW >"$ffbuild_conf"
#!/bin/bash
# This file contains settings for the ffbuild.sh build script
## Configuration flags
CFGFLAGS_ffmpeg=""
CFGFLAGS_mpv=""
## FFmpeg feature set (libraries)
ENABLE_x264=1 # H.264 encoding (x264)
x264_bits=8 # higher bit encoding, values: 10
ENABLE_x265=1 # HEVC encoding (x265)
x265_bits=8 # higher bit encoding, values: 10, 12
ENABLE_vpx=0 # VP8 and VP9 encoding (libvpx)
vpx_bits=8 # higher bit encoding, values: 10, 12
ENABLE_aom=0 # AV1 decoding/encoding (libaom)
ENABLE_vorbis=0 # Vorbis encoding via libvorbis
ENABLE_opus=0 # Opus encoding (libopus)
ENABLE_fdkaac=1 # high quality AAC encoding (libfdk-aac)
ENABLE_flac=0 # FLAC encoding via libflac
## Standalone programs
ENABLE_mpv=1 # mpv
ENABLE_flake=0 # Flake encoder (FLAC)
## General settings
# By uncommenting the setting below you can override the number of jobs used for compiling
#CORES=2
# Faster cloning of git repositories (--depth=1)
FAST_GIT_CLONE=0
MEOW
echo "No configuration file found, creating one at $ffbuild_conf"
echo "You probably want to adjust some settings before running ffbuild.sh."
exit 1
fi
source $ffbuild_conf
if [ -z "$CORES" ]; then
CORES=$(grep processor /proc/cpuinfo | wc -l)
fi
vorbis_version=1.3.6
opus_version=1.3-beta
flac_version=1.3.2
mpv_version=0.28.2
flake_version=0.11
get_git() {
if [ -d "$1" ]; then
cd "$1"
git pull
cd ..
else
args=""
# arg 3 signals that shallow clone is not allowed
[[ $FAST_GIT_CLONE -eq 1 && -z "$3" ]] && args="--depth=1"
git clone $args "$2" "$1"
fi
}
get_tarball() {
if [ ! -d "$1" ]; then
arname=${2##*/}
[ -f "$arname" ] || wget "$2" -O "$arname"
mkdir "$1"
tar -xav -f $arname -C "$1" --strip-components=1
fi
}
build_autoconf() {
mkdir -p "$1"; cd "$1"
[ ! -f "$SRC/$1/configure" ] && (cd "$SRC/$1"; ./autogen.sh)
nice "$SRC/$1/configure" "--prefix=$PRFX" $2
nice make -j$CORES
make install
cd ..
}
enabled() {
local tmp="ENABLE_$1"
tmp=${!tmp}
return `[ $tmp -eq 1 ]`
}
echo "== Retrieving sources"
mkdir -p src; cd src
enabled x264 && echo "=== x264"
enabled x264 && get_git x264 http://git.videolan.org/git/x264.git
enabled x265 && echo "=== x265"
enabled x265 && get_git x265 https://github.com/videolan/x265.git "x"
enabled vpx && echo "=== libvpx"
enabled vpx && get_git libvpx https://chromium.googlesource.com/webm/libvpx
enabled aom && echo "=== libaom"
enabled aom && get_git libaom https://aomedia.googlesource.com/aom/
enabled vorbis && echo "=== libvorbis"
enabled vorbis && get_tarball libvorbis http://downloads.xiph.org/releases/vorbis/libvorbis-$vorbis_version.tar.xz
enabled opus && echo "=== opus"
enabled opus && get_tarball opus http://downloads.xiph.org/releases/opus/opus-$opus_version.tar.gz
enabled fdkaac && echo "=== fdk-aac"
enabled fdkaac && get_git fdk-aac https://github.com/mstorsjo/fdk-aac.git
enabled flac && echo "=== flac"
enabled flac && get_tarball flac http://downloads.xiph.org/releases/flac/flac-$flac_version.tar.xz
enabled mpv && echo "=== mpv"
enabled mpv && get_tarball mpv https://github.com/mpv-player/mpv/archive/v$mpv_version.tar.gz
enabled flake && echo "=== flake"
enabled flake && get_tarball flake http://prdownloads.sourceforge.net/flake-enc/flake-$flake_version.tar.bz2
echo "=== ffmpeg"
get_git ffmpeg https://github.com/FFmpeg/FFmpeg.git
cd ..
echo "== Building"
mkdir -p target
export PRFX=$PWD/target
export SRC=$PWD/src
export BLD=$PWD/build
export PKG_CONFIG_PATH=$PRFX/lib/pkgconfig
export LD_LIBRARY_PATH=$PRFX/lib
export PATH=$PRFX/bin:$PATH
mkdir -p build; cd build
enabled x264 && echo "=== x264"
enabled x264 && build_autoconf x264 "--bit-depth=$x264_bits --enable-shared --enable-pic --disable-lavf --disable-swscale"
if enabled x265; then
echo "=== x265"
mkdir -p x265; cd x265
bitopts=""
if [ $x265_bits -eq 8 ]; then
bitopts="-DHIGH_BIT_DEPTH=OFF"
elif [ $x265_bits -eq 10 ]; then
bitopts="-DHIGH_BIT_DEPTH=ON -DMAIN12=OFF"
elif [ $x265_bits -eq 12 ]; then
bitopts="-DHIGH_BIT_DEPTH=ON -DMAIN12=ON"
else
echo "Invalid x265_bits"
exit 1
fi
cmake "$SRC/x265/source" -DNATIVE_BUILD=ON -DCMAKE_INSTALL_PREFIX=$PRFX $bitopts
make -j$CORES
make install
cd ..
fi
if enabled vpx; then
echo "=== libvpx"
bitopts=""
if [ $vpx_bits -eq 8 ]; then
:
elif [[ $vpx_bits -eq 10 || $vpx_bits -eq 12 ]]; then
bitopts="--enable-vp9-highbitdepth"
else
echo "Invalid vpx_bits"
exit 1
fi
build_autoconf libvpx "--enable-shared --enable-runtime-cpu-detect $bitopts"
fi
if enabled aom; then
echo "=== libaom"
mkdir -p libaom; cd libaom
cmake "$SRC/libaom" \
-DCMAKE_INSTALL_PREFIX=$PRFX \
-DENABLE_{EXAMPLES,DOCS}=0 -DBUILD_SHARED_LIBS=1
make -j$CORES
make install
sed '/^Libs:/ s|$| -lpthread|' $PRFX/lib/pkgconfig/aom.pc -i # lol
cd ..
fi
enabled vorbis && echo "=== libvorbis"
enabled vorbis && build_autoconf libvorbis
enabled opus && echo "=== opus"
enabled opus && build_autoconf opus
enabled fdkaac && echo "=== fdk-aac"
enabled fdkaac && build_autoconf fdk-aac
enabled flac && echo "=== flac"
enabled flac && build_autoconf flac
#
echo "=== ffmpeg"
flags="$CFGFLAGS_ffmpeg --enable-shared --disable-debug"
enabled x264 && flags="$flags --enable-libx264 --enable-gpl"
enabled x265 && flags="$flags --enable-libx265 --enable-gpl"
enabled vpx && flags="$flags --enable-libvpx"
enabled aom && flags="$flags --enable-libaom"
enabled vorbis && flags="$flags --enable-libvorbis"
enabled opus && flags="$flags --enable-libopus"
enabled fdkaac && flags="$flags --enable-libfdk-aac --enable-nonfree"
build_autoconf ffmpeg "$flags"
#
if enabled mpv; then
echo "=== mpv"
pushd "$SRC/mpv"
[ -f waf ] || ./bootstrap.py
./waf configure $CFGFLAGS_mpv --prefix=$PRFX
./waf build -j$CORES
./waf install
popd
fi
if enabled flake; then
echo "=== flake"
pushd "$SRC/flake"
./configure --disable-debug --prefix=$PRFX
make
make install
popd
fi
cd ..
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.