#!/bin/bash -e

#####################################################
#
# This script will create a 10 second title and
# overlay it on a video with a blend mode:
#
# addition
# grainmerge
# and
# average
# darken
# difference
# grainextract
# divide
# dodge
# freeze
# exlusion
# extremity
# glow
# hardlight
# hardmix
# heat
# lighten
# linearlight
# multiply
# multiply128
# negation
# normal
# or
# overlay
# pheonix
# pinlight
# reflect
# screen
# softlight
# subtract
# vividlight
# xor
#
# Usage: bash ffmpeg/filter/title.sh <title text> <input video> <output video> <blendmode> "invert" (optional)
#
#####################################################


W=720
H=480

TEXT="${1}"
INPUT="${2}"
OUTPUT="${3}"
BLENDMODE="${4}"

# Check if output file extension is .mov
if [[ "${OUTPUT}" != *".mov" ]]; then
	echo "Please use an .mov extension on your output file argument"
	exit 1
fi

if [ "${BLENDMODE}" == "" ]; then
	BLENDMODE="multiply"
fi

if [ "${5}" == "invert" ]; then
	convert -size ${W}x${H} \
		-background white \
		-fill black \
		-gravity Center \
		-weight 700 \
		-pointsize 100 \
		label:"${TEXT}" \
		title.png
else
	convert -size ${W}x${H} \
		-background black \
		-fill white \
		-gravity Center \
		-weight 700 \
		-pointsize 100 \
		label:"${TEXT}" \
		title.png
fi

ffmpeg -y -loop 1 \
	-framerate 24 \
	-f image2 \
	-i title.png \
	-s ${W}x${H} \
	-c:v prores_ks \
	-profile:v 3 \
	-t 10 \
	"title.mov"

ffmpeg -i "${INPUT}" -i "title.mov" -filter_complex "[0][1]blend=${BLENDMODE}" "${OUTPUT}"

# Clean up by removing the image and title video
rm title.png
rm title.mov