« Home Automation with HomeBridge | Main | Upgrading my MacPro »
Automating Handbrake
By Derek | February 4, 2017
Last night I wrote a script to automate HandBrakeCLI.
I ended up needing to rip a DVD with many chapters that were originally individual movies, that I had foolishly lost the sources for. (Always keep backups of your source files!)
Generally, when you rip a DVD, you want the Title to be one movie file, with individual chapters. This script instead creates files for each chapter of a VOB Title.
My chapter-ripper.sh script can be found here:
https://github.com/derekmoyes/handbrake-automation
Keep reading for an explanation of the script, what it does, and how it does it.
So, I started out with some basic comments, describing who I am, and where to get the script:
# Derek Moyes
After that, I broke out the section with the variables that should be changed by anyone wanting to run the script.
### Fill in these variables ##################################################
videotitle=2
videochapters=53 ### Use the actual number of chapters
discname=OurVacationDisc1
storepath=/Users/yourusername/Movies ### No trailing slash
videotitle is the DVDs VOB Title, which includes chapters.
videochapters is how many chapters you want to rip.
discname is the name you want given to the output source directory.
storepath is where you want to store the output files.
If you were attempting to get this to work on Linux, you’d change the storepath variable to something like this:
storepath=/home/yourusername/Videos
or
storepath=/home/yourusername/Rips
I then include some variables that shouldn’t be changed, unless you know what you are doing.
### Dont change stuff below here #############################################
ripcounter=1
rippath=$storepath/zAutoRipping-$discname-t$videotitle
ripcounter is a variable that will be incremented so that HandBrake can rip the next chapter in sequence.
rippath is the concatenated name of the output file. It combines your storepath with the word zAutoRipping (so that you know a rip is still in progress, even if you aren’t looking at the terminal) and then includes the discname and videotitle variables. This makes your output directory name very unique, and on a Mac, this sorts that directory name down to the bottom of your Finder window, and theoretically out of your way.
The example, based on the variables above, would be:
/Users/yourusername/Movies/zAutoRipping-OurVacationDisc1-t1
Next, chapter-ripper will attempt to create the directory, if it’s not already created, that’s what the -p option does:
### Start the work #####################################################
mkdir -p $rippath
Now, chapter-ripper will finally get down to business.
### Rip the chapter files
while [ $ripcounter -le $videochapters ];
do
This starts a while loop. Essentially, as long as the counter ripcounter is less than or equal to -le the number of videochapters, the script will make another loop through, before ending. Looping (iteration) is a very important part of programming, and it’s what makes computers really, really good at what they do.
The next line builds the output filename.
ripname=$rippath/$discname-t$videotitle-ch$ripcounter.mp4
Remember that the directory name has already been built. Since the directory name doesn’t change throughout the process, it was built once, outside the loop. However, the output filename needs to change every time the loop runs again, therefore, it’s built inside the loop. Using the examples above, this name would end up as:
/Users/yourusername/Movies/zAutoRipping-OurVacationDisc1-t1-ch1.mp4
Let’s echo out some text to the console.
echo Ripping Title $videotitle Chapter $ripcounter to $ripname
This really just gives us a status about what we’re doing, as each loop scrolls past. One thing you’ll notice is that HandBrakeCLI outputs tons of text, so this output has a tendency to get lost in the scroll back. Personally, I think it’s more useful while troubleshooting, so I leave it in.
This next line is the meat of the ripper. It’s a carefully crafted HandBrakeCLI line that took me much longer to get right than I’d care to admit. Yeah, about 3 hours.
HandBrakeCLI --preset-import-gui -Z "Legacy/High Profile" -i VIDEO_TS -o $ripname -t $videotitle -c $ripcounter --all-audio
HandBrakeCLI is the name of the program.
–preset-import-gui allows me to use the High Profile preset.
-Z “Legacy/High Profile” -Z is shorthand for –preset, but when I converted my one-liner into a script, I had trouble getting HandBrakeCLI to recognize some of the double-dash commands, so I fell back to using the shorthand ones. I also had to include double quotes around Legacy/High Profile in order to get it to work inside the loop. This is the preset I’ve used for years, and it gives very good results. Most of my playback is local, although I do some streaming. Let’s just say it makes a standard DVD look great on my 70″ 4k set. It’s a really nice preset. Some day I’ll do a bunch of testing and see which of the newer presets (post 1.x) are comparable, but man, this one works well.
-i VIDEO_TS is the input folder.
-o $ripname is the final generated name of the output file.
-t $videotitle is the actual VOB Title you want to rip. It’s one of the variables you should set at the top of the program.
-c $ripcounter is the generated looping counter that allows us to rip each successive chapter.
–all-audio is the easy way out, trust me. You might get multiple audio tracks when you don’t need them, or your file might be a little larger than it would be otherwise, but seriously, figuring out the audio options on this beast of a program… is a pain. Essentially, when I was specifying options, I kept getting audio/video sync errors which would prevent the rip from completing.
That’s the summary of the HandBrakeCLI options. Feel free to call me an idiot for my choice of preset, or whatever. Also, feel free to explain in great detail why your method is better, just do both nicely.
Looping.
((ripcounter++))
done
sleep 5
((ripcounter++)) is the bash shell magic that increments your chapter counter. As long as the chapter counter number is smaller than the ripcounter the script will loop again.
done is the end of our do loop. Once we’re out of chapters, we’ll close the loop.
sleep 5 I like to have the script wait a few seconds after ripping is all complete, before running the post-rip cleanup.
Now, our script can complete.
### Clean up
zdonePath=$storepath/RipDone-$discname-t$videotitle
mv $rippath $zdonePath
zdonePath lets us change the directory name after the rip is complete. During the rip, the directory starts with the name zAutoRipping. After the rip, it changes to start with RipDone. The example using the variables included here would look like:
/Users/yourusername/Movies/RipDone-OurVacationDisc1-t1
Our scripts’ final task is to echo our completion status to the console.
echo Completed $discname
Completed OurVacationDisc1
I hope you enjoyed this walkthrough/tutorial. Let me know in the comments! If you have specific improvements to the script itself, create an Issue, or a PR over on GitHub.
Topics: Entertainment, Family, Me, Movies, Technology, TV | No Comments »
Comments
You must be logged in to post a comment.