#!/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;} }
In reply to Random-fill media device by colakong
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |