reading, ejecting, ripping and polling dvd devices in linux; also, notes on my dvd library

It has been a looong time since I both posted in my blog and worked on my custom DVD ripper scripts.  Apparently the last time I worked on the code was last June, and even then I didn’t make many updates.

I’ve been spurred onto building up my DVD library again by a couple of things. First, I realized that my Blu-ray player has  support for Matroska videos with VobSub and SRT subtitle support!  I was not expecting that.  In fact, it’s way better than what my PS3 can playback, which is … depressing.

I put away my HTPC about two years ago, when I was living in my previous apartment.  I moved into a place that was probably about 550 square feet.  Pretty tiny, and I liked it, but no room for a fantabulous multimedia setup.  So I sacked it for a while and was okay with that.  The fact is I actually spent more time getting it up and running and customizing it than using it.  Which is weird.  Actually I spent even *more* time ripping the DVDs and then not watching them.  But that’s okay.  It wasn’t until recently that I found a setup I think I’d like even more.

For now, I’m preferring having *less* hardware, and so just sticking a small 8GB USB thumb drive in my Blu-ray player with a smattering of samples of shows suits me just fine.  It’s no amazing thundershow of hardware and multimedia, but it *does* get me actually watching the content, so there.  I imagine if (and when) I have a house where I can properly get loud without upsetting neighbors, that’s when I’ll whip the big speakers back out and deck it out properly.  Some day. 🙂

In the meantime, today, I’ve been working on my DVD scripts.  I call it dart for “dvd archiving tool.”  It’s a complex set of scripts that I’ve been putting together for years, and it is highly customized for my own setup, with a CLI tool to read and access DVDs, then archive them in a database.  I also have a web frontend that I use to tag tracks, titles, episodes, etc. and so on.  If it wasn’t so unwieldly I’d throw the source out there, but the thought of having to explain to *anyone* how to get it up and running makes my head hurt.  So, if you want a good DVD ripper, here’s my advice: use Handbrake.

One problem I was trying to solve tonight was checking for these three statuses of my DVD drive: is the tray open, is the tray closed, is there media in the tray (while closed).  I have to use different tools for each one, but the problem that I always run into is this: it’s impossible (as far as I have been able to discover) to know when a DVD tray is both closed and ready to access.

The problem is that you can run eject just fine to close the tray, but once the command exits successfully, that doesn’t mean the drive can be accessed.  That is, running “eject -t /dev/dvd” and then “mplayer dvd://” in sequence, mplayer will complain that there’s no DVD device.

What’s the solution to all this?  Well, wait four seconds after running “eject.”  That’s simple, but I still spent hours today trying to find out if there was another way to do it.  While I never did (and ended up using ‘sleep’), I did find some cool stuff for polling and reading DVD devices.

blockdev

blockdev basically displays some interesting information about the block devices — in this case, /dev/dvd.  Now, for my library, one thing I have been doing lately is storing the size of the DVD in my database, so I can get an accurate number of how much HDD space I need when I want to archive the UDF or rip it.

You can use blockdev to get the amount of bytes like this:

blockdev –getsize64 /dev/dvd

Now if you want to see that in megabytes, just divide it by 1024

expr `blockdev –getsize64 /dev/dvd` / 1024

udisks

Next up is udisks, which can get information about the DVD device itself.  In this instance, I use it to see if there is media (a DVD) in the tray or not.

Running “udisks –show-info /dev/dvd” spits out all kinds of interesting information, but what I’m looking for is the “has media” field.

udisks –show-info /dev/dvd | grep “has media”
has media:                   1 (detected at Wed Jul  3 23:21:23 2013)

Now, that will say 1 *if* the both the disc tray is closed and there is something in there.  And if the DVD drive has stopped spinning enough for the command to work (again, sleep 4 seconds after closing the tray).

It will display a zero if there is no media *or* if the DVD tray is open.  Here’s a simple command to get just the number:

udisks –show-info /dev/dvd | grep “has media” | awk ‘{print $3}’

cddetect

This is an old small command-line tool I’ve used in the past.  It polls the drive to see if there’s something in there or not, and if the tray is open or not.  Sounds great, right?  It should do everything I want, solving all my problems … except that it doesn’t build on my system (Ubuntu 12.10 with gcc 4.7.2).  It used to, on my older setup, which would have been about 2.5 years ago.

It’s just a small C script, just over 500 lines, you can find it here on Freshmeat.  If someone wants to patch it to get it working, I’ll personally deliver you a plate of brownies.  Mmmm, brownies.

I actually *do* have an old 64-bit binary that I built way back when, because I kept a copy of my old development filesystem.  So I have a working blob, but it kind of breaks.  So I kind of rely on it.  I can only use it if the tray is open or if the tray is closed and empty.  So the way I check if a device is empty and closed in my script is I’ll first poll it to see if it has media with udisks, and if it doesn’t, then I’ll run this one.  If I run it with a disc in there, it pukes on me, and so I have to work around it.  It’s a hack, I know, but whatever.

qpxtool and readdvd

This is the project I ran into today, and I am super, super excited about it.  The QpxTool project is full of way cool little utilities for accessing your drive settings.  Honesty, I didn’t look at the other ones, because I was so hyperfocused on ‘readdvd’.

From the man page, “readdvd reads even a corrupted dvd and writes the the result into a new image file on your harddisk.”  This is awesome, because it’s the first utility I’ve found *specifically* for creating an exact image of a DVD filesystsem (UDF).  In the past, I’ve always used dd, but now I’m onto this one.  It skips over bad sectors and gets the image squeaky clean off of there, and I could not be happier.  This one ranks up there with Handbrake in both awesomeness and must-have-ness.  I should add that it’s also in Ubuntu’s default repos, so have fun.

Just run “readdvd -o movie.iso /dev/dvd”.  Pretty simple.

That’s pretty much it for now.  There are other great tools out there: lsdvd also ranks in the “must have” category.  I couldn’t do anything without it.

I mentioned dd earlier, and I actually use pv with it to give me a nice progress bar (also in Ubuntu repos).  It works just fine, I’ve been using this approach for years.

pv -ptre /dev/dvd | dd of=movie.iso

One more thing I wanted to mention.  Sometimes, some errors get thrown to the syslog because either an application or the DVD drive itself is being fussy.  I haven’t quite narrowed down which it is, but I’m betting it’s the firmware on the DVD drive complaining since some brands (Memorex) complain, and some do not (BenQ).  By far, the best-quality DVD drive I’ve had to date was actually a Sony BD-ROM drive.  At least, I think it was Sony.  Here’s some of the errors I get sometimes:

Jul 3 18:37:04 localhost kernel: [11955.073772] sr 0:0:0:0: [sr0]
Jul 3 18:37:04 localhost kernel: [11955.073784] sr 0:0:0:0: [sr0]
Jul 3 18:37:04 localhost kernel: [11955.073795] sr 0:0:0:0: [sr0]
Jul 3 18:37:04 localhost kernel: [11955.073808] sr 0:0:0:0: [sr0] CDB:
Jul 3 18:37:04 localhost kernel: [11955.073826] end_request: I/O error, dev sr0, sector 4096
Jul 3 18:37:04 localhost kernel: [11955.074286] Buffer I/O error on device sr0, logical block 512

To avoid issues like this, I run a small command to just decrypt the CSS on the DVD so it can kind of clear its head a bit.  Just run mplayer on it, watching about 60 frames (or 2 seconds worth of video), but just ignore it and dump it out.  The whole point of it is to decrypt the DVD, and move on with your life.  And here you are:

mplayer dvd:// -dvd-device /dev/dvd -frames 60 -nosound -vo null -noconfig all

I don’t pretend to understand how or why that helps, but I know it does.  If someone knows why the drives are doing that, I’d love to know.

The only other app I can think of right now off the top of my head is ‘dvdxchap’, which is part of ‘ogmtools’.  I know ogmtools is old, and the OGM container isn’t popular anyway (that I’ve seen), but it’s perfect for getting the chapter information out.  Although I may use something else now (lsdvd?).  I can’t remember, and I haven’t had to mess with chapters lately.

That’s it for me.  Have fun, rip away, and watch some cool Super Friends DVDs.  There are a LOT of seasons out there.  It’s great. 🙂

Leave a Reply