Skip to content

Instantly share code, notes, and snippets.

@captbrogers
Last active August 29, 2015 13:59
Show Gist options
  • Save captbrogers/10780201 to your computer and use it in GitHub Desktop.
Save captbrogers/10780201 to your computer and use it in GitHub Desktop.
Ubuntu install FFmpeg from source
#!/bin/bash
# Script Meta
SCRIPT_NAME="ffmpeg-installer"
VERSION="1.0.0"
BUILD_DATE="Wednesday 16 April 2014"
# Colors
txtRESET=$(tput sgr0)
txtBOLD=$(tput bold)
txtUNDER=$(tput sgr 0 1)
txtWHITE=${txtRESET}$(tput setaf 7)
txtRED=${txtUNDER}${txtBOLD}$(tput setaf 1)
txtGREEN=${txtUNDER}${txtBOLD}$(tput setaf 2)
txtYELLOW=${txtUNDER}${txtBOLD}$(tput setaf 3)
# Default directory paths
SOURCE_DIR='/opt/ffmpeg-source'
BUILD_DIR='/opt/ffmpeg-source/build'
BIN_DIR='/usr/local/bin'
LIB_DIR='/usr/local/lib'
LOG_DIR='/opt/ffmpeg-source/logs'
DOWNLOAD_ONLY=false
UNPACK_ONLY=false
SKIP_X11=false
SKIP_LAME=false
checkPKG () {
for i in "$@"
do
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' ${i}|grep "install ok installed")
printf "Checking for ${i}..."
if [ "install ok installed" == "$PKG_OK" ];
then
printf "${txtGREEN}OK!${txtRESET}"
if [ "${i}" != build-essential ]; then
printf " found at $(which ${i})\n"
else
printf "\n"
fi
else
printf "${txtRED}NOT FOUND${txtRESET}\n"
printf "Please install ${i} before trying again.\n"
exit 1
fi
done
}
checkUp () {
if [ $1 == yes ]; then
apt-get update && apt-get upgrade
elif [ $1 == y ]; then
apt-get update && apt-get upgradesudo
elif [ $1 == no ]; then
return 1
elif [ $1 == n ]; then
return 1
else
printf "Please use 'yes' or 'y' to perform an update/upgrade. 'no' or 'n' will continue installation.\n"
read -p "Do you want to perform an update/upgrade at this time? [y/n] "
checkUp $REPLY
fi
}
checkDirs () {
while [[ ${#} -gt 0 ]];
do
dir="$1"
name="$2"
if [[ "$dir" =~ ^-{1,2}.* ]]; then
# no flags/arguments/parameters lef, get out of the loop
continue
fi
printf "$name Dir..."
if [ ! -d $dir ]; then
printf "${txtYELLOW}not found${txtRESET}, creating...\n"
mkdir -p $dir
if [ -d $dir ]; then
printf "$dir ${txtGREEN}created successfully!${txtRESET}\n"
else
printf "${txtRED}ERROR${txtRESET}: Something went wrong with creating Source Dir. Exiting.\n"
exit 1
fi
else
printf "${txtGREEN}OK!${txtRESET} at $dir\n"
fi
shift;
shift;
done
}
downloadPkgs () {
cd $SOURCE_DIR
printf "Fetching yasm..."
curl -s -C - -o yasm-1.2.0.tar.gz http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
printf "${txtGREEN}done${txtRESET}\n"
printf "Fetching x264..."
curl -s -C - -o last_x264.tar.bz2 http://download.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
printf "${txtGREEN}done${txtRESET}\n"
printf "Fetching fdk-aac..."
curl -s -L -C - -o fdk-aac.zip https://github.com/mstorsjo/fdk-aac/zipball/master
printf "${txtGREEN}done${txtRESET}\n"
if ( $SKIP_LAME ); then
return
else
printf "Fetching lame..."
curl -s -L -C - -o lame-3.99.5.tar.gz http://sourceforge.net/projects/lame/files/lame/3.99/lame-3.99.5.tar.gz/download
printf "${txtGREEN}done${txtRESET}\n"
fi
printf "Fetching opus..."
curl -s -C - -o opus-1.1.tar.gz http://downloads.xiph.org/releases/opus/opus-1.1.tar.gz
printf "${txtGREEN}done${txtRESET}\n"
printf "Fetching libvpx..."
curl -s -C - -o libvpx-v1.3.0.tar.bz2 http://webm.googlecode.com/files/libvpx-v1.3.0.tar.bz2
printf "${txtGREEN}done${txtRESET}\n"
printf "Fetching ffmpeg..."
curl -s -C - -o ffmpeg-snapshot.tar.bz2 http://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
printf "${txtGREEN}done${txtRESET}\n"
chown root.root *
if ( $DOWNLOAD_ONLY ); then
printf "Downloads complete\n"
exit 0
else
unpackPkgs
fi
}
unpackPkgs () {
cd $SOURCE_DIR
printf "Unpacking yasm..."
tar zxf yasm-1.2.0.tar.gz
printf "${txtGREEN}done${txtRESET}\n"
printf "Unpacking x264..."
tar xjf last_x264.tar.bz2
printf "${txtGREEN}done${txtRESET}\n"
printf "Unpacking fdk-aac..."
unzip -q fdk-aac.zip
printf "${txtGREEN}done${txtRESET}\n"
if ( $SKIP_LAME ); then
return
else
printf "Unpacking lame..."
tar zxf lame-3.99.5.tar.gz
printf "${txtGREEN}done${txtRESET}\n"
fi
printf "Unpacking opus..."
tar zxf opus-1.1.tar.gz
printf "${txtGREEN}done${txtRESET}\n"
printf "Unpacking libvpx..."
tar xjf libvpx-v1.3.0.tar.bz2
printf "${txtGREEN}done${txtRESET}\n"
printf "Unpacking ffmpeg-snapshot..."
tar xjf ffmpeg-snapshot.tar.bz2
printf "${txtGREEN}done${txtRESET}\n"
chown -R root.root *
printf "Unpacking complete\n"
if ( $UNPACK_ONLY ); then
exit 0
else
runInstalls
fi
}
runInstalls () {
# Time to start configs, makes, and installs
cd $SOURCE_DIR/yasm-1.2.0/
printf "Configuring yasm..."
bash configure --prefix="${BUILD_DIR}" --bindir="${BIN_DIR}" > $LOG_DIR/yasm/configure.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make..."
make > $LOG_DIR/yasm/make.log 2>&1
make install > $LOG_DIR/yasm/install.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make disclean..."
make distclean > $LOG_DIR/yasm/distclean.log 2>&1
printf "${txtGREEN}done${txtRESET}\n"
cd $SOURCE_DIR/x264-snapshot*
printf "Configuring libx264..."
bash configure --prefix="${BUILD_DIR}" --bindir="${BIN_DIR}" --enable-static > $LOG_DIR/x264/configure.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make..."
make > $LOG_DIR/x264/make.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make install..."
make install > $LOG_DIR/x264/install.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make disclean..."
make distclean > $LOG_DIR/x264/distclean.log 2>&1
printf "${txtGREEN}done${txtRESET}\n"
cd $SOURCE_DIR/mstorsjo-fdk-aac*
printf "Configuring libfdk-aac..."
autoreconf -fiv > $LOG_DIR/fdk-aac/autoreconf.log 2>&1
bash configure --prefix="${BUILD_DIR}" --disable-shared > $LOG_DIR/fdk-aac/configure.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make..."
make > $LOG_DIR/fdk-aac/make.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make install..."
make install > $LOG_DIR/fdk-aac/install.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make disclean..."
make distclean > $LOG_DIR/fdk-aac/distclean.log 2>&1
printf "${txtGREEN}done${txtRESET}\n"
if ( $SKIP_LAME ); then
return
else
cd $SOURCE_DIR/lame-3.99.5
printf "Configuring lame..."
bash configure --prefix="${BUILD_DIR}" --enable-nasm --disable-shared > $LOG_DIR/lame/configure.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make..."
make > $LOG_DIR/lame/make.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make install..."
make install > $LOG_DIR/lame/install.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make disclean..."
make distclean > $LOG_DIR/lame/distclean.log 2>&1
printf "${txtGREEN}done${txtRESET}\n"
fi
cd $SOURCE_DIR/opus-1.1
printf "Configuring opus..."
bash configure --prefix="${BUILD_DIR}" --libdir="${LIB_DIR}" --disable-shared > $LOG_DIR/opus/configure.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make..."
make > $LOG_DIR/opus/make.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make install..."
make install > $LOG_DIR/opus/install.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make disclean..."
make distclean > $LOG_DIR/opus/distclean.log 2>&1
printf "${txtGREEN}done${txtRESET}\n"
cd $SOURCE_DIR/libvpx-v1.3.0
printf "Configuring libvpx..."
bash configure --prefix="${BUILD_DIR}" --disable-examples > $LOG_DIR/libvpx/configure.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make...(this can take a while, especially on a server)..."
make > $LOG_DIR/libvpx/make.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make install..."
make install > $LOG_DIR/libvpx/install.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make clean..."
make clean > $LOG_DIR/libvpx/clean.log 2>&1
printf "${txtGREEN}done${txtRESET}\n"
cd $SOURCE_DIR/ffmpeg/
printf "Configuring ffmpeg..."
PKG_CONFIG_PATH="$BUILD_DIR/lib/pkgconfig"
export PKG_CONFIG_PATH
# Check if x11-grab should be excluded
if ( $SKIP_X11 ); then
bash configure --prefix="${BUILD_DIR}" --bindir="${BIN_DIR}" --extra-cflags="-I${BUILD_DIR}/include" --extra-ldflags="-L${BUILD_DIR}/lib" --extra-libs="-ldl" --enable-gpl --enable-libass --enable-libfdk-aac --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree > $LOG_DIR/ffmpeg/configure.log 2>&1
else
bash configure --prefix="${BUILD_DIR}" --bindir="${BIN_DIR}" --extra-cflags="-I${BUILD_DIR}/include" --extra-ldflags="-L${BUILD_DIR}/lib" --extra-libs="-ldl" --enable-gpl --enable-libass --enable-libfdk-aac --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --enable-x11grab > $LOG_DIR/ffmpeg/configure.log 2>&1
fi
printf "${txtGREEN}done${txtRESET}\nRunning make...(this can take a while, especially on a server)..."
make > $LOG_DIR/ffmpeg/make.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make install..."
make install > $LOG_DIR/ffmpeg/install.log 2>&1
printf "${txtGREEN}done${txtRESET}\nRunning make disclean..."
make distclean > $LOG_DIR/ffmpeg/distclean.log 2>&1
printf "${txtGREEN}done${txtRESET}\n"
hash -r
exit 0
}
helpMenu () {
printf "FFmpeg Ubuntu Installer Script v${VERSION}\nBuild Date: ${BUILD_DATE}\nHelp Menu\n\n"
printf "example: ffmpeg-installer --source-dir /path/to/source --skip-lame --no-x11\n\n"
printf " ${txtBOLD}--source-dir${txtRESET} directory where source files will be stored\n default: ${SOURCE_DIR}\n\n"
printf " ${txtBOLD}--build-dir${txtRESET} directory where built files will be stored\n default: ${BUILD_DIR}\n\n"
printf " ${txtBOLD}--bin-dir${txtRESET} directory where executables will be stored\n default: ${BIN_DIR}\n\n"
printf " ${txtBOLD}--lib-dir${txtRESET} directory where library files will be stored\n warning: if you change this, make sure that what\n you use is in your \$PATH variable\n default: ${LIB_DIR}\n\n"
printf " ${txtBOLD}--skip-x11${txtRESET} do not enable x11-grab in ffmpeg's config\n default: x11-grab enabled\n\n"
printf " ${txtBOLD}--skip-lame${txtRESET} use this option to skip compiling of LAME if you\n already have LAME installed via package manager\n default: compile from source (not skipped)\n\n"
printf " ${txtBOLD}--download-only${txtRESET} download all the packages, but do not unpack them\n\n"
printf " ${txtBOLD}--unpack-only${txtRESET} unpack all the packages\n\n"
printf " ${txtBOLD}--install-only${txtRESET} skip all checks and go right to installation\n\n"
printf " ${txtBOLD}-h${txtRESET} print this help menu again, not sure why you'd do that again though....\n\n\n\n"
exit 0
}
################################################################
# #
# Kick it off #
# #
################################################################
# Must run as root
if [ $(whoami) != "root" ]; then
echo "You need to run this script with sudo access."
echo "Use 'sudo $SCRIPT_NAME' then enter your password when prompted."
exit 1
fi
# Check if they are calling for the help menu
if [[ "$1" =~ ^((-{1,2})([Hh]$|[Hh][Ee][Ll][Pp]))$ ]]; then
helpMenu; exit 0
else
# Otherwise, loop through the params and override defaults
while [[ $# -gt 0 ]]; do
opt="$1"
shift;
current_arg="$1"
if [[ "$current_arg" =~ ^-{1,2}.* ]]; then
# no flags/arguments/parameters lef, get out of the loop
continue
fi
case "$opt" in
"--source-dir" ) SOURCE_DIR="$1"; shift;;
"--build-dir" ) BUILD_DIR="$1"; shift;;
"--bin-dir" ) BIN_DIR="$1"; shift;;
"--lib-dir" ) LIB_DIR="$1"; shift;;
"--skip-x11" ) SKIP_X11=true; shift;;
"--skip-lame" ) SKIP_LAME=true; shift;;
"--download-only" ) DOWNLOAD_ONLY=true; printf "Only downloading packages...\n"; downloadPkgs; shift;;
"--unpack-only" ) UNPACK_ONLY=true; printf "Only unpacking downloaded packages...\n"; unpackPkgs; shift;;
"--install-only" ) printf "Beginning installs...\n"; runInstalls; shift;;
* ) printf "Invalid option: $opt\nusage: $SCRIPT_NAME\n (option parameters)\n$SCRIPT_NAME [--source-dir dir] [--build-dir dir] [--bin-dir dir] [--lib-dir dir]\n"
exit 1;;
esac
done
fi
printf "\n -----------------------------------------------------------------"
printf "\n | Logging enabled |"
printf "\n -----------------------------------------------------------------\n"
# Prompt for update and upgrade before continuing
#read -p "Do you want to perform an update and upgrade at this time? [y/n] "
#checkUp $REPLY
# Ensure that nasm, curl, and dev packages are installed
printf "Checking for dependencies..."
# Necessary regardless of desktop or server
checkPKG nasm curl build-essential libass-dev libfreetype6-dev libgpac-dev unzip autoconf automake libtool autotools-dev m4 libtheora-dev libvorbis-dev pkg-config texi2html zlib1g-dev
if ( ! $SKIP_X11 ); then
# Additional packages for desktop (i.e. x11 usage)
checkPKG libsdl1.2-dev libva-dev libvdpau-dev libx11-dev libxext-dev libxfixes-dev
fi
# Make sure all the directories exist before we start
printf "\nChecking if expected directories are in place...\n"
checkDirs $SOURCE_DIR "Source" $BUILD_DIR "Build" $BIN_DIR "Bin" $LIB_DIR "Lib" $LOG_DIR "Log" $LOG_DIR/yasm "Yasm Log" $LOG_DIR/x264 "x264 Log" $LOG_DIR/fdk-aac "fdk-aac Log" $LOG_DIR/lame "LAME Log" $LOG_DIR/opus "Opus Log" $LOG_DIR/libvpx "libvpx Log" $LOG_DIR/ffmpeg "FFmpeg Log"
# Go to where we will be storing all the source files
printf "Moving into $SOURCE_DIR\n"
cd $SOURCE_DIR
# Start downloading
downloadPkgs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment