Sign up to create your own snipts, or login.

Public snipts » mp3's The latest public mp3's snipts.

showing 1-1 of 1 snipts for mp3's
  • Load random mp3's to mp3 player
    #!usr/bin/perl -w
    # http://ubuntuforums.org/attachment.php?attachmentid=17255&d=1160475724
    # http://ubuntuforums.org/showthread.php?t=216102
    ######################################################################################
    # Author : Jan Roald Haugland janroald@gmail.com                                     #
    ######################################################################################
    # No effort is made to make this a good script :-)                                   #
    # Free to use, free to modify                                                        #
    # If something goes to hell; dont blame me. No guarantees provided...                #
    ######################################################################################
    ######################################################################################
    # Syntax :                                                                           #
    #   perl copyrandom.pl [source directory] [destination] ([max megabytes to transfer])#
    # Example :                                                                          #
    #   perl copyrandom.pl /home/you/mp3/ /media/ipod/ 500                               #
    ######################################################################################
    #500 is MB's you want to transfer (maximum). This is also default if u dont specify  #
    ######################################################################################
    
    ######################################################################################
    # Config #############################################################################
    
    # Add or remove extesions to be transferred here, separate with comma
    $extensions_allowed = 'mp3,ogg,wma';
    
    # Edit number of megabytes to be transferred if not specified as argument
    $default_mb = 500;
    
    # Only edit below if u know what u are doing
    ######################################################################################
    ######################################################################################
    
    # Parameters #########################################################################
    
    $source = $ARGV[0];
    if($source !~ m/\/$/) { $source = $source.'/'; }
    $destination = $ARGV[1];
    $max_mb = (!$ARGV[2]?$default_mb:$ARGV[2]);
    $extensions_allowed =~ s/,/|/g;
    
    # Other variables ####################################################################
    my $mb_total = 0;    #Start counting
    my $file = '';
    my @list = ();
    my @files = ();
    my %files_test = ();
    my $dir = '';
    ######################################################################################
    
    print "\n--- Looking for songs in $source ... ---\n\n";
    print "--- Copying maximum ${max_mb}MB's to $destination... ---\n\n";
     
    # Using shell 'ls' to list all files and directories
    @list = `ls -1pshR $source`; 
    splice(@list, 0, 1);
    
    # Looping through the list, selecting directories and songs
    # Generating songlist with trailing megabytes
    $x = 0;
    foreach(@list) {
      if($_ =~ /^\.\/(.*):$/) {
        $dir = "$1/";
        $dir =~ s/([\s\(\)!&'"`\[\],\|:])/\\$1/g;
      }
      if($_ =~ /^$source(.*):$/) {
        $dir = "$1/";
        $dir =~ s/([\s\(\)!&'"`\[\],\|:])/\\$1/g;
      }
    
      if( $_ =~ /^\s?([\d|\.]*)M\s((.*)\.($extensions_allowed))$/i ) {
        $file = $2;
        $mb = $1;
        $file =~ s/([\s\(\)!&'"`\[\],\|:])/\\$1/g;
        push(@files, "$dir$file|$mb");
      }
    $x++; 
    }
    
    # Picking out random files and copying to destination. Making same song isn't copied twice
    $number_of_files = @files; 
    while($max_mb > $mb_total) {
      $random = rand($number_of_files);
      ($song,$size) = split('\|',$files[$random]); 
      if(!$files_test{$song}) {
        $mb_total+=$size;
        $files_test{$song} = 1;
        if($max_mb > $mb_total) {
          `cp $source$song $destination`;
          print "### Transferred file $song ### ${size}MB's	### ${mb_total}MB's total ###\n";
        }
      } 
    }
    

    copy | embed

    0 comments - tagged in  posted by rschu68 on Jan 05, 2010 at 8:58 a.m. EST
Sign up to create your own snipts, or login.