#!/bin/bash -e

#####################################################
# 
# This script will export your video to frames and 
# use the application "primitive" to reproduce the
# image with polygonal primitive shapes. Then the
# converted frames are p
#
# The modes available for the -m argument are:
# 0=combo
# 1=triangle
# 2=rect
# 3=ellipse
# 4=circle
# 5=rotatedrect
# 6=beziers
# 7=rotatedellipse
# 8=polygon
# 
# primitive repo : https://github.com/fogleman/primitive
#
# Usage: bash ffmpeg/frames/primitive.sh <input video> <output video>
#
#####################################################

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

MODE=1
NUMBER=100

# 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 "Processing $frame with primitive..."
	# Run the "primitive" application on every frame
	primitive -i "${frame}" -o "${frame}" -n ${NUMBER} -m ${MODE}
done

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

rm -rf frames