#!/bin/sh
# UPDATED: 01-16-2009
# HandBrakeCLI-batchHD.sh is a script to batch execute the HandBrakeCLI specifially for encoding m2ts files to m4v.
#############################################################################
# globals
# const global variables
scriptName=`basename "$0"`
skipDuplicates=1 # if this option is off,
# the new output files will overwrite existing files
E_BADARGS=65
# set the global variables to default
toolName="HandBrakeCLI"
# set path to HandBrakeCLI
toolPath="/Applications/$toolName"
# Set your HandBrakeCLI encode settings. Default is the AppleTV preset, 1280w, with quality set at 57%. For more info in setting parameters, visit http://handbrake.fr and read the CLI Guide Wiki.
toolArgs="-e x264 -q 0.56 -a 1,1 -E faac,ac3 -B 160,auto -R 48,Auto -6 dpl2,auto -f mp4 -4 -w 1280 -m -x level=30:cabac=0:ref=3:mixed-refs=1:bframes=6:weightb=1:direct=auto:no-fast-pskip=1:me=umh:subq=7:analyse=all"
# The input directory to process all m2ts files with a specified string appended to the filename. Default path is: ~/Movies/BatchRip.
inputSearchDir="$HOME/Movies/BatchRip"
# The output directory for all output files. Default is: ~/Movies/BatchEncode.
outputDir="$HOME/Movies/BatchEncode"
# Search String: This script's default search string is "_BD.m2ts". Any .m2ts file name with this search string (located in the input directory) will be processed with HandBrake.
searchString="_BD.m2ts"
#############################################################################
# functions
parseProcessInArgs()
{
if [ -z "$1" ]; then
return
fi
toolArgs=""
while [ ! -z "$1" ]
do
case "$1" in
-i) inputSearchDir=$2
shift ;;
--input) inputSearchDir=$2
shift ;;
-o) outputDir=$2
shift ;;
--output) outputDir=$2
shift ;;
--searchString) searchString=$2
shift ;;
*) toolArgs="$toolArgs $1" ;;
esac
shift
done
}
verifyFindCLTool()
{
# attempt to find the HandBrakeCLI if the script toolPath is not good
if [ ! -x "$toolPath" ];
then
toolPathTMP=`PATH=.:/Applications:/:/usr/bin:/usr/local/bin:$HOME:$PATH which $toolName | sed '/^[^\/]/d' | sed 's/\S//g'`
if [ ! -z $toolPathTMP ]; then
toolPath=$toolPathTMP
fi
fi
}
displayUsageExit()
{
echo "Usage: $scriptName [options]"
echo ""
echo " -h, --help Print help"
echo " -i, --input <string> Set input directory to process all DVDs in it (default: /Movies/BatchRip)"
echo " -o, --output <string> Set output directory for all output files (default: ~/Movies/BatchEncode)"
echo " --searchString <string> Set the text appended to the filename of the .m2ts/s to encode (default: _BD.m2ts)"
if [ -x "$toolPath" ];
then
echo " $toolName possible options"
mForkHelp=`$toolPath --help 2>&1`
mForkHelpPt=`printf "$mForkHelp" | egrep -v '( --input| --output| --help|Syntax: |^$)'`
printf "$mForkHelpPt\n"
else
echo " The options available to HandBrakeCLI except -o and -i"
if [ -e "$toolPath" ];
then
echo " ERROR: $toolName command tool is not setup to execute"
echo " ERROR: attempting to use tool at $toolPath"
else
echo " ERROR: $toolName command tool could not be found"
echo " ERROR: $toolName can be install in ./ /usr/local/bin/ /usr/bin/ ~/ or /Applications/"
fi
fi
echo ""
exit $E_BADARGS
}
#############################################################################
# MAIN SCRIPT
# initialization functions
verifyFindCLTool
parseProcessInArgs $*
# see if the output directory needs to be created
if [ ! -e $outputDir ]; then
mkdir -p "$outputDir"
fi
# sanity checks
if [[ ! -x $toolPath || ! -d $inputSearchDir || ! -d $outputDir || -z "$toolArgs" ]]; then
displayUsageExit
fi
# find all the m2ts files in the input directory tree
find "$inputSearchDir" -type f -name "*$searchString" | while read i ; do
# set the bd movie name to output
echo "$i"
dvdName=`basename "$i" $searchString`
# execute handbrakecli and skip output files that already exist.
if [[ ! -e $outputDir/"$dvdName".m4v || skipDuplicates -eq 0 ]];
then
$toolPath -i "$i" -o "$outputDir"/"$dvdName".m4v $toolArgs & wait
else
echo " Output file SKIPPED because it ALREADY EXISTS" & wait
fi
done