#!/bin/bash
# usage is ./handbrakeencode.sh /path/to/folder
# written by mcmusic http://videoscripts.wordpress.com
#  if you use this as a 'files newer than' script then add it to crontab with:
# echo "0 0 * * 6 root /path/to/thisfile/handbrakeencode.sh /folder/withfiles" >> /etc/crontab
# to run every saturday at midnight

# type in your path to handbrakecli
hbpath=/usr/bin
# key in your file extension for files to process
ext=mkv
#key in your target file extension
outext=mp4
# key in your query
query=--preset="Apple TV"


# clean up incoming naming
O=$IFS
IFS=$(echo -en "\n\b")
# create some time vars for logging
DATE=$(date)

###########logging
# log it part 11 - this are commented out, uncomment if you want logging, change the paths to suit your own use
#echo "starting batch process at $DATE" >> /root/uw/ipodlog.txt
#rm -f /root/uw/encodecomplete
#touch /root/uw/encodein.progress
#echo "Starting encoding" | /bin/mail -s "Encoding Started" someemail@someemail.com -- -r "fromemail@fromemail.com"
# create a log of files to be done this is based on using the script in its 'newer than mode'
#for i in $(find $@ -mtime -6 -name '*.avi') ; do
#echo "$i" >> /root/uw/ipodlog.txt
#done
##########logging

# Simple progress report subroutine
progressReport()
{
file1=`find . -name *.avi | wc -l|sed 's/ //g'`
file2=`find . -name *.mkv | wc -l|sed 's/ //g'`
file3=`find . -name *.mp4 | wc -l|sed 's/ //g'`
total=`expr $file1 + $file2 + $file3`
mp4per=$((file3 * 100))
percen=`expr $mp4per / $total`

log1="You have encoded "
log2="$file3 out of $total files."
log3="$log1""$log2"

echo $log3

dia1="You have encoded $percen"
dia2="% of your files."
dia3="$dia1""$dia2"

echo $dia3
}

# do the processing
# this one for all files in folder
for i in $(find . -name "*.$ext") ; do
# uncomment this one to process only files in last seven days last seven days
# for i in $(find $@ -mtime -6 -name '*.avi') ; do
#path="$(dirname $i)"

#Print an onscreen progress report
progressReport

# this makes sure the file hasn't been taken out of the queue
if [ -f "$i" ]; then

# split up the incoming name into file and source folder

path=`dirname "$i"`
filename=`basename "$i" .$ext`

#####useful echo statments for testing comment them in if you don't need them or simply delete
#echo filename now is $filename
#echo directory will be "$path"
#echo output file will be "$path/$filename.$outext"
# log it 2
#echo "now going to encode $filename.$outext" >> /root/uw/ipodlog.txt
#echo Now I can process my encode
########################################


# now run the file renaming for processing file & encode
# This forces HandbrakeCLI to begin encoding the file it just renamed.
echo "Converting $i to $filename.$outext"
chmod 777 $i
mv $i $path/$filename.processing && $hbpath/HandBrakeCLI -i "$path/$filename.processing" -o "$path/$filename.$outext" "$query"

# this echo just in for testing
#echo $hbpath/HandBrakeCLI -i "$path/$filename.processing" -o "$path/$filename.$outext" "$query"

# now take the processing file and delete file
chmod 777 $path/$filename.processing
mv $path/$filename.processing $path/$filename.delete

else

# This is for files that it looks for, but have been taken out of the queue
echo "$i is no longer in Queue, Moving on..."

fi

############more logging should you need it
# log it 3
#echo "Finished encoding $filename.$outext on $DATE" >> /root/uw/ipodlog.txt

# next file
done