Tuesday, September 10, 2013

Logans Lessions #2: Scripting Handbrake in Windows.

Logans Lessions #2: Scripting Handbrake in Windows

The Scenario

You want to rip some of your movies to your computer. You want to do this to either 1) watch the
movies on something that doesn't have an optical drive, or 2) you want to make your entire DVD/BR
archive available without needing to dig out the physical DVD's each time you play them.

With this in mind, how exactly do you go from ripping a DVD onto the computer, to viewing it on any of these devices? Its not very hard actually, although there is no really streamlined way of doing it as of yet (its not built into any OS that I'm aware of).

First you need to rip the DVD to your hard drive. I would suggest an ISO format (the literal image
file of the DVD, exactly the same space on disk, for exactly the same quality and features). There
are programs out there that can actually play these iso files, and if you have no storage limitations, then this is the easiest way to store your DVD's. If this is what you want, you simply stop here.

For the rest of us that have to worry about storage space, or want a more portable and
generally more useful file you might want something else. An ISO of a typical DVD is close to 8GB.
Depending on the size of your movie collection that could be a huge issue. And no portable device I
know plays ISO.

So, if you want to keep the original source file quality, but want the resulting file to be smaller
and more widely playable what do you do? You encode that file into something else. The tool that
many people use to do this encoding/compression is called "handbrake". Handbrake is a free program, and it allows you to take an ISO file or a folder full of VOB files (the other way to get everything off of a DVD), and compress it into much smaller space.

Using Handbrake is pretty simple, with the hardest part being understanding all of the options that
are available to you. Compressing movies is also a pretty time consuming task. A typical movie can
take anywhere from 20 minutes to 2 hrs depending on the options that you select, and the processing
power of your computer.

However, if you have a large Movie library ready, it can be really tedious to compress each movie
one at a time. Luckly, the people at handbrake have already thought about that, and have created a
command line version of the tool that can be scripted. For those that don't know, scripting is a
generic term for taking something that we do on the computer, and then automating it in an attempt
to reduce the amount of human interaction that is required. In this case, we'll be automating the
process of compressing movies in one folder, and then outputing them to an output folder.

Handbrake scripting on a windows machine

Since I'm a bit of a windows guy (as are most of the people out there... by numbers at least), and
when I searched about on the internet for examples of a someone scripting handbrake on windows, the results were remarkably thin, I thought I would do the internet a favor and share with everyone the little script I made to make my life easier.

Powershell is my language of choice, and if you're not already familiar with the old school windows
command prompt (and even if you are), I'd suggest learning powershell as it is rapidly gaining traction as the way to do things in a windows world. I'm about even in my knowledge of cmd and powershell, but I prefer to work with powershell if given the choice.

I'll provide the template script, and explain some of it as I go. Please keep in mind that I am not
a one of microsofts "scripting guys", nor do I proclaim myself as any sort of expert at scripting.
Instead, I'm just sharing something I made in my spare time, and hope that it may be of use to
someone.

Also, as you look at the script in detail, you'll see that I'm creating .mkv files. I prefer using them, as I have found better results with them on my surround sound setup than with MP4. However, MP4 is much more widely accepted and played (almost everything will play MP4 files), so if you want MP4, then all you have to do is change the file ending here in my script.


#Script created by ocnlogan, as part of Logans Learnings #1: Scripting handbrake in windows.

#Creating a variable to store the path to all of the source files that will be encoded. 
#Change the text below (inside the quotes) to reflect where your files are located
$inputpath = "e:\isos" 

#getting the output path for the movies. Change this to what you would like it to say.
$outputpath = "e:\output"

#grabbing the filenames of all of the movies in the folder you just provided.
$movies = ls $inputpath

#The actual logic of the script.
#starting to loop through each movie in your list of movies
foreach($movie in $movies){
 
 #getting the name of the movie, without the file type added to the end.
 $name = $movie.basename
 
 #checking to see if the current movie has already been converted, and is sitting in the output folder.
 #this allows you to run the script repeatedly, without it wasting time converting something that is already done.
 if(!(test-path -path "$outputpath\$name.mkv")){
 
  #the actual command used to start handbrake, and encode/compress a movie.
  #Adjust these settings as necessary. 
                 #These are what I use when encoding, but YMMV for your application
  C:\"Program Files (x86)"\handbrake\HandBrakeCLI.exe -i "$inputpath\$movie" -o "$outputpath\$name.mkv" -e x264 -q 20.0 -a 1,1 -E copy:ac3,copy:dts,copy:dtshd -D 0.0,0.0 -f mkv --detelecine --decomb --loose-anamorphic -m -x b-adapt=2:rc-lookahead=50

 }

}

So all you need to do is to copy and paste that script into a blank notepad file (press and hold the
windows key and then press "R", then in the prompt that comes up type "notepad" and press enter).
Change whatever settings you want (file paths, handbrake settings, etc) and then name it whatever
you would like, as long as you remember to save it as a ".ps1" (powershell) file.

After that, you'll likely only run into one other small "problem" running the script. Powershell by
default is not very trusting (read, more secure), so when you first try to run this script, you'll
likely get a message saying that it cannot be run because the execution policy of the machine does not allow it. Luckily, this is an easy fix. Just type this into an elevated (meaning, run-as administrator) prompt.

Set-executionpolicy -executionpolicy remotesigned

Then after that, try to run your script again, and everything should be peachy.

The wrapup

And there you have it. Now you can start off handbrake to run while you sleep, and you'll have all
those movies converted and compressed in no time :). If you want to get really fancy, you could even
adapt the script to run as a scheduled task, so that anytime you upload files to a folder, it could
convert them, move the outputs to the correct location, and then delete the input file. However, the
core functionality is here in my template.

Feel free to post up and changes or adaptations here, I'd love to see what others come up with :).

Tada!

6 comments:

  1. Thank you for this. Very neat.
    Now only if there was a way to limit cpu usage of the handbrakecli process, it working at 100% ;)

    /iSh0w

    ReplyDelete
    Replies
    1. you can limit your threads in the GUI using ":threads=1". Wonder if there is something similar on the command line side.

      Delete
  2. Replies
    1. Powershell is an interesting combination of many languages, and it appears that they tried to make it easy for users of many backgrounds to use it.

      ls is of course a bash command. But in PowerShell it is a default alias for "get-childitem". Similarly the old windows command line command "dir" is also an alias for get-childitem. Ls is just faster to type, and helps not confuse me between systems :).

      There are actually quite a lot of Linux/bash aliases in PowerShell. Other ones I use all the time are cat, ps, man, and tee, but there are many others.

      Delete
  3. Sands Casino: The Ultimate Guide to Slots
    The Sands casino is an excellent place to try the newest slots, the newest video slot machines and even 온카지노 the newest septcasino jackpot games. It is a 메리트 카지노 쿠폰 great

    ReplyDelete