#!/bin/sh

##### FPSFinder
# This script checks for certain
# PAL/NTSC/FILM/BatshitInsane framerates
# and compensates for anything above 30fps
# with something mathmatically happier.

## QuickTime knows its own file's framerate a lot better than ffmpeg.
## ffmpeg sometimes thinks files are like, 600fps. Stupid ffmpeg.  
movfps=$1
ismov=$2

if [ "$ismov" == "mov" ]
	then
		if [ "$currfps" == "inf" ]
			then
			echo "$movfps"
			exit 0
		fi
		if [ "$movfps" -le 1 ]
			then
			echo "30"
			exit 0
		fi
		if [ "$movfps" -ge 30 ]
			then
			echo "30"
			exit 0
		fi
	echo "$movfps"
	exit 0
fi

## realfps is the full FPS as given by ffmpeg.
## currfps drops it down to a whole number for easier processing.
realfps=`cat /tmp/visualhub_dur | grep fps | rev | awk -F spf '{print $2}' | awk '{print $1}' | rev`
currfps=`echo $realfps | awk -F . '{print $1}'`

## Common non-good framerates.
## They'll get dropped to mathematically
## happier numbers - hopefully correctly-divisible ones.
if [ "$currfps" == "600" ]
	then
	echo "29.97"
	exit 0
fi
if [ "$currfps" == "59" ]
	then
	echo "29.97"
	exit 0
fi
if [ "$currfps" == "60" ]
	then
	echo "29.97"
	exit 0
fi
if [ "$currfps" == "50" ]
	then
	echo "25"
	exit 0
fi
if [ "$currfps" -ge 30 ]
	then
	echo "29.97"
	exit 0
fi
if [ "$currfps" -le 1 ]
	then
	echo "29.97"
	exit 0
fi
if [ "$currfps" == "" ]
	then
	echo "29.97"
	exit 0
fi

## ...if the original framerate's good, then use it.
echo $realfps
exit 0
