User Tools

Site Tools


fillplayer.pl

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
fillplayer.pl [2006/11/13 13:39]
209.8.40.30
fillplayer.pl [2006/11/13 14:32] (current)
andi old revision restored
Line 1: Line 1:
 +====== 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 [[http://​www.digitalnetworksna.com/​shop/​_templates/​item_main_Rio.asp?​model=267|Rio Carbon Player]] with music from my collection (I leave some space for manual selections).
 +
 +<code perl>
 +#​!/​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;
 +}
 +</​code>​
fillplayer.pl.txt ยท Last modified: 2006/11/13 14:32 by andi