Posted By

noah on 07/03/07


Tagged

file folder tags name copy mp3 move id3 rename utilities 2004 metadata


Versions (?)

Move MP3s into Artist/Album folders


 / Published in: Perl
 

Given a directory of MP3s, read the ID3 tags and reorganize the directory so that all of the MP3s reside in folders that are nested in the order: Artist/Album/File.mp3

  1. #!/usr/bin/perl -w
  2. use strict;
  3. use MP3::Info;
  4. use File::Copy;
  5. use File::Spec::Functions;
  6.  
  7. #Doesn't understand about compilation CDs.
  8.  
  9. my $format_for_priya = 0;
  10.  
  11. foreach my $tagged_mp3 (@ARGV) {
  12.  
  13. my $tags_hashref = get_mp3tag($tagged_mp3) or die "No TAG info";
  14.  
  15. my $track_number = 0 + $tags_hashref->{TRACKNUM};
  16. #Convert strings to numbers
  17.  
  18. $track_number = "0" . $track_number unless ($tags_hashref->{TRACKNUM} > 9);
  19. #Add leading zero to single digits
  20.  
  21. # my $new_file_name = "$tags_hashref->{ARTIST}/$tags_hashref->{ALBUM}/$track_number - $tags_hashref->{ARTIST} - $tags_hashref->{TITLE}.mp3";
  22. my $new_file_name = catfile($tags_hashref->{ARTIST}, $tags_hashref->{ALBUM}, "$track_number - $tags_hashref->{ARTIST} - $tags_hashref->{TITLE}.mp3");
  23.  
  24. if ($format_for_priya == 1) {
  25. #Don't include the artist, and truncate filename to 27 chars + extension
  26. my $priya_name = "$track_number-$tags_hashref->{TITLE}";
  27. $priya_name =~ s/(.{0,27}).*/$1/;
  28. $new_file_name = catfile($tags_hashref->{ARTIST}, $tags_hashref->{ALBUM}, $priya_name . ".mp3");
  29. }
  30.  
  31. $new_file_name = lc($new_file_name);
  32.  
  33. # print $new_file_name;
  34.  
  35. my $artist_dir = lc($tags_hashref->{ARTIST});
  36.  
  37. unless (-d $artist_dir) {
  38. mkdir($artist_dir, 0755) || die "Cannot mkdir $artist_dir: $!";
  39. }
  40.  
  41. # my $album_path = $artist_dir . "/" . lc($tags_hashref->{ALBUM});
  42. my $album_path = catfile($artist_dir , lc($tags_hashref->{ALBUM}));
  43.  
  44. unless (-e $album_path) {
  45. mkdir($album_path, 0755) || die "Cannot mkdir $album_path: $!";
  46. }
  47.  
  48. print "$tagged_mp3 Changed To:\n";
  49. print $new_file_name . "\n";
  50.  
  51. copy ($tagged_mp3, $new_file_name) or die "couldnt copy files because $!";
  52.  
  53. #system ("cp $tagged_mp3 $new_file_name");
  54.  
  55. }

Report this snippet  

You need to login to post a comment.