HandBrake script for recursive batch convertion on Mac OS X (Linux too)

I wrote a script to recursively batch encode video files using HandBrake.

I needed to convert a bunch of AVIs into M4V so I wrote this script to convert all AVIs under a directory, with sub-directories, into M4V. The script will find all files of a given file type and then convert them (via HandBrake) into the specified file type. It’ll place the new, output file in the same directory as input file.

I’ve tested this to work on Mac OS X Lion but it should work on other versions of OSX and Linux too.

Here’s a link to my GitHub project that’ll always have the most up-to-date version:
https://github.com/derrekyoung/ScriptsAndUtils/blob/master/HandBrake-BatchConvert.sh

#!/bin/sh

###############################################################################
#
# Script to recursively search a directory and batch convert all files of a given
# file type into another file type via HandBrake conversion.
#
# To run in your environment set the variables:
#   hbcli - Path to your HandBrakeCLI
#
#   source_dir - Starting directory for recursive search
#
#   input_file_type - Input file type to search for
#
#   output_file_type  - Output file type to convert into
#
#
# Change log:
# 2012-01-08: Initial release.  Tested on Mac OS X Lion.
#
###############################################################################

hbcli=/Applications/HandBrakeCLI/HandBrakeCLI
source_dir="/Movies"
input_file_type="avi"
output_file_type="m4v"

echo "# Using HandBrakeCLI at "$hbcli
echo "# Using source directory "$source_dir
echo "# Converting "$input_file_type" to "$output_file_type

# Convert from one file to another
convert() {
	# The beginning part, echo "" | , is really important.  Without that, HandBrake exits the while loop.
	echo "" | $hbcli -i "$1" -o "$2" --preset="Universal";
}

# Find the files and pipe the results into the read command.  The read command properly handles spaces in directories and files names.
find $source_dir -name *.$input_file_type | while read in_file
do
        echo "Processing…"
	echo ">Input  "$in_file

	# Replace the file type
	out_file=$(echo $in_file|sed "s/\(.*\.\)$input_file_type/\1$output_file_type/g")
	echo ">Output "$out_file

	# Convert the file
	convert "$in_file" "$out_file"

	if [ $? != 0 ]
        then
            echo "$in_file had problems" >> handbrake-errors.log
        fi

	echo ">Finished "$out_file "\n\n"
done

echo "DONE CONVERTING FILES"
Advertisements

15 thoughts on “HandBrake script for recursive batch convertion on Mac OS X (Linux too)

  1. Might I suggest changing input_file_type="avi" to ‘input_file_type=$1’ and output_file_type="mp4" to output_file_type=$2.

    You could also change source_dir="Movies" to source_dir=$3.

    With these changes the command could be run with variable types and directories.
    Example: BatchConvert flv mp4 ~/Movies
    Will yield same results with custom directory and filetypes.

      1. You could use something like if [ -z $input_file_type ]
        then
        # Code to run if null
        else
        # Code to run if valid

  2. This looks great, my problem is that I want to have the encoded video files put into a folder called ‘output’ within the folder the original videos are located in, eg:

    /Movies/video.mp4
    /Movies/output/video.mp4

    How would I do this using your script?

    1. Forgot to mention that the output folder doesn’t exist, and I need the folder to be created within the script rather than doing it by hand.

  3. HI,

    this line works for me only after adding quotation marks
    find $source_dir -name *.$input_file_type

    find $source_dir -name “*.$input_file_type”

    thx

  4. #!/bin/bash
    # script convert your videos from any file to mp4(appletv preset)
    # this script deletes your original files.

    if test -z “$1”
    then
    echo “You must specified the container type.”
    echo “Example $0 mpg”
    echo “You may also specified the source and destination folder”
    echo “Example $0 mpg src_dir dest_dir”
    else
    if test -z “$2″
    then
    sourcedir=”/home/vlad/Videos/vid1”
    echo “The location of your $1 is $sourcedir”
    else
    sourcedir=$2
    fi
    if test -z “$3″
    then
    destdir=”/home/vlad/Videos”
    echo “your new videos are here: $destdir”
    else
    destdir=$3
    fi
    arr_preset=( ‘High Profile’ ‘Normal’ ‘AppleTV 2’ ‘Android High’ )
    cd “$sourcedir”
    echo -n “Select the output preset would you like to use:”
    echo -e “\n 0 : High Profile \n 1 : Normal \n 2 : AppleTV 2 \n 3 : Android High”
    presetname =${arr_preset[$(read)]}
    # start to convert files
    for i in *.$1; do
    HandBrakeCLI -i “$i” -o “$destdir/${i%.*}.mp4″ –preset=”$presetname”
    rm -f “$i”
    echo “——-”
    echo “$i ——has been converted and deleted”—-
    echo “——-”
    done
    cd “$destdir”
    echo “your converted files are here:”
    pwd
    ls -l

    fi

    1. want some suggestion i want to be able to use change the input and output via command line

      the above script.

  5. Derrek, this is fantastic!
    Extremely useful as an efficient way to back up video libraries automatically by creating proxies of everything it sees in it’s search!
    One question however, is there a way for the script to check if there is already a transcoded result file in the destination folder and skip to the next file?
    I use this as ongoing backup system and want to run the script daily, so at the moment it picks up all our 30tb+ of video and transcodes it all over again every time, not ideal in our situation!
    Thanks very much!

Comments are closed.