Feed on
Posts
Comments

For some reason MythTV’s built-in DVD player did not work well for me so I had to configure it to use MPlayer instead. That worked relatively well for some time, but every now and then there was a disc for which MPlayer would fail to automatically detect which track to play. Speaking of which, for those of you who maybe did not know, it tries to auto-detect it when you invoke it like this:

mplayer dvd://

When that would fail it would be pretty inconvenient to manually mess with MythTV setup and enter the correct track number so I wrote a quick and dirty Perl script to do it for me. It used to invoke lsdvd to find the longest track and run MPlayer on it. However lsdvd is also not perfect because it would fail on some discs because of some strict formatting checks, or at least that is what I thought. Well fix for that was easy - I just made use of MPlayer’s auto detection as a backup if lsdvd would fail.

This solution worked well so I decided to share it by posting here. You should be able to use it anywhere where you would normally use MPlayer, since it is supposed to transparently pass command line parameters to it, and just do it’s thing when it detects DVD playing mode.

I must admit having slightly re-arranged and cleaned up the script before making public therefore it is possible something got broken in the process. Either that or if you find some other problem with it please do not hesitate contacting me so I can post the improved version.

Here it goes:

#! /usr/bin/perl

use IPC::Open3;
use POSIX ":sys_wait_h";

# Parse some arguments we want to know about
$i = 0;
foreach (@ARGV) {
	$device = $ARGV[$i+1] if $_ eq '-dvd-device';
	$quiet = 1 if $_ eq '-quiet';
	if (/^dvd:\/{1,2}(\d*)/) {
		$dvd = $i;
		$track = $1 if $1 ne '';
	}
	$i++;
}

if (defined $dvd and not defined $track) {
	# Run lsdvd to get the longest track
	$pid = open3(\*IN, \*OUT, \*ERR, "lsdvd -Op $device");
	if ($pid) {
		$output = do { local $/; <OUT>  };
		waitpid $pid, 0;
		if ($? == 0) {
			eval $output;
			$ARGV[$dvd] = "dvd://$lsdvd{longest_track}";
			print "Longest track $ARGV[$dvd].\n" unless $quiet;
		}
	}
} elsif (defined $dvd and not $quiet) {
	print "Requested track $ARGV[$dvd].\n";
}

exec 'mplayer', @ARGV;

Trackback URI | Comments RSS

Leave a Reply