Handbrake batch conversion using CLI shell script

I contributed to a Handbrake forum a while back. Thought you might find it useful too.

This is my evolving script for transcoding DVDs from MacTheRipper.

1) Save the file in a logical place (I keep it in the Movies Folder… same place I rip movies to). It will loop through all titles longer than X.

#!/bin/sh
# usage is ./thisScript.sh minimumDuration SomeDirContainingVIDEO_TS
# duration is set in minutes ("30" equals 30 minutes)
# specify the container directory for the VIDEO_TS directory
# example ./handbrake-longer-than.sh 30 24_SEASON6_D1
#set default path to HandBrakeCLI
PathToHandBrakeCLI=/some/path/to/HandBrakeCLI
if [ "${1}x" = "x" ]; then
echo "Minutes not provided"
exit
fi
if [ "${2}x" = "x" ]; then
echo "VIDEO_TS path not provided"
exit
fi
time=$1
export time
for i in $(find $2 -type d -name VIDEO_TS) ; do
for title in $($PathToHandBrakeCLI -t 0 -i $i -L 2>&1 | grep "has length" | sed 's/sec//' | sed 's/[()]//g' | awk '$8 > (60 * ENVIRON["time"]) { print $3 "-" $5 } ') ; do
#this names the title for the output file
titlenum=$(echo $title | cut -f 2 -d '-')
# you can change the preset or any other variables here
$PathToHandBrakeCLI -i $i -o ${2}-title${titlenum}-appletv.m4v --preset="AppleTV" -2 -T -P -t ${titlenum}
#output example: 24_SEASON6_D1-title1-appletv.m4v
done
 done

2) Within the script, set the path to your HandBrakeCLI app on line 9.

3) Don’t forget to run this before trying to execute it:
chmod 777 ./handbrake-longer-than.sh

4) Execute it like this:
./handbrake-longer-than.sh 30 24_SEASON6_D1

Where “30” is the shortest title you want to export and “24_SEASON6_D1” is the name of the folder containing your VIDEO_TS directory.

2 Comments

  1. Here is an updated script using your code 🙂

    #!/bin/sh
    # Run run_handbrake_cli.sh with minimumDuration directory logfile as parameters
    # Will find all *.img and *.iso files and convert all titles longer than minimumDuration
    # duration is set in minutes (“30” equals 30 minutes)
    #set default path to HandBrakeCLI

    if [ “${1}x” = “x” ]; then
    echo “Minutes not provided”
    exit 0
    fi
    if [ “${2}x” = “x” ]; then
    echo “Path not provided”
    exit 0
    fi
    if [ “${3}x” = “x” ]; then
    echo “Logfile not provided”
    exit 0
    fi

    for FILENAME in `find $2 -type f \( -name “*.img” -or -name “*.iso” \)`
    do
    MINDURATION=`expr 60 \* $1`
    HANDBRAKE_SCAN=”HandBrakeCLI -t 0 -i ${FILENAME} –min-duration ${MINDURATION}”
    for title in `${HANDBRAKE_SCAN} 2>&1 | grep “title [0-9]:”|cut -f1 -d’:’|cut -f3 -d’ ‘`
    do
    OUTPUT=”${FILENAME%.*}-title${title}.mp4″
    HANDBRAKE_RUN=”HandBrakeCLI -i ${FILENAME} –native-language SWE -o ${OUTPUT} -t ${title} -e x264 -q 20.0 -a 1,1 -E faac,copy:ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 –detelecine –decomb –loose-anamorphic -m -x b-adapt=2:rc-lookahead=50”
    ${HANDBRAKE_RUN} >> $3 2>&1
    done
    done

    Reply
  2. Good stuff.

    Some scripting suggestions:

    * Quote arguments to protect against spaces

    * Prefer a pipe to “while read” to a for-in over $() (or “), to protect against spaces

    * I suggest that the script should output a script that performs the work. This way, you can eyeball it for correctness before committing to a possibly long run that may go wrong, and it’s still easy to put into action – just pipe the output to sh.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *