#!/bin/sh
SRC=collections/_aulas
# SCRIPT=script/
SCRIPT=/var/www/src/AliceClass/script/
# TMP=tmp
TMP=$(mktemp -d)
ASSETS="$TMP/assets"
PARAM="scale=1920:1080: force_original_aspect_ratio=decrease: eval=frame,pad=1920:1080: -1:-1:color=black"


process_video () {
	NAME=$1
	AUTHOR=$2
	INPUT=$3
	AULA=$4
	OUTPUT=$5

	# no output path
	if [ -z "$OUTPUT" ]; then
		OUTPUT="$ASSETS/videos/${NAME}.mp4"
	fi

	THUMB_PATH=$(dirname $(dirname $OUTPUT))/thumb/${NAME}

	echo "########################################"
	echo "# Aula: ${AULA}"
	echo "# Author: ${AUTHOR}"
	echo "# Input: ${INPUT}"
	echo "# Src: ${SRC}"
	echo "# Dest: ${OUTPUT}"
	echo "# Name: ${NAME}"
	echo "# Script Path: ${SCRIPT}"
	echo "########################################"
	# exit

	# ####################################
	# Creating the thumbnail if does not exist
	# ####################################
	if test ! -f "${THUMB_PATH}.jpg" ; then
		ffmpeg -y -i "${INPUT}" -vframes 1 -an -s 400x222 -ss 30 "${THUMB_PATH}.jpg"
	fi

	# ####################################
	# if output exist, continue
	# ####################################
	if test -f "$ASSETS/videos/${NAME}.mp4" ; then
		continue
	fi

	# ####################################
	# Creating the intro clip
	# ####################################
	# sed "s/nomenome/$AUTHOR/g" ${SCRIPT}/intro.svg | sed "s/curso/${NAME}/g" > ${TMP}/tmp-intro.svg
	sed "s/nomenome/$AUTHOR/g; s/curso/${NAME}/g" ${SCRIPT}/intro.svg > ${TMP}/tmp-intro.svg
	ffmpeg  -y -loop 1 -i ${TMP}/tmp-intro.svg -i ${SCRIPT}/intro.wav -shortest ${TMP}/tmp-intro.mkv

	# ###################################################
	# Audio adjustment
	# ###################################################
	# Some possible options
	# -af loudnorm 
	# -af dynaudnorm=p=1
	# -af speechnorm=e=12.5:r=0.0001:l=1 
	# -af acompressor=threshold=0.089:ratio=9:attack=200:release=1000:makeup=2 
	ffmpeg -y -i "${INPUT}" -af dynaudnorm=p=1  ${TMP}/tmp-normalized.mkv

	# ####################################################
	# Wartermarking the video file
	# ####################################################
	sed "s/nomenome/$AUTHOR/g" ${SCRIPT}/watermark.svg | sed "s/curso/${NAME}/g" > ${TMP}/tmp-watermark.svg
	# add watermark to video
	ffmpeg -y -i ${TMP}/tmp-normalized.mkv -i ${TMP}/tmp-watermark.svg -i ${SCRIPT}/logo.png \
	    -filter_complex "[0][1]overlay=5:H-h-5:format=auto[bg];[bg][2]overlay=W-w-5:5:format=auto" -c:a copy ${TMP}/tmp-video.mkv

	# ####################################################
	# Concatenating videos
	# ####################################################

	echo "Concatenando videos..."			
	ffmpeg -y \
		-i ${TMP}/tmp-intro.mkv \
		-i ${TMP}/tmp-video.mkv \
		-i ${TMP}/tmp-credits.mkv \
		-filter_complex \
		"[0:v]$PARAM[v0]; [1:v]$PARAM[v1]; [2:v]$PARAM[v2]; [v0][0:a][v1][1:a][v2][2:a] concat=n=3:v=1:a=1 [v] [a]" \
		-map [v] -map [a] \
		-s hd720 \
		-vcodec libx264 \
		-pix_fmt yuv420p \
		-vsync 2 \
		"$OUTPUT"
}


# mkdir -p ./${TMP}

# ####################################
# Creating the credits clip
# ####################################
#if test ${TMP}/tmp-credits.mkv -ot ${SCRIPT}/credits.svg ; then
	echo "########################################"
	echo "# Gerando o tmp-credits.mkv"
	echo "########################################"
	ffmpeg -y -loop 1 -i ${SCRIPT}/credits.svg -i ${SCRIPT}/credits.wav -shortest ${TMP}/tmp-credits.mkv
#fi

# ####################################
# Creating dest dir
# ####################################
mkdir -p $ASSETS/thumbs
mkdir -p $ASSETS/videos


if [ $# -gt 1 ]; then
	NAME=$1
	AUTHOR=$2
	INPUT=$3
	AULA=$4
	OUTPUT=$5
	process_video "$NAME" "$AUTHOR" "$INPUT" "$AULA" "$OUTPUT"
	exit
fi

# for aula in ./${SRC}/*.md; do 
# 	# ####################################
# 	# Getting parameters from aulas
# 	# ####################################
#     NAME="$(basename "${aula}" .md)"
# 	AUTHOR=$(grep "^author: " "${aula}" | sed -e 's/author: //g')
# 	INPUT="${SRC}/${NAME}.mkv"
#
# 	process_video "$NAME" "$AUTHOR" "$INPUT"
# done

# rm -f ${TMP}
