#!/usr/bin/env bash
set -e

SAVEDIR="$(pwd)/"
mkdir -p "${SAVEDIR}"

renderAudio() {
    filename="$1"
    bitrate="$2"
    if [ ! -f "${filename}_audio_${bitrate}.m4a" ]; then
	ffmpeg -y -i "${inputfile}" -c:a aac -ac 2 -ab "${bitrate}k" -vn "${filename}_audio_${bitrate}.m4a"
    fi
}

renderVideo() {
    filename="$1"
    bitrate="$2"
    height="$3"
    if [ ! -f "${filename}_${height}_${bitrate}.mp4" ]; then
#	ffmpeg -y -i "${inputfile}" -an -c:v libx264 -x264opts 'keyint=25:min-keyint=25:no-scenecut' \
#            -movflags frag_keyframe+empty_moov -g 25 -b:v ${bitrate}k -vf "scale=-2:${height}" -f mp4 -pass 1 -y /dev/null
	ffmpeg -y -i "${inputfile}" -an -c:v libx264 -x264opts 'keyint=25:min-keyint=25:no-scenecut' \
	    -movflags frag_keyframe+empty_moov -g 25 -b:v ${bitrate}k -vf "scale=-2:${height}" -f mp4 "${filename}_${height}_${bitrate}.mp4"
    fi

}

for inputfile in $@
do
    fname=$(basename "${inputfile}") # fullname of the file
    fname="${fname%.*}" # name without extension

    echo "Converting \"${inputfile}\" to multi-bitrate video in MPEG-DASH"

    renderAudio "${SAVEDIR}/${fname}" 96
    renderAudio "${SAVEDIR}/${fname}" 48

    renderVideo "${SAVEDIR}/${fname}" 1500 540
    renderVideo "${SAVEDIR}/${fname}" 0500 540
    renderVideo "${SAVEDIR}/${fname}" 0300 270
    renderVideo "${SAVEDIR}/${fname}" 0200 180

    ffmpeg -i "${SAVEDIR}/${fname}_540_0500.mp4" -i "${SAVEDIR}/${fname}_audio_96.m4a" \
           -acodec copy -vcodec copy \
           "${fname}_stream.mp4"
    MP4Box -inter 500 "${fname}_stream.mp4"

    MP4Box -dash 2000 -min-buffer 3000 -frag 2000 -rap -out "${fname}_mp4.mpd" \
	"${SAVEDIR}/${fname}_540_1500.mp4" \
	"${SAVEDIR}/${fname}_540_0500.mp4" \
	"${SAVEDIR}/${fname}_270_0300.mp4" \
	"${SAVEDIR}/${fname}_180_0200.mp4" \
	"${SAVEDIR}/${fname}_audio_96.m4a" \
	"${SAVEDIR}/${fname}_audio_48.m4a"
#	"${SAVEDIR}/${fname}_360_0400.mp4"
#	"${SAVEDIR}/${fname}_audio_32.m4a"

#    rm 	"${SAVEDIR}/${fname}_540_1500.mp4" "${SAVEDIR}/${fname}_540_0500.mp4" \
#	"${SAVEDIR}/${fname}_360_0400.mp4" "${SAVEDIR}/${fname}_270_0300.mp4" \
#	"${SAVEDIR}/${fname}_180_0200.mp4" \
#	"${SAVEDIR}/${fname}_audio_96.m4a" "${SAVEDIR}/${fname}_audio_48.m4a" "${SAVEDIR}/${fname}_audio_32.m4a"


done

