Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Random-fill media device

by colakong (Initiate)
on Sep 19, 2008 at 02:22 UTC ( [id://712418]=sourcecode: print w/replies, xml ) Need Help??
Category: Audio Related Programs
Author/Contact Info Cedric Nelson (cedric.nelson@gmail.com)
Description: A script to fill a flash device with randomly-selected music from your collection. I created this to fill my Samsung S3 (using the Korean UMS firmware), as Rhythmbox doesn't have an auto-fill feature.
#!/usr/bin/perl
#fill_player.pl
#Fill a flash device with randomly-selected songs from your collection
#Copyright Cedric Nelson, 2008
#cedric.nelson@gmail.com

use File::Find;
use File::Copy;

use strict;

my ($source_dir,
    $destination_dir,
    %music_index,
    @music_file_list,
    $music_file_count,
    $total_file_size,
    $remaining,
    $percent_used,
    $random_file_index,
    $random_file_size,
    @transfer_list,
    $transfer_file_count);

$source_dir = '/home/cedric/Music';
($destination_dir) = @ARGV;
unless ($destination_dir) {$destination_dir = '/media/S3/Music';}

#Build music file index
find(\&index_files, $source_dir);
while (my ($file, $size) = each %music_index) {
  if ($file =~ /\.mp3$/) {
    push @music_file_list, $file;
    $total_file_size += $size;
  }
}

$music_file_count = @music_file_list;
($remaining, $percent_used) = calculate_free($destination_dir);

print "Source dir: $source_dir\nFile count: $music_file_count\nTotal f
+ile size (KB): $total_file_size\n\n";
print "Destination dir: $destination_dir\nPercent used: $percent_used\
+nRemaining space (KB): $remaining\n\n\n";

#Generate list of randomly-selected files
until ($music_file_count == 0) {
  $random_file_index = int(rand($music_file_count + 1));
  $random_file_size = $music_index{$music_file_list[$random_file_index
+]};  
  if ($music_file_list[$random_file_index] && $random_file_size < $rem
+aining) {
    ##print "Selected file: $random_file_index, $music_file_list[$rand
+om_file_index], $random_file_size\n";
    push @transfer_list, (splice @music_file_list, $random_file_index,
+ 1);
    $remaining -= $random_file_size;
    ##print "Estimated remaining: $remaining\n";
    $music_file_count = @music_file_list;
  }
  else {
    splice @music_file_list, $random_file_index, 1;
    $music_file_count = @music_file_list;
  }
}

print "Random playlist:\n";
for (@transfer_list) {
  print "$_\n";
}
$transfer_file_count = @transfer_list;
print "Total: $transfer_file_count\n";
print "Estimated remaining space (KB): $remaining\n\n\n";

#Copy files to media player
print "Copying...\n";
print "Used\tFiles\tRemaining space (KB)\n";
until ($transfer_file_count == 0) {
  my $file = shift @transfer_list;
  $transfer_file_count = @transfer_list;
  copy("$file","$destination_dir/$_") or warn "Copy failed: $!";
  ($remaining, $percent_used) = calculate_free($destination_dir);
  print "$percent_used\t$transfer_file_count\t$remaining\n";
}
print "Done.\n";

sub calculate_free {
  my ($dir) = @_;
  my ($free_space,
      @free_space,
      $used_percent);
  
  $free_space = `df $dir`;
  @free_space = split /\n/, $free_space;
  shift @free_space;
  $free_space = shift @free_space;
  #Example output:
  #/dev/sdc1              3889424   1869992   2019432  49% /media/S3
  @free_space = split /\s+/, $free_space;
  $free_space = $free_space[3];
  $used_percent = $free_space[4];
  #Returned value is in KB
  return ($free_space, $used_percent); 
}

sub index_files {
  #Filesize is returned in bytes
  my $filesize = -s "$File::Find::name";
  $filesize = int($filesize / 1024);
  unless ($music_index{$File::Find::name}) {$music_index{$File::Find::
+name} = $filesize;}
}
Replies are listed 'Best First'.
Re: Random-fill media device
by Xenofur (Monk) on Sep 19, 2008 at 10:14 UTC
    Thanks for sharing this. I was recently pondering writing something like that for my mp3 player and you just saved me a bunch of time. :)

    Some ideas on how this could be improved: Possibility to exclude files above a certain size. Possibility to have it ignore files with certain strings in their names. I'll be adding those for myself anyhow, but figured you might want to know about them to update yours if you like.

      I like those ideas, Xenofur. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://712418]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 00:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found