User Tools

Site Tools


fillplayer.pl

fillplayer.pl

This Script takes random mp3 files from $MP3LIB and copies them to $OUTDIR until a $MAXSIZE bytes were copied - It preserves directory structures. I use this script to fill my Rio Carbon Player with music from my collection (I leave some space for manual selections).

#!/usr/bin/perl
 
my $MAXSIZE=3*1024*1024*1024; #3 Gigs
my $MP3LIB="/ftp/mp3z/music";
my $OUTDIR="/mp3player/auto";
 
use File::Basename;
use File::Copy;
 
$|=1;
print "loading available files... ";
my @LIST = &getlist('',$MP3LIB);
print "found $#LIST files.\n";
 
print "cleaning outdir... ";
system("rm -rf $OUTDIR/");
print "done.\n";
 
print "copying files...\n";
my $s = 0;
while ($s < $MAXSIZE){
  my $file = splice(@LIST,int(rand scalar(@LIST)),1);
  $s += -s "$MP3LIB/$file";
  mkdirp("$OUTDIR/".dirname($file));
  copy("$MP3LIB/$file","$OUTDIR/$file");
  print "$file\n";
}
print "done.\n";
 
sub getlist($$){
  my $path = $_[0];
  my $base = $_[1];
 
  opendir(DIR,"$base/$path");
  my @files = readdir(DIR);
  closedir(DIR);
  my @list;
  foreach my $file (@files){
    next if($file =~ /^\./);
    my $full = "$path/$file";
    if(-d "$base/$full"){
      push(@list,&getlist($full,$base));
    }elsif($file =~ /\.mp3/i){
      push(@list,$full);
    }
  }
  return @list
}
 
sub dbg(){
  unless ($QUIET){
    print STDERR @_;
    print STDERR "\n";
  }
}
 
sub mkdirp(){
  my $dir = shift();
  my @parts = split('/',$dir);
  my $path = '';
  foreach my $part (@parts){
    $path .= "/$part";
    unless(-d $path){
      my $ok = mkdir($path);
      return $ok unless($ok);
    }
  }
  return 1;
}
fillplayer.pl.txt · Last modified: 2006/11/13 14:32 by andi