#!/bin/bash -e

#####################################################
#
# Frameloom is a script that exports all frames from
# two videos and alternates them in
#
# Usage: bash ffmpeg/frames/frameloom.sh <input video1> <input video2> <output video>
#
#####################################################

TMPDIR=`mktemp -d`
i=0

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] ;
  then
    echo "Not enough arguments supplied"
fi

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

mkdir -p $TMPDIR

ffmpeg -i "$1" -compression_algo raw -pix_fmt rgb24 "${TMPDIR}export-%08d_a.tif"
ffmpeg -i "$2" -compression_algo raw -pix_fmt rgb24 "${TMPDIR}export-%08d_b.tif"

#rm -r $TMP

for filename in ${TMPDIR}*.tif; do
	value=`printf %08d $i`
	mv "$filename" "${TMPDIR}render_${value}.tif"
    i=`expr $i + 1`
done

ffmpeg -r 24 -f image2 -s 1280x720 -i "${TMPDIR}render_%08d.tif" -c:v prores_ks -profile:v 3 -y "$3"

rm -r "$TMPDIR"