uphpu presentation: ripping dvds

I gave a short presentation last night at uphpu on ripping and encoding DVDs under Linux. I wanted to type up a few notes on some stuff I mentioned during the meeting.

Short version: Use Handbrake (available on Linux, Mac OS X and Windows). I’ve gone through a lot of applications through the years trying to find that “perfect rip”, and Handbrake beats everything hands down. I’m a videophile who likes everything looking as nice as it can, and this is what I use regularly.

Accessing DVDs

The first things you’ll need installed on your box is libdvdread and libdvdcss. libdvdread will access the DVD filesystem, and libdvdcss will decrypt the DRM on the disc. These should both ship with your distro, or if they don’t, should be relatively easy to acquire.

I use a lot of command line tools to access DVDs, so my focus was on that. The reason for that is because I have a lot of shell scripts to rip my massive library of discs, and I store data about them in a database.

lsdvd is the first stop. A very small app that uses libdvdread to display human and script-friendly output of the details of your disc.

mplayer dvd:// and mplayer dvdnav:// plus the optional track (dvd://3) or optional -dvd-device argument to specify the location of the device or ripped ISO.

I use dd to copy the contents of the entire disc to the harddrive and then rip it from there, to avoid wear and tear on the DVD drive, and also because I can do things like simultaneously rip one disc in the background and encode another at the same time.

When accessing the drive, you’ll want to use mplayer or some other program to access the drive and decrypt the CSS so that the drive itself caches the access. Otherwise, it may lock up.

I use pv to dump the disc and give me a pretty progress bar with ripping ETA and disc read speed: pv -ptre -w 80 /dev/dvd | dd of=movie.iso

If you want to extract the chapters, you can use dvdxchap, which is part of the ogmtools package.

DVD Tracks

Every movie I’ve run into has one track specifically for the video. Using lsdvd you can find the longest track, time-length wise, and that will be the movie.

TV shows on DVD often put many episodes into one track, where one or more chapters may contain an episode. You’ll need to examine these yourself to find out where each one starts and each one ends.

Another caveat is that they are not always ordered on the track or on the DVD in the same order that shows up in the menu. The best advice when accessing tracks and titles on DVDs is to never assume anything, if you are trying to preserve order.

DVD Subtitles

Subtitles on DVDs come in two formats: VobSub and Closed Captioning. VobSub subtitles are images that are overlaid onto the picture. Closed captioning is a text format.

If you have a ripped VOB (DVD MPEG-2 video), you can extract the subtitles using a program called ccextractor. It will save the subtitles to SRT, a subtitle format that can be muxed into your final video.

Encoding DVDs

Handbrake is a very “smart” application that will make things extremely easy on you. It supports encoding using three codecs: x264, ffmpeg and theora. x264 is an actively-developed H.264 video codec, and is used professionally by many companies. It also has a lot of options that you may find useful in your encode, based on your target playback device (ipod, ipad, iphone, apple tv, htpc, computer, etc.).

Handbrake supports muxing (or putting audio and video into a wrapper format) to two containers: MP4 and Matroska. MP4 is a very popular standard used in lots of places. It’s an open-specification. Audio is usually AAC. Matroska is an open-source openly developed container format that has a standardized specification. Support for it is growing in a lot of devices. Matroska can handle multiple video streams, multiple audio streams, multiple subtitles, stores metadata, chapters and attachments. Strictly speaking, either one will work fine for most people’s needs, but for those looking for advanced usage and tagging, Matroska will fit your needs. On linux, the tools for muxing and accessing information about a Matroska file are found in the mkvtoolnix package.

Deinterlacing and Decombing

Most movies will be progressive video, meaning that each frame is one full picture. Production studios and DVD authors may have their video interlaced. Interlaced video is where half of the frame is shown in quick succession where the second half is shown. On older TVs (like the CRT tube TVs) display at a native resolution of 480i, so you won’t notice the lines. Progessive displays (computer monitors, HDTVs, etc.) will display the lines and it can be a visual annoyance.

Handbrake uses filters to convert interlaced video to progressive video, called deinterlacing. Decombing is also a feature that Handbrake supports, which is basically “smart” deinterlacing. It is safe to turn on the decombing filter to the default setting for all your encodes, and let Handbrake automatically convert your video for you. Don’t use the deinterlacing filter unless you specifically know what you are doing.

Please see 100fps.com for a quick explanation of video deinterlacing.

Other Handbrake Features

Handbrake has lots of handy features. It has support for “profiles” which is a combined set of preferences for container format, video codec, audio codecs, preferred subtitle settings, post-processing, deinterlacing, decombing, and all the H.264 settings that come with x264.

One nice thing it will do as well is auto-crop your feature film for you. This is a boon if you have a letterbox source video, where the movie itself is in widescreen, but the presentation is in fullframe.

You can also “queue” up your encodes, so Handbrake can be working on multiple files. You don’t need to wait for one to finish before assigning it it’s next task.

Handbrake is also multi-threaded when using x264 to encode your video. With a six-core desktop, I have been able to encode video using the “Normal” profile between 160 to 180 frames per second. Meaning a 25 minute video will take about 2 1/2 minutes to encode.

PHP Classes

I’ve got some small classes that I use in my shell scripts for Matroska and Handbrake (v0.95 required). You may or may not find these helpful. I should warn that these are just used on my own set of scripts, so they may or may not be very clean or can have small bugs.

References

If you want more information, here are some good places to go:

doom9
Multimedia wiki
Handbrake forums
100fps.com

Also, feel free to contact me if you have any questions, and I’ll be glad to help.

Advertisements

wrapper script for disc_id

I wrote a little wrapper script for disc_id tonight, available here. disc_id is a little binary that ships with libdvdread, or at least, it used to in older versions.

I use disc_id to give me a unique 32-character string of a DVD, so I have an identifier to track them by in my database of DVDs.

I don’t know if it’s just me or not, but my DVD drives have issues trying to poll the devices. Once I insert a disc, it will take a few seconds for it to register completely so I can access it. However, binaries that access it will think it’s ready to respond sooner than it is able, and will die unexpectedly. So what I needed was a way to get the disc id and not worry about whether or not the drive has finished registering or not.

I just call my little script dvd_id and it is simply a small wrapper that checks the exit code of the disc_id binary. If it doesn’t work the first time, it sleeps for one second and tries again, then repeats the process until it gets a successful exit code of zero.

That’s it. Pretty simple, but like all little scripts, you really tend to depend on them.
#!/bin/bash
EXIT_CODE=1
DEVICE=$1
if [[ -z $DEVICE ]]; then
DEVICE=/dev/dvd
fi

if [[ ! -b $DEVICE ]]; then
echo "Device $DEVICE doesn't exist" >&2
exit 1
fi

while [[ $EXIT_CODE != 0 ]]; do
/usr/local/bin/disc_id $DEVICE 2> /dev/null
EXIT_CODE=$?

if [[ $EXIT_CODE != 0 ]]; then
sleep 1
fi
done

web media frontend

I have always wanted to tweak my HTPC frontend quite a lot to add extra functionality, but the entry barrier to learning a GUI language has been way too high for me.  I’ve had success though, in patching MythFrontend to do some things a little better for me, but I’ve always wanted to get my own going if I could.

Recently, I was thinking about how LIRC can capture IR events and map them to X keyboard events.  Basically, you can control X applications with your remote control.  I started to reason that if that were possible, then I could just use my web development skills and create a webpage frontend for my HTPC that would run on a lightweight browser, and listen for keystrokes.

Just playing around with it tonight, I actually made some really great progress thanks to a combination of a good friend, my humble jQuery beginnings, and my laughable CSS skills.  This is the result so far. :)

I’m really stoked about the implementation so far.  You’ll most likely need Firefox to get that working properly.  It will capture the arrow key presses (up, down, left, right) and use that for navigation.  I realize that the beginnings are rather crude, but the fact that I could throw this together, so quickly, while I’m just barely learning my way around jQuery seems pretty impressive to me.  I’m actually quite proud, though, that I got the navigation to work properly, too, so wrapping around rows and columns works. :)

This is certainly going to be a fun project to hack on.  If I could get this working, this would open up all kinds of possibilities for me for displaying metadata and new options for navigation.

For comparison, here’s a screenshot of what my frontend looks like right now.  As you can see, I’m trying to imitate the style as closely as possible.

There’s a lot of advantages to having it web-based — not that I’m going to serve up anything remotely or anything, this is solely for my LAN.  It’ll just allow me to build out stuff much faster.

The hard part is going to be doing testing on the frontends.  They are both running off of tiny installations, and it’s not easy building and porting software to run on them.  Sounds like a challenge that’s extremely hard, going to take a lot of time, and will have marginal benefit and at the same time increase my workload and opportunity to own more of my software stack when things go wrong.  That’s just right up my alley. :)

handbrake ebuild

My life has been uncharacteristically busy lately, which is a really new experience for me, because I usually have so much free time that I don’t know what to do with myself.  Anyway.  As a result of lack of time, it’s been interesting to see how I deal with the crunches when there’s stuff I want to work on.  So far though, my adaptation has been nothing more than watching tasks I want to do be ignored for weeks on end.

So, in an attempt to get one task of many moved forward, I’m just going to do a brain dump of my thoughts into a blog post and hope that someone can take it running from here.

To start with, I totally love the video encoding tool Handbrake.  It is an aboslute godsend, one that makes it possible for me to actually encode all my DVDs to MPEG4 using x264, and have me happy on every count.  (If I’ve talked about this already before … oh well.  I can’t remember these things anymore.)

There’s a lot of reasons for it’s awesomeness, but I’ll write those up in a later post.  The simplest summary is probably to say that it passed the Star Trek test with flying colors — which was always assumed to be an impossible task.  So, saying I’m happy is putting it mildly.  It’d be more accurate to say I feel like a schoolgirl on crack who is dancing on the rain.  Or something.

Anyway.  I’d like to roll an ebuild for it and get it into portage, if possible, but because of the build system, there’d be some things that need to change first.

The build system used in Handbrake downloads sources from their website and unpacks them during the building stage.  While that’s fine if you’re building it yourself, and if you wanted to roll your own ebuild (which, in fact, there are some already in our multimedia overlay), it wouldn’t be good from a QA stand for Gentoo.

So, what needs to be done (this is where I start whining about how busy I am, and how this is your job to fill in the gap) is the Makefile needs to be modified so it won’t download and unpack the remote sources.  It can still access them, but it needs to be up to the ebuild to do those in its own stages — like moving the tarballs into SRC_URI and using src_unpack to unpack them.

I haven’t looked closely at the build system, but I imagine it wouldn’t be too difficult to patch.  If someone wanted to take it from there, I could run the last few legs and see about cleaning up the ebuild and possibly getting it included in the tree.

If anyone’s up for the challenge, follow this bug.  Thanks :D

mplayer + libbluray support

MPlayer just very recently got support for playback of unencrypted Blu-Ray discs using libbluray.  (Thanks to all the devs and testers! :) )  Apparently development for the library is being hosted on VLC’s git servers now, something I had no idea about.  I thought the project was dead upstream.

I’m adding an ebuild for libbluray to the gentoo multimedia overlay if someone wants to access it.  It’s something I plan on pushing into the mainline tree soon enough, once it’s properly finished.

If you are building MPlayer from SVN, it will automatically detect the new library, and build against it.  You can use the -9999 ebuild in the portage tree.

To playback some of your Blu-Ray content, you will first need to extract it to your harddrive.  I use MakeMKV, also in the multimedia overlay, to accomplish that.

Here’s a simple way using the CLI to dump the contents:

$ makemkvcon backup –decrypt disc:/mnt/bluray/ <location to dump content>

The syntax for playback is:

$ mplayer br:// -bluray-device <path to dumped content>

By default, it will play the longest playlist (I think).  If you can get the list of playlists available, you can pass that as an optional parameter to br:// (fex: list_titles /home/steve/bluray/src; mplayer br://5 -bluray-device /home/steve/bluray/src).

libbluray also ships with a few example programs that do basic stuff like listing the titles (list_titles), dumping information about the playlists (mpls_dump), and a few more (sound_dump, index_dump, mobj_dump, libbluray_test, bdsplice, clpi_dump).

Have fun with it. :)

gentoo + youtube – flash + mplayer

So, if you’re getting a little tired of Flash and it’s silly security hiccups, but still can’t live without the YouTubey goodness that is the awesome sauce of life, here’s a simple solution I stumbled onto: use mplayer to watch the videos!

I haven’t found a way to embed this in my browser yet, but I haven’t really looked either, so this is for all the CLI geeks.

$ mplayer $(youtube-dl -b -g http://www.youtube.com/watch?v=9IfEInQ7aic)

And thar ya go. :)

Oh, and did you know that Flash Gordon is on Blu-Ray now?  Flash!  Aaaaaa-ah!

blu-ray on gentoo

I’m pretty excited because I got my first BD-ROM drive last night from NewEgg, a LITE-ON iHOS104-06.  That means I can do some real testing, ripping and playing around.

Decrypting Blu-Ray discs is a really confusing process … I’m still not even sure of all the steps that are involved.  Everything I understand has been cobbled together from posts on the doom9 forums.  While the forums are a great resource, it’s not a comprehensive one at times.

I was playing around with aacskeys (from doom9 forums, available in portage), and it managed to decrypt / find the keys / whatever it’s doing / work successfully on most of my movies.  I’m not sure how to get them off after that, though, or why that’s important yet, but I do know it’s a good sign. :)

For now I’m taking the simple route of using shareware to access my movies.  There’s two programs I’ve used so far to rip my Blu-Rays, AnyDVDHD and MakeMKV.  They are both nice programs with some good features, but MakeMKV is the only one that has a Linux port.

The last time I tried MakeMKV, it couldn’t decrypt all my discs, so I had to use my PS3 to rip the ISOs, and then use AnyDVDHD.  This time, though, using the most recent version (1.5.6), it managed to decrypt all of my discs.  I was going through my Blu-Rays to see if it could handle all of them, but I gave up after the 15th one, since it was working on every single one. :)

While AnyDVDHD will extract the original, unencrypted files to your harddrive, MakeMKV will additionally mux them at the same time into Matroska.  I kinda wish I could still have the originals, but I’m not going to be picky. (Edit: you can, see comments)

So, no real plans after this except to play around and post my results.  I really don’t have that much interest in playing with Blu-Rays on Linux other than curiosity.  I don’t wanna rip them and stream them to my HTPC just yet since I don’t have the storage space, and because my frontend isn’t quite as HD-ready as I’d like it to be (I still need to update some software and tweak settings … lots of testing, meh).

I am going to be looking at some other tools and see if I can get them in portage or our multimedia overlay, which reminds me, I just added MakeMKV to there this morning if someone else wants to try it out.