Why didn’t anyone tell me…

Things I wish people would document, plus some original fiction. Weird, huh?

Why didn’t anyone tell me… header image 3

How do I encode that pile of DVDs?

January 5th, 2009 · No Comments

encode

Back Story

We finally stepped part way into the HD era, by attaching a PS3 to our SD television. Sacrilege, I know, but it’s what I have for now, and the economy isn’t getting any better this week. But, as we only have two games for it (Little Big Planet and Mater-National Racing), we have found that it has another wonderful use. The Videos menu.

(Keep in mind that I have 3 little kids in the house, and while most of their viewing is in the broadcast-DVR, I still have hopes that they will take an interest in longer attention span viewing, or at least the Muppet Show season sets and Looney Tunes sets I have waiting. But handing them DVDs is a great way to have things get destroyed. Enter digital viewing on the TV.)

So, having an Infrant ReadyNAS x600 in the garage, I bought a copy of the far more predictable TwonkyMedia server that handles all the gross stuff related to sharing your video folder with the PS3. The built-in server just wasn’t maintaining itself well, no matter how I tried. I’m sure someone can make it go, but I needed something a bit more turn-key. But the bigger problem still remained: I have something along the line of 300 DVDs between movies, television series, and home-made things. Ripping is the tetchy topic, and if you have questions we can talk via email, but the fact is that ripping even the most difficult DVDs is now possible. But now you have at least one hard drive full of DVD folders or ISO images of DVDs, and you have to turn this into something a bit more manageable.

This is the PS3 specific portion – if you are comfortable with how your player plays back and what encoder settings you can get from Handbrake, then feel free to skip forward. As clients to my encoded DVDs, the first priority was everything was to be HD resolution for the PS3 and/or my TViX 4100. There was also a smaller subset that I wanted to play back on my iPhone. After much experimenting with  Handbrake, I settled on a set of 2 encoding strings that made me happy. One was the default PS3 profile, plus chapter markers (for computer playback), plus 2-pass encoding. The other was the same, but with deinterlacing turned on, for those titles like the Muppet show that were interlaced on the discs. (If you don’t deinterlace those, then the frames come out in the wrong order when replaying on the PS3, and make things jump around forward-backward-forward and drive you to turn it off.) Fortunately, the TViX plays these files without modification, so I was set for both devices. So those two strings were written and set aside. Then I came up with a parallel pair of strings for the iPhone setting.

So I had my 4 preferred encoding settings and a pile of 300ish ripped DVD images or folders to eat through. I knew there was a problem. At PS3-2pass resolution, each encode was about 4 hours. That’s a lot of hours, and queuing each in Handbrake’s GUI takes more time than I want to do. I already knew exactly what I wanted, but I didn’t want to do it by hand.

What I Did

So I did a little digging on Google and discovered that for some reasonably old version of Handbrake, someone wrote a bash script that would turn VIDEO_TS folders into mp4’s. As time had left this little gem behind, I did some minor repairs and used that for a couple encodes. But it had some limitations I didn’t like, and didn’t work at all for Windows machines, and so on. I even modified the original VIDEO_TS script to work with ISO images, and that saved me some more work. I could then do massive batch encodes using my two scripts. I took images to work and left the far faster work computers cooking m4v’s for me over weekends and nights. It was great for saving me weeks of computing at home. But I had to custom-build the scripts for Windows via shared folders on the mac, and it still took too long to manage two scripts across multiple machines. Enter the perl experience. At work I’ve been working with cross-platform scripting for a while now by using Perl. I opened up the guts of the VIDEO_TS script again, figured out how to do what they were doing, added the new abilities of Handbrake’s latest versions, and got to work giving the Windows machines the same logic the Mac had with the bash script.

The new script, stupidly named encode.pl, now handles pretty much anything that Handbrake can handle – or at least anything I encountered. First, you queue a directory. It simultaneously queues VIDEO_TS directories, ISO images, wmv/avi/mov/etc. files for encoding. Then it proceeds to do discovery via Handbrake about what it should encode. It does this by querying the title list for the duration and then applying a filter for length. For instance, my default setting is anything between 20 and 600 minutes, but that can be tuned with the –minimum or –maximum settings. Then it goes through them one by one, handing the whole thing off to Handbrake to be encoded. As each one finishes, it goes to the next one. No mess, no fuss. Until you run out of hard drive space that is. Can’t help you there.

So then I had to tune the way I organized my DVD images for feeding into this thing. My ReadyNAS had enough space at the time, so I put things into distinct folders. In the media share, I had a Videos folder. Inside I created the following:

/Volumes/media/Videos/todo/…

…/deinterlace/long

…/deinterlace/episodes

…/long

…/episodes

- and here’s why. The long directories were for things that I wanted only the titles that were over an hour and ten minutes. This let me get a single file for all of the Looney Tunes shows, strung together, instead of a pile of 7-9 minute shorts. There are enough per DVD to make it frustrating to have to return to the menu for each new show. Kids will just give up, and so will I. They already have a title with all of them together, so I just added the –minimum 70 setting when I do the long directories.The episodes directories were for things no longer than an hour. These are the opposite of the long things. So for cartoons, sitcoms, and other 22-44-minute episodes, I don’t want the whole DVD as one title, I want each show as an individual m4v file. That directory got the –maximum 50 setting. This pretty much handled everything except for a couple odd cases that I just encoded more or less by hand with the GUI – mabye 3 discs total.

For output directories, I created:

…/m4v

…/iPhone

Now, because I had central storage for both the input directory and the output directory, I also considered using multiple computers. I was able to use the 4 computers under my desk to all work on a central queue. I would go to each computer, mount the share, and put together the encode string for that machine, and set it running. Each computer when it starts an encode creates the file it’s starting on, and then starts the Handbrake line. I had to do this because with 2-pass encodes, the output file is not present until the second pass starts. Because of this, if you have computer A starting on title 1, then computer B might start on it too. If the file is present, then computer B says, “Oh, that one’s there, let’s start the next one.” So you get a cheap claim-before-starting system built in if encoding to the same destination. Then another thing came up. I could do encodes on my laptop while away from the main servers. So I added the ability to keep a log of what’s encoded and copy that to remote locations (*cough* work) and still not get double encodes. It’s not a perfect system, but it’s better than nothing.

The Interesting Part

So the result is a semi-distributed way of encoding a seriously big pile of movies into a standardized format using both Macs and PCs, possibly central storage, and getting a pile of files with at least standardized names.

I’ve actually considered trying to add a way to use STAF to auto-distribute encodes to available systems, but that takes a bit more time than I have at the moment. But that could allow you to have a central “encode controller” and some “encode slaves” that you hand out single encode jobs to. It’s within reach, I just need an hour here or there to try it.

Anyway, here’s how you use it:

perl encode.pl -i /Volumes/media/Videos/todo/long -o /Volumes/media/Videos/m4v --minimum 5 --maximum 70 -e "-Z PS3"

or the equivalent in Windows paths:

perl encode.pl -i M:\Videos\todo\long -o M:\Videos\m4v --minimum 5 --maximum 70 -e "-Z PS3"

-i for an input directory

-o for an output directory

-e for anything you want to hand directly to the encoder. “-Z PS3″ is the default PS3 string, but be careful with “-Z ‘iPhone & iPod Touch’” because the shells differ in quote handling. Try it on a single command line to get it right and then keep it around in a text file somewhere to be sure. Also, because of the funny quoting problems, I usually try to make the -e setting the last one I hand in.

–minimum is the minimum time in minutes that you want to approve a title for encoding

–maximum is the opposite. Note that minimum and maximum ignore seconds.

Note that almost everything is optional. You can open the perl code and change to your favorite defaults. On the Mac, the default input and output directory is ~/Movies, on PC it’s in My Documents. The pitfall of using the output directory as the input directory is if you cancel any encode, you’ll try to re-encode anything you’ve finished in the last pass. So try to keep them separated for sanity’s sake. I ended up one morning with MY_MOVIE.ISO_T2(through7)_T1.m4v trying to figure out what the heck went wrong.

So here you are readers, a new and improved mass encoder script for using Handbrake to eat massive piles of DVDs with one or more computers and Handbrake. Enjoy!

–edit

A little more experience added a few more things.

1. Favorites, so you can save your favorite encoding strings by name. Mine are PS3 PS3-d PS32 iPhone and iPhone-d with predictable results. But this is easier to type. So replace your -e arg with a favorite -f iPhone and you can save yourself a world of hurt. But -e still works as it did before.

2. Partial encodes are saved to blah.part.m4v until they complete, when they take on the name of their finished product. This was supposed to handle the situation of an unfinished encode displacing the encoder’s next pass, but alas, something doesn’t consider it an error when it’s interrupted *cough handbrake* so I’ll have to work on that some more.

Upload to follow shortly.

Tags: Bookshelf · Computer · Entertainment · House and Home · Kids · Projects