[mca1001] / bin / m3u123.pl  

mca1001: bin/m3u123.pl

File: [mca1001] / bin / m3u123.pl (download) (as text)
Revision: 1.4, Fri Jun 3 20:49:19 2005 UTC (5 years, 3 months ago) by mca1001
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +2 -2 lines
[2004-08-06] ditch warning

#! /usr/bin/perl -w

# $Id: m3u123.pl,v 1.4 2005/06/03 20:49:19 mca1001 Exp $

=head1 NAME

m3u123.pl - call ogg123 or mpg123 for files in a playlist

=head1 DESCRIPTION

=head2 Options

=over 4

=item --shuffle | -s

Randomise the script's non-option arguments before using them.
Playlists still play in normal order.

=item --max-items | -n <num>

Start at most this many playlists.

=item --min-time | -t <min>

Continue playing for at least this many minutes.  If we run out of
playlist items before the time is up, loop.

=back

=cut


use strict;
use Getopt::Long;
use Cwd;

$ENV{PATH} = "/bin:/usr/bin";

my %opt = ('min-time' => 0);
GetOptions(\%opt, 'help|h', 'shuffle|s', 'max-items|n=i', 'min-time|t=f')
    or die "Syntax: $0 [--shuffle] [--max-items n] [--min-time m] album.m3u ...\n";

my @m3us = @ARGV;

if ($opt{'shuffle'}) {
    for (my $i=0; $i<@m3us; $i++) {
	my $swap = int(rand(@m3us));
	@m3us[$i, $swap] = @m3us[$swap, $i];
    }
}

if ($opt{'max-items'}) {
    splice @m3us, $opt{'max-items'};
}

if ($opt{'min-time'}) {
    $opt{'min-time'} *= 60;
    $opt{'min-time'} += time();
}


die "Nothing to do\n" unless @m3us;

do {
  LIST:
    foreach my $list (@m3us) {
	my $dir;

	while (1) {
	    $dir = $list;
	    $dir =~ s{/+[^/]+$}{\/};
	    $dir = cwd()."/" unless $dir =~ m{\/};
	    last unless -l $list;
	    # argh, gotta chase the symlink round else the relative names don't work
	    # I bet there's a neat module that does this
	    my $dest = readlink $list;
	    if (!defined $dest) {
		warn "Failed to readlink($list): $!, ignoring";
		next LIST;
	    }
	    $list = ($dest =~ m{^/}) ? $dest : "$dir$dest";
	    while ($list =~ s{/(?!\.\./)[^/]+/\.\./}{/}) {
	    }
	}

	if ( ! open LIST, "<", $list) {
	    warn "Failed to read '$list': $!";
	    next LIST;
	}

	my @tracks = <LIST>;
	close LIST;
	chomp @tracks;
	if (!@tracks) {
	    warn "Empty list '$list', ignoring";
	    next LIST;
	}

	my %type;
	foreach (@tracks) {
	    $type{lc($1)}++ if /\.(\w{2,5})$/;
	    next if m{^\/};	# absolute
	    s{^}{$dir};
	}
	my $types = "(".(join ", ", sort keys %type).")";
	warn "Multiple types $types found in $list - will take the first I like\n"
	    if 1 < scalar keys %type;
	my $prog;

	if ($type{ogg}) {
	    $prog = "ogg123";
	} elsif ($type{mp3}) {
	    $prog = "mpg123";
	} else {
	    $prog = "ogg123";
	    warn "Unknown types $types found in $list - will use $prog";
	}

	system( $prog, "-q", @tracks );
    }
} while (time() < $opt{'min-time'});

Repository owner

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help