#!/bin/bash -e

#####################################################
#
# This script exports all frames of your video into
# a directory named "frames" and then runs a convert
# command which does edge detection on each frame.
# Then all frames are stitched back into a ProRes
# video.
#
# Usage: bash ffmpeg/frames/loop.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

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

FRAMES=frames/*.png

for frame in ${FRAMES}; do
	echo "Running edge detection on $frame..."
	convert "${frame}" -edge 1 "${frame}"
done

sleep 10

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

rm -rf frames