Script to run handbrake recursively through a folder tree
Handbrake is a fantastic tool for (among other uses), converting videos into mobile formats. It is extremely easy to use and can usually get a video properly converted in just a few clicks. To get Videos encoded for use on my phone, I use the “iPhone & iPod Touch” preset. It will convert your video to .mp4 as well as scale it down to a consumable size. This format will work on any smartphone I’ve seen.
My issue is that when converting a folder full of videos, I don’t want to have to use the gui to add a bunch of videos to the queue one by one. Its a very clicky process. This simple bash script will walk handbrake through all the files in a selected folder (and its subfolders). This uses the find command to traverse recursively through a directory structure. It will place the transcoded file in the same folder as its source and change its extension to “.mp4″.
Usage: handbrakefolder.sh [FOLDER]
Run handbrake on all the files contained in [FOLDER]. (the current directory by default)
#!/bin/bash
#
# Change this to specify a different handbrake preset. You can list them by running: "HandBrakeCLI --preset-list"
#
PRESET="iPhone & iPod Touch"
if [ -z "$1" ] ; then
TRANSCODEDIR="."
else
TRANSCODEDIR="$1"
fi
find "$TRANSCODEDIR"/* -type f -exec bash -c 'HandBrakeCLI -i "$1" -o "${1%\.*}".mp4 --preset="$PRESET"' __ {} \;
Save that into a .sh file like “handbrakefolder.sh” and grant it the execute permission (chmod +x handbrakefolder.sh).
Thanks to Vinnie and http://mywiki.wooledge.org/UsingFind for their help in this.
Now you can simply execute this script against a folder containing your video files. like this:
For a folder structure like this:
Videos
-> show1
vid1.avi
-> show2
-> season 1
ep1.avi
ep2.avi
-> season 2
ep1.avi
ep2.avi
-> show3
vid1.avi
vid2.avi
hostname% handbrakefolder.sh Videos
...
....
...
When done, it will look like this:
Videos
-> show1
vid1.avi
vid1.mp4
-> show2
-> season 1
ep1.avi
ep1.mp4
ep2.avi
ep2.mp4
-> season 2
ep1.avi
ep1.mp4
ep2.avi
ep2.mp4
-> show3
vid1.avi
vid1.mp4
vid2.avi
vid2.mp4
This just gets better and better!
The bonus is that link to http://mywiki.wooledge.org/UsingFind now, maybe, I can make more sense out of these shell scripts!
So what’s next Jacob? Rewriting output file names to drop the .avi?
Cheers,
Dave
Dave,
I just realized that I posted a new comment instead of replying.
Here is a modified version of the script that strips the .AVI extension from the output file. Using this will be left ORIGINALFILE.AVI and ORIGINALFILE.MP4 (instead of ORIGINALFILE.AVI.MP4) Enjoy!
Vinnie
My handbrake is stiff.
you’re welcome?
Great script, I am trying to modify the output folder and am hitting issues.
Here is what I put.
find “$TRANSCODEDIR”/* -type f -exec bash -c ‘/Applications/HandBrakeCLI -i “$1″ -o /Volumes/Terror/iFlicks_Watch_Folder/ –preset=”$PRESET”‘ __ {} \;
The script runs but handbrake throws an error saying that no muxer was selected. I think the issue is something to do with my “” and thefroe handbrakeCLI is not seeing the preset flag.
Any idea?
Thanks.
Phil
Yes, could also use help on this as well. How to change the output director? Has to do with the $1 being the full path including filename. I just want to get the filename itself and output to a directory I specify, rather than in the folder of the original file. Makes things tidier.
first.. thanks! I will give this a try. I’ve got 7TB of ripped DVD’s (legitimate) and I need to free up some space.
second.. “it’s” is a contraction for ‘it is’. The correct spelling should be “its” with no apostrophe when you are using a possessive pronoun similar to his or hers.
Thanks!
Thx for the spellcheck.
It’s
all fixed.
I hope the script helps you. If you run into any problems, let me know.
I just found this script and decided it solves all my problems…then realized it sure looks like it might be for a Linux environment…and I’m running XP.
I also don’t know enough to know if that’s the case. I >COULD< eventually set up another PC that runs Linux (not the family PC), but is this script workable in some scenario under Windows XP?
Thank you
Yes, this is a BASH shell script. If you’re stuck with windows XP, you could try running the script inside a cygwin environment. Give it a shot and report back. I’ll be happy to help you through it.