Category Archives: bend / dvd2mkv

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 comment

Filed under bend / dvd2mkv, MPlayer, Multimedia

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

Leave a comment

Filed under bend / dvd2mkv, Gentoo, Multimedia, Programming

playlist resume

I wrote about this a little bit a while back, wrt mplayer-resume, that I wanted to work on a way to resume playback from a playlist as well using MPlayer. Well, I finally sat down and figured it out last week. For some reason it took me a good while to figure out the logistics of how to do it rather than the coding. I dunno why. Anyway.

I wanted to wait to bump mplayer-resume until after I added something else, and this is gonna be it. Just gotta clean up the code and package it now, and it’ll be ready to go.

Here’s how it works though: the script, like mplayer-resume, acts as a wrapper to mplayer. In fact, everything is pretty much the same except that it saves both the filename along with the position in the playlist.

The problem I was having a hard time figuring out was that I wanted to easily make two options available: resume playback in the current file I’m in (old mplayer-resume functionality), and resume playback in the position in the playlist I’m in. I just finally mapped two keys on my remote, exit and stop, to take care of both of them.

Hitting Exit on my remote will save the playback of the current file and write that to the playlist position file. Then it also saves the seek time where I left off. Hitting Stop will kill the old entry and just add a line to the playlist saved position file that says what the *next* file should be on playback.

Pretty simple when you consider it, but it took me some time to figure out, mostly so that it seemed intuitive. I figured I’d have a hard time getting used to it, and hit stop accidentally and losing my place where I wanted to be, but so far it’s worked out fine. I’m happy with it.

I’m using it for playback of my TV shows, which is pretty much 95% of what I’ve got ripped on my media setup. I have a few movies, but no AC3 output, so I prefer to just stick those in my DVD player. It is the only way I can watch my Region 2 or 4 DVDs though. For TV, it works great, since I generally want to watch them in general succession of episode order. And if I don’t feel like watching a certain episode, you just hit stop and it’ll skip to the next one. Very nice. I suppose I could map a button to go *back* in the playlist if I screw things up accidentally, but I’m kinda running out of buttons to map on my remote. Besides, vague mappings that I’m supposed to remember usually hurt more than help.

2 Comments

Filed under bend / dvd2mkv, Gentoo, Multimedia, MythTV

bend, drip, mythvideo

Here’s something I haven’t written about in a long time — bend, my custom written CLI PHP5 scripts to rip and encode TV shows.

I actually rewrote the entire thing over Labor Day weekend.  What’s amazing is it took so long to write the original one, but so short a time to completely revamp it.  It’s something I’ve been wanting to do for a long time, and I’m glad I finally got to it.  The code on the old one was so horrible, and was such a frustrating experience to patch, debug or add features.  The new one is already 20 times better.

The first one was just plagued by scope creep, though — I started off just mostly coding it around the way that I thought DVDs *should* work and how they ought to be authored, only to be constantly slapped in the face by so many exceptions that I’d have to go back and hack it to work around the new found realities.

One example is that either lsdvd or libdvdread is buggy in how it outputs chapter information.  Actually, my whole experience with chapters have been that if there are any oddities, then the players will just freak out.  You wouldn’t believe the cases I ran into.  Anyway, here’s a small example.  On one DVD, lsdvd will report in original output that one track has 30 chapters on it.  But when you go to display the chapters, it will only say that there are two.  Most of the time, what happens, is that it will choke anytime there is a chapter between others that is zero length.  In this case, lsdvd just chokes and stops counting them.  MPlayer (at least, the ancient version I’m using) will do a couple of things depending on its mood — sometimes freeze, sometimes skip over it, sometimes act like its not even there.  It’s very odd.  I’ve found a lot of interesting little bugs in the dvd libraries and tools.  I’d love to poke and the source and fix them up … when I have time.

The code is online in my svn repo, and the new one is called ‘drip’ for dvd ripper.  Original, I know, but eventually it will replace bend completely once I add in all the features the old one had plus all the new stuff I want.  I would throw in a link to trac which has prettier display output for viewing SVN files, but my installation is broken (again) and I have no idea why, and it’s always a royal pain trying to figure out what went wrong, so I’ll just fix it later.  I love trac, but its not easy debugging the setup.

Oh yah, also I’ve been working on my mythvideo setup, tweaking it even more.  One really thing that dawned on me, which I’ll write in more detail once I actually have a script ready, is that you can use it to execute shell scripts using the File Types admin menu.  Just tell it to execute .sh files in your folders with /bin/bash and away you can go.

Another thing I learned is that MythVideo will only pass two variables to any external scripts, the default player (%d) and the video file (%s), or more accurately, the file you’ve selected to run.  So if you wanted to see what you’re executing, you would add this to the file type for .sh files: /bin/bash %s %s

Then, say you had test.sh, this would be the contents:

#!/bin/bash

echo $a

I’m getting ahead of myself, though .. I’ll write more about that when I’ve got something to show.  I’m actually working on a shell script similar to mplayer-resume to resume playback of a playlist you’re in.  It’s a bit trickier than I thought it would (or rather, not nearly as simple as I had hoped), so I’m still scoping it out in my head.

Speaking of mplayer-resume, I fixed a bug I kept running into with it for a while now.  The script will now catch the exit code of mplayer, and if it’s not successful (zero), then it won’t overwrite or delete the old position.  I used to hit it all the time because I used to run mplayer -hardframedrop when playing my videos, which would crash the playback about 10% of the time and of course kill the file that had the playback position.  I need to repackage it and push it live, but there’s a few more small fixes I want to make to it first … I might finish the playlist resume script first and add it to there.  Plus I want to get trac working, because that’s where it’s homepage is.

But, I moved my mini-itx to the living room and hooked it up to my HDTV.  It was sitting in my bedroom just collecting dust, and I figured I might as well move it to see if it gets any more usage.  Actually, I remember now, I moved it was because the LED lights were really bright in my bedroom at night, and I have to sleep in total darkness to get a good night’s rest.   Anyway, it’s worked out well so far.  My TV has a VGA port so it’s super simple to plug it in, not to mention I like the fact that it doesn’t use up an HDMI port.  I love my TV. :)  Once I have this series playlist resume script finished, I think I’ll be pretty much “done” with having the setup that I’ve wanted so long.  Well, aside from the fact that I need about 12 more terabytes of harddrive space.

Good times, I tell you what.  I’m gonna go watch some Star Trek TNG.

2 Comments

Filed under bend / dvd2mkv, Gentoo, Multimedia

mplayer-resume-1.5

A bug in MythVideo inspired me to work on fixing mplayer-resume tonight, so that it can properly handle movies with filenames.  I don’t know why I didn’t think about this before, but it’s simple if the file is properly escaped or quoted.  And so, mplayer-resume v1.5 is released, with support for spaces in filenames, finally, and also one other cool little thing: it works with playlists now, to a degree.

The playlists thing is kind of hard to explain, and it’d be easier to point you to the documentation that I’ve already written.  Instead, I’ll just describe what it is I’m going to use it for.

One thing I’ve been wanting to add to my MythVideo setup is some playlists so that I can randomly play something.  I have a lot of cartoons and videos and movies, and sometimes I don’t feel like picking something myself — one of the nice things about TV in general is you are genuinely surprised when you’re channel surfing and something cool just happens to crop up.  That’s kinda what I like, and what I wanted to do.  But, I wanted to take it a step further.  If I started playing $random_episode, then if I quit, I want to be able to resume playback of that same show.  Up until now, mplayer-resume wouldn’t work that way, since if you’re randomly picking something from a playlist file, there’s no real way to seek back to the same one.

That’s fixed now.  The script will read the filename of the movie you are playing when you exit (once you setup .lircrc correctly), and checks to see if that’s the same file you started playing.  So if I play random.pls and it plays Tarzan.mkv, and I exit, then when I go back to watch Tarzan, it will resume in the same place.  Basically, it saves the file position for Tarzan instead of the playlist file.  Pretty cool. :)

So, there you go.  I’ll put it in portage shortly as well.  Enjoy. :D

Leave a comment

Filed under bend / dvd2mkv, Entertainment, Gentoo, MPlayer, Multimedia, MythTV

preparing for dts

Now that I’ve got my mythbox all setup properly (I screwed up LVM2 and lost all my data, twice, trying to remove harddrives … meh), I’m back to ripping my DVDs again.  This time I’m using dvd2mkv, my custom little script I wrote, to do all the heavy lifting for my movies.  But, there’s one thing I didn’t really put in there the first time around, and that’s support for alternate audio tracks.

Originally I wrote it simply to check automatically for the highest number of tracks and best audio format.  As a general rule, that chooses the first channel that’s in English with 6 channels, which is always (in every case I’ve seen so far), Dolby Digital.  If there is a DTS track, it’s always the second or third track behind it, but never gets selected automatically since it’s not the first one on the list.  I can, however, select it if I run the program interactively.  Not really ideal, of course, but it’ll have to work for now.

My real question though is, why aren’t there more movies with DTS audio tracks to them?  Back in the day when I was working at a movie theater, one of my managers would swear up and down that DTS was better quality than Dolby.  He would even make the projectionist screen the movies for him in the DTS  theaters if the movie was equipped for it.  He was quite the audio and videophile so I took his word for it.  Now, though, you hardly see it anywhere.  The only DVDs I’ve seen them on are some Paramount and Fox titles, and even then it’s only the newer ones that have it.

What’s also really interesting, and I kind of assumed this, is that SDDS, Sony’s 8 channel format, is completely missing from a home theater setup.  Good ol Sony, going off and making their own standard yet again.

Anyway, when I listen to DVDs with both tracks, I really can’t tell a difference myself.  My receiver supports both DTS and Dolby, so I figure … why not, I’ll rip em anyway and see if it really is any nicer.

2 Comments

Filed under bend / dvd2mkv, Entertainment

dvd ripping scripts

I was lucky enough to catch Martin’s post on Planet Larry today about undvd, a script he’s working on — very cool, Martin, I love it.  I’m actually working on one of my own, or have been for a while, and I’ve been toying with the idea of cleaning it up and releasing it for public consumption.

I actually started writing my own because, as Martin says, all the other ones out there are too complex, and I prefer a simple command-line app that does exactly what I need it to do.  I call mine dvd2mkv, since I extract everything I want (video, audio, subtitles, chapters) and dump it straight into a Matroska file.

In fact, the script is already done, and it works great.  It automates the entire process — selects the longest video track, the widescreen one if there if full frame is on there as well, grabs the English (or preferred language track) with the highest number of channels (Dolby or DTS), plus the English subtitles if they exist, and finally the chapters.  All I have to do is put in the title of the movie, and even then if the disc ID is the title, then you can skip past that as well.

In fact, here’s the output of a movie I just ripped a few minutes ago, Return to Me.  Great movie, btw.

steve@charlie ~/dvd $ dvd2mkv
[DVD] Disc title: RETURN_TO_ME
Enter a movie title: [Return To Me]
[Video] Track number: 5
[Video] Aspect ratio: 16/9
[Video] Length: 115.79
[Audio] Track: 128
[Audio] Format: Dolby Digital
[Audio] Channels: 6
[DVD] Subtitles: None
[DVD] Ripping MPEG-2
[DVD] Ripping chapters
[MKV] Creating Matroska file

And that’s it!  Pretty simple. :)  Mine doesn’t have support (right now) for re-encoding the movies, since I’ve already gone into quite a bit of length on why I don’t like doing that, but it would be simple to add.  In fact, my shortcut method would just be to have the user setup mencoder profiles in the config file and call those directly.

Anyway, I like the idea of cleaning it up and throwing it out there, so I’ll probably be doing that fairly soon here.  If it works good enough for me (picky as I am), it’s sure to help out someone.  :)

3 Comments

Filed under bend / dvd2mkv, Gentoo, Multimedia