#!/bin/bash
# Compilador de ffmepeg para centos 7.
# Zaratdev
# https://zarat.dev

bindir="${HOME}/bin"

run_ffmpeg() {
    # Dependencies
    yum -y install autoconf automake bzip2 bzip2-devel cmake freetype-devel gcc gcc-c++ git libtool make pkgconfig zlib-devel;

    # Dir
    mkdir ~/ffmpeg_sources

    # NASM
    cd ~/ffmpeg_sources || exit;
    curl -O -L https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.bz2;
    tar xjvf nasm-2.15.05.tar.bz2;
    cd nasm-2.15.05 || exit;
    ./autogen.sh;
    ./configure --prefix="$HOME/ffmpeg_build" --bindir="$bindir";
    make;
    make install;

    # Yasm
    cd ~/ffmpeg_sources || exit;
    curl -O -L https://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz;
    tar xzvf yasm-1.3.0.tar.gz;
    cd yasm-1.3.0 || exit;
    ./configure --prefix="$HOME/ffmpeg_build" --bindir="$bindir";
    make;
    make install;

    # libx264
    cd ~/ffmpeg_sources || exit;
    git clone --branch stable --depth 1 https://code.videolan.org/videolan/x264.git;
    cd x264 || exit;
    PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$bindir" --enable-static;
    make;
    make install;

    # libx265
    cd ~/ffmpeg_sources || exit;
    git clone --branch stable --depth 2 https://bitbucket.org/multicoreware/x265_git;
    cd ~/ffmpeg_sources/x265_git/build/linux || exit;
    cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source;
    make;
    make install;

    # libfdk_aac
    cd ~/ffmpeg_sources || exit;
    git clone --depth 1 https://github.com/mstorsjo/fdk-aac;
    cd fdk-aac || exit;
    autoreconf -fiv;
    ./configure --prefix="$HOME/ffmpeg_build" --disable-shared;
    make;
    make install;

    # libmp3lame
    cd ~/ffmpeg_sources || exit;
    curl -O -L https://downloads.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz;
    tar xzvf lame-3.100.tar.gz;
    cd lame-3.100 || exit;
    ./configure --prefix="$HOME/ffmpeg_build" --bindir="$bindir" --disable-shared --enable-nasm;
    make;
    make install;

    # libopus
    cd ~/ffmpeg_sources || exit;
    curl -O -L https://archive.mozilla.org/pub/opus/opus-1.3.1.tar.gz;
    tar xzvf opus-1.3.1.tar.gz;
    cd opus-1.3.1 || exit;
    ./configure --prefix="$HOME/ffmpeg_build" --disable-shared;
    make;
    make install;

    # libvpx
    cd ~/ffmpeg_sources || exit;
    git clone --depth 1 https://chromium.googlesource.com/webm/libvpx.git;
    cd libvpx || exit;
    ./configure --prefix="$HOME/ffmpeg_build" --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=yasm;
    make;
    make install;

    # FFmpeg
    cd ~/ffmpeg_sources || exit;
    curl -O -L https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2;
    tar xjvf ffmpeg-snapshot.tar.bz2;
    cd ffmpeg || exit;
    PATH="$bindir:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \
    --prefix="$HOME/ffmpeg_build" \
    --pkg-config-flags="--static" \
    --extra-cflags="-I$HOME/ffmpeg_build/include" \
    --extra-ldflags="-L$HOME/ffmpeg_build/lib" \
    --extra-libs=-lpthread \
    --extra-libs=-lm \
    --bindir="$bindir" \
    --enable-gpl \
    --enable-libfdk_aac \
    --enable-libfreetype \
    --enable-libmp3lame \
    --enable-libopus \
    --enable-libvpx \
    --enable-libx264 \
    --enable-libx265 \
    --enable-nonfree;
    make;
    make install;
    ffmpeg;
}

set_new_bindir() {
    while true; do
        read -rp "Nuevo path [ejemplo: /usr/bin]: " new_bindir
        case $change_bindir in
            * )
                if [ ! -d "$new_bindir" ]; then
                    echo "No existe el directorio :/"
                else
                    bindir=$new_bindir
                    run_ffmpeg
                    break
                fi
            ;;
                
        esac
    done
}

clear;
cat << "EOF"
 _____                _         __  __                            
/ _  / __ _ _ __ __ _| |_      / _|/ _|_ __ ___  _ __   ___  __ _ 
\// / / _` | '__/ _` | __|____| |_| |_| '_ ` _ \| '_ \ / _ \/ _` |
 / //\ (_| | | | (_| | ||_____|  _|  _| | | | | | |_) |  __/ (_| |
/____/\__,_|_|  \__,_|\__|    |_| |_| |_| |_| |_| .__/ \___|\__, |
                                                |_|         |___/ 


EOF

while true; do
    read -rp "FFmpeg se compilara en ${HOME}/bin, Quieres continuar? [y/n]: " change_bindir
    case $change_bindir in
        [Yy]* ) run_ffmpeg; break;;
        [Nn]* ) set_new_bindir; break;;
        * ) echo "Si o No pendejo!.";;
    esac
done