#! /usr/bin/perl -w
use strict;

$ENV{PATH} = '/bin:/usr/bin/:/usr/local/bin';

my $titles;
# read global info
open MPLAYER, "echo -n q|mplayer -nosound -v -vo null -dvd 1 2>&1|" or die;
while (<MPLAYER>) {
	if (/There are (\d+) titles on this DVD/) {
		$titles = $1;
	} elsif (/^\[open\] audio stream: (\d+) audio format: (.*) language: (.*) aid: (.*)$/) {
		printf "aid %3d: %s / %s\n", $4, $3, $2;
	} elsif (/^\[open\] subtitle \( sid \): (\d+) language: (.*)$/) {
		printf "sid %2d: %s\n", $1, $2;
	}
}
close MPLAYER;

unless ($titles) {
	print "DVD missing or busy";
	exit;
}

# read each title (slow, but works)
for my $i (1 .. $titles) {
	my ($chapters, $angles) = (0, 0);
	open MPLAYER, "echo -n q|mplayer -nosound -v -vo null -dvd $i 2>&1|" or die;
	while (<MPLAYER>) {
		if (/There are (\d+) chapters in this DVD title/) {
			$chapters = $1;
		} elsif (/There are (\d+) angles in this DVD title/) {
			$angles = $1;
		}
		last if ($chapters && $angles);
	}
	close MPLAYER;

	printf "title %3d: %3d chapters, %3d angles\n", $i, $chapters, $angles;
}

