January 16, 2013

Transcoding a Movie Library with Python

Filed under: Home Theatre,Programming,Python — pj @ 10:18 pm

Like many a fellow geek, I have a Ubuntu server with shared folders that hold my collection of photos, music, movies, etc.. To show the movies on my TV, I started off with ASUS O!Play media player. Eventually, I bought a Samsung Blu-ray player with built-in media player. While the ASUS box supported CFS (Windows file sharing), the  Samsung could only see DNLA servers. I installed a DNLA service on my Ubunto and that worked fine until recently. Last summer, I moved to a new house. My new place doesn’t have CAT5 run all over, so I needed a new Blu-ray player with WiFi. I bought an inexpensive Panasonic unit from Costco because the Panasonic supported Amazon Instant Videos. Unfortunately, the Panasonic was much more picky about the supported video formats. While it can play modern .mkv files, it can’t play .avi files at all. I had a choice: I could buy another Media Player (the Western Digital WD TV Live was recommended for wide codec support) or I could figure out how to transcode videos.

After a few experiments, I discovered that the Panasonic was happy with .mkv files with H.264 encoding. After playing with several programs capable of transcoding on Ubuntu, I settled on Handbrake.

As many probably do, I had a large and messy collection of video files that needed to be converted. The one saving grace to the collection was that each group of files for a given movie was in a separate folder with a name that was based on the movie title plus other junk. Since I was cleaning things up anyway, I took some time to manually rename the folders in a consistent way. I did this in a way that allowed my transcoding program to parse the movie’s folder name and use it to name the resulting .mkv files. The result of all this is the simple Python program, transcode_avis.py that generates the Handbrake commands needed to convert .avi files with arbitrary video encoding to .mkv files encoded with H.264. The audio stream is copied rather than transcoded, as is my preference, to preserve original DVD digital surround sound streams when available.

I used transcode_avis.py to generate a batch file that I ran by hand. The batch file invokes HandbrakeCLI to do the work. I did it this way because I was going to slice up the the work into multiple batch files which I could run simultaneously to take advantage of my 8 core desktop CPU. But, this was unnecessary as Handbrake does a good job of using up all the cores on its own:

handbrake_task_manager

 

Transcoding 2 videos at once does push the CPU usage completely to 100% but it essentially transcodes each video at 1/2 speed.  The typical transcoding speed on my system is 265 frames per second (or 1/2 of that if transcoding 2 videos at once).

Leave a Reply

Powered by Teztech