Skip to content

Instantly share code, notes, and snippets.

@sfan5
Last active March 11, 2022 03:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfan5/8672767 to your computer and use it in GitHub Desktop.
Save sfan5/8672767 to your computer and use it in GitHub Desktop.
Builds ffmpeg with many libraries; easily configurable; can build mpv too
#!/bin/bash
set -e
ffbuild_conf=~/.config/ffbuild.conf
nobuild=0
noextract=0
usage () {
echo "Usage: ffbuild.sh [options]"
echo "-p <file> Set path to configuration file"
echo "-o Only download and extract sources"
echo "-e Do not download sources (uses existing files)"
exit 0
}
while [ $# -gt 0 ]; do
case "$1" in
-p)
shift
ffbuild_conf=$1
;;
-o)
nobuild=1
;;
-e)
noextract=1
;;
-h|--help)
usage
;;
*)
echo "Unrecognized argument: $1"
exit 1
;;
esac
shift
done
if [[ $nobuild -eq 1 && $noextract -eq 1 ]]; then
echo "Nothing to do?!"
exit 1
fi
if [ ! -f $ffbuild_conf ]; then
mkdir -p "$(dirname "$ffbuild_conf")"
cat <<MEOW >"$ffbuild_conf"
# 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 -c ^processor /proc/cpuinfo)
fi
vorbis_version=1.3.6
opus_version=1.3.1
flac_version=1.3.3
mpv_version=0.32.0
flake_version=0.11
get_git() {
if [ -d "$1" ]; then
cd "$1"
git pull
cd ..
else
local args=""
# arg 3 signals that shallow clone is not allowed
[[ $FAST_GIT_CLONE -eq 1 && -z "$3" ]] && args="--depth=200"
git clone $args "$2" "$1"
fi
}
get_tarball() {
if [ ! -d "$1" ]; then
local 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; }
"$SRC/$1/configure" "--prefix=$PRFX" $2
make -j$CORES
make install
cd ..
}
enabled() {
local tmp="ENABLE_$1"
tmp=${!tmp}
[[ -z "$tmp" || $tmp -eq 0 ]] && return 1
return 0
}
if [ $noextract -eq 0 ]; then
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 ..
fi
if [ $nobuild -eq 0 ]; then
echo "== Building"
mkdir -p target
PRFX=$PWD/target
SRC=$PWD/src
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-static"
flags="$flags --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 ..
fi
echo "Done."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment