# Script ffmpeg compile for Arch Linux and Gentoo
# Alvaro Bustos, thanks to Hunter.


# Create a temporary directory for sources.
SOURCES=$(mkdir ~/ffmpeg_sources)
cd ~/ffmpeg_sources

# Download the necessary sources.
# git clone --depth 1 git://git.videolan.org/x264
# wget ftp://ftp.videolan.org/pub/x264/snapshots/last_stable_x264.tar.bz2
wget ftp://ftp.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20180720-2245.tar.bz2
# hg clone https://bitbucket.org/multicoreware/x265
# git clone --depth 1 https://github.com/mstorsjo/fdk-aac
wget https://netcologne.dl.sourceforge.net/project/opencore-amr/fdk-aac/fdk-aac-0.1.6.tar.gz
# curl -L -O http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
wget https://pkgs.rpmfusion.org/repo/pkgs/free/lame/lame-3.99.5.tar.gz/84835b313d4a8b68f5349816d33e07ce/lame-3.99.5.tar.gz
# git clone http://git.opus-codec.org/opus.git
wget http://www.mirrorservice.org/sites/distfiles.macports.org/libopus/opus-1.2.1.tar.gz
wget http://downloads.xiph.org/releases/ogg/libogg-1.3.3.tar.gz
wget http://ftp.osuosl.org/pub/xiph/releases/vorbis/libvorbis-1.3.6.tar.gz
wget http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.gz
git clone --depth 1 https://chromium.googlesource.com/webm/libvpx.git
# git clone --depth 1 git://source.ffmpeg.org/ffmpeg
wget http://ffmpeg.org/releases/ffmpeg-3.4.2.tar.gz

# Unpack files
for file in `ls ~/ffmpeg_sources/*.tar.*`; do
tar -xvf $file
done

cd x264-*/
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static && make && make install && make distclean; cd ..

# cd x265/build/linux
# cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source && make && make install; cd ~/ffmpeg_sources

cd fdk-aac-*/
autoreconf -fiv && ./configure --prefix="$HOME/ffmpeg_build" --disable-shared && make && make install && make distclean; cd ..

cd lame-*/
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --disable-shared --enable-nasm && make && make install && make distclean; cd ..

cd opus-*/
autoreconf -fiv && ./configure --prefix="$HOME/ffmpeg_build" --disable-shared && make && make install && make distclean; cd ..

cd libogg-*/
./configure --prefix="$HOME/ffmpeg_build" --disable-shared && make && make install && make distclean; cd ..

cd libvorbis-*/
LDFLAGS="-L$HOME/ffmeg_build/lib" CPPFLAGS="-I$HOME/ffmpeg_build/include" ./configure --prefix="$HOME/ffmpeg_build" --with-ogg="$HOME/ffmpeg_build" --disable-shared && make && make install && make distclean; cd ..

cd libtheora-*/
./configure --prefix="$HOME/ffmpeg_build" --with-ogg="$HOME/ffmpeg_build" --disable-examples --disable-shared --disable-sdltest --disable-vorbistest && make && make install; cd ..

cd libvpx
./configure --prefix="$HOME/ffmpeg_build" --disable-examples && make && make install && make clean; cd ..

cd ffmpeg-*/
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --extra-cflags="-I$HOME/ffmpeg_build/include" --extra-ldflags="-L$HOME/ffmpeg_build/lib" --bindir="$HOME/bin" --pkg-config-flags="--static" --enable-gpl --enable-nonfree --enable-libfdk_aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libtheora && make && make install && make distclean && hash -r; cd ..

cd ~/bin
cp ffmpeg ffplay ffprobe ffserver lame /usr/local/bin

echo "FFmpeg Compilation is Finished!"






