#!/bin/bash -e

#####################################################
# 
# This script will export all frames of a video and
# then loop over them by running a processing sketch
# on each one. The example provided "example-sketch"
# will output the same image provided as input in its
# current state.
#
# Usage: bash processing/processing.sh <input video> <output video>
#
#####################################################

INPUT="${1}"
OUTPUT="${2}"

# 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

mkdir -p frames
mkdir -p processing-frames

ffmpeg -i "${INPUT}" frames/frame-%06d.png

FRAMES=frames/*.png

for frame in ${FRAMES}; do
	echo "Processing $frame with example-sketch..."

	# Run the processing sketch on every frame
	bash processing/example-sketch.sh "${frame}" "processing-${frame}"

done

ffmpeg -f image2 -i processing-frames/frame-%06d.png -c:v prores_ks -profile:v 3 "${OUTPUT}"

# clean up frames folder
rm -rf frames
rm -rf processing-frames
