#!/bin/bash


#A script to take a series of images from Wink, add pauses and turn them into a .mov file

#The script relies on the images being called 'test1.png, test2.png, test3.png,...' and being
#stored in a directory called /input

#The output images and movie are stored in a directory called /output

#Script's not that clever so pauses must be added in order, starting with the earliest frame and ending with the latest


#Find the no. of input images

totalframes=`ls input | wc -l`

echo "You have" $totalframes "frames in your video"


#Ask for the frame rate to allow caluclation of 'addframes' the number of copies of the frame needed
#to add a pause

echo "What frame rate did you record the flash movie at?"

read fpsrecorded

#Let user know why fps is set at 30

echo "The video will be encoded at 30 fps to match the .mov format"

fps=30


#the first frame to be moved without adding a pause will be the 0 frame

startunadframe=0


#Variable used to give a new number to output images to compensate for fact that new frames have been added
#to create pauses

newframeno=0



#Main loop for adding pauses and bits between pauses

for i in $*;

do


#Read in pause info from command line

winkpauseframe=$(echo $i | awk -F ':' '{print $1}')
pauselength=$(echo $i | awk -F ':' '{print $2}')


echo "wink pause frame is " $winkpauseframe
echo "pause length is" $pauselength


# Wink numbers frames 1 to x and output images 0 to (x-1) so pauseframe variable corrects for this

pauseframe=$((winkpauseframe-1))



#number of frames needed to add pause

addframes=$((fps*pauselength))


echo "start" $startunadframe "pause"  $pauseframe "end" $endunadframe


#Calculate the last frame before the pause (as demarquated by startunadframe and endunadframe), move frames and renumber
#If the next pause starts directly after the previous one then there are no before-pause frames to move
#This If statement identifies this situation

if [ $startunadframe -lt $pauseframe ]

then

endunadframe=$((pauseframe-1))


	#This loop moves before-pause frames to output folder

	#There may be a disparity between the recorded frame rate and the fps set by the video encoding (30 fps)
	#so we copy the non-pause frame by a number (multiplecopies) of times to make up for it


echo $fps $fpsrecorded

	# The multiplecopies variable tries to correct differences between the recorded frame rate and the frame rate of the
	# output .mov (set at 30 fps). It does this by copying non-paused frames a multiple of times equal to the 
	# .mov frame rate divided by the recorded frame rate 

	multiplecopies=$(awk -v var=$((fps/fpsrecorded)) 'BEGIN { printf"%0.f\n", var}')

		# The if loop ensures that multiple copies never goes below zero, which would cause the non-paused frames
		# not to be copied. If multiplecopies is 1 or greater, it increments multiple copies by 1, which experience has proven
		# produces good results

		if [ $multiplecopies -lt 1 ]

		then
	
		multiplecopies=1

		else

		multiplecopies=$((multiplecopies+1))

		fi

echo "multiple" $multiplecopies


	for (( i=$startunadframe; i<=$endunadframe; i++ ))

	do

		for (( j=1; j<=$multiplecopies; j++ ))

		do

		cp input/test${i}.png output/image${newframeno}.png


		#Increment newframeno so that next frame is correctly numbered
	
		newframeno=$((newframeno+1))	

		done

	done

fi


	#This loop copies the pauseframe by addframes times and renumbers the frames accordingly	

	for (( i=pauseframe; i<=((pauseframe+addframes)); i++ ))
	
	do

	cp input/test${pauseframe}.png output/image${newframeno}.png	


	#Increment newframeno so that next frame is correctly numbered

	newframeno=$((newframeno+1))

	done



#The start of the next possible before-pause frame will be the one following the previous pause, so set
#startunadframe accordingly

startunadframe=$((pauseframe+1))

done



#Let user know why the script hasn't finished

echo "Just need to copy over the last of the frames..."




#This loop copies all frames after the last pause and renumbers accordingly
#Again, the minus 1 in the totalframes argument compensates for the fact that
#the images run from 0 to (x-1) rather than 1 to x

for (( i=(($pauseframe+1)); i<=$((totalframes-1)); i++ ))

do

cp input/test${i}.png output/image${newframeno}.png

newframeno=$((newframeno+1))

done


#And let them know that it's all over

echo "All done!"




#And now to create the movie

echo "Do you want to create the video now? yes/no"
 
read govideo

if [ "$govideo" == "yes" ]

then

echo "What would you like to call the video?"

read videoname

#The frame rate for a .mov is 30 fps any other frame rate causes ffmpeg to drop some frames

/Applications/ffmpeg/ffmpeg -f image2 -i output/image%d.png -r $fps output/${videoname}.mov

fi

echo "Bye bye"

