We Recommend

Learning Perl Learning Perl
In this smooth, carefully paced course, a leading Perl trainer teaches you to program in the language that threatens to make C, sed, awk, and the Unix shell obsolete for many tasks. This book is the "official" guide for both formal (classroom) and informal learning. It is fully accessible to the novice programmer.


Posted By

noah on 07/03/07


Tagged

regex file tags filter name copy mp3 id3 collection rename utilities 2001 metadata


Versions (?)


id3.pl


Published in: Perl 


This script takes a list of directories as its argument. For mp3s that don't have any id3 information, guess
the name of the artist and track based on the filename
and use the MP3-Info module to insert that guess into
the file as id3v1 tags.

  1. #!/usr/bin/perl -w
  2. use strict;
  3. use MP3::Info;
  4.  
  5. #############################################################
  6. # #
  7. # #
  8. # #
  9. # NOAH SUSSMAN #
  10. # #
  11. # id3 #
  12. # #
  13. # Created 6/27/01 at 11:07 PM #
  14. # #
  15. # This script takes a list of directories as its argument. #
  16. # For mp3s that don't have any id3 information, guess #
  17. # the name of the artist and track based on the filename #
  18. # and use the MP3-Info module to insert that guess into #
  19. # the file as id3v1 tags. #
  20. # #
  21. #############################################################
  22.  
  23. # get_mp3tag returns the following hash values: TITLE, ARTIST, ALBUM, YEAR, COMMENT, GENRE, TRACKNUM
  24.  
  25. foreach my $dir (@ARGV){ # Get a list of directories from the command line, then cycle through each directory performing the following:
  26.  
  27. chdir $dir or die qq{Can't open the directory "$dir" because : $!\n} if $dir; # Go to the directory.
  28.  
  29. # search nested directories
  30.  
  31. while (<*>) { #for all files in the directory
  32.  
  33. if (-d "$_"){ # If the current file is a directory.
  34. my $look_here = $dir."\\$_"; # Add the full path to the directory name
  35. #$look_here =~ s/::/:/g; # UNCOMMENT THIS LINE ON MAC ONLY: Remove doubled colons (this fixes what I think may be a bug in MacPerl)
  36. push @ARGV, $look_here; # Add the directory name to @ARGV
  37. next;
  38. }
  39.  
  40. }
  41.  
  42. # add id3 tags to files that don't have them
  43.  
  44. while (<*.mp3>) { # Then do the following to files that have the .mp3 file extension:
  45.  
  46. if (get_mp3tag($_)){ # if the current file already has id3 tags, skip it!
  47. #print "Skipping $_ because it already has id3 tags!\n\n";
  48. next;
  49. }
  50.  
  51. my $name = $_; # copy the current file's full name into $name
  52.  
  53. $name =~ s{
  54. (.*?) # capture the filename up to
  55. \.mp3 # but not including the ".mp3" file extension
  56. }{
  57. $1
  58. }ix;
  59.  
  60. # Now, most of my mp3s are named something like: "Tom Waits- Small Change.mp3", so I'm going to treat the name of the current file as a dash-delimited list, where the list items are ARTIST NAME - TRACK NAME. If there are more than two items in the list, they'll be ignored-- for now.
  61.  
  62. my @name = split (/-/, $name); # Store each item in our tab-delimited list in an array called @name
  63.  
  64. my $counter = 0;
  65.  
  66. foreach my $i (@name) { # For each item stored in the @name array
  67. $i =~ s{^\s*(.*?)\s*$}{$1}g; #destroy trailing and leading whitespace
  68. }
  69.  
  70. my $artist = $name[0]; # The name of the artist (hopefully!)
  71.  
  72. my $song = $name[1]; # (hopefully) the name of the track
  73.  
  74. # The script thinks "Gil Scott-Heron" is a dash-delimited list! So I wrote the Gil Scott-Heron filter:
  75.  
  76. my $gsf = $artist . $song ;
  77.  
  78. if ($gsf =~ /gil scottheron/i){ #If $artist is Gil Scott and $song is Heron, that's very wrong.
  79. $artist = "Gil Scott-Heron"; # The artist's name is actually Gil Scott-Heron
  80. $song = $name[2]; # Get the track name from the array, where it's been waiting patiently.
  81. }
  82.  
  83. # Same problem with "Deee-Lite"
  84. elsif ($gsf =~ /deeelite/i){
  85. $artist = "Deee-Lite";
  86. $song = $name[2];
  87. }
  88.  
  89. # Same problem with "The Chi-Lites"
  90. elsif ($gsf =~ /chilites/i){
  91. $artist = "Chi-Lites";
  92. $song = $name[2];
  93. }
  94.  
  95. # Same problem with "T-Rex"
  96. elsif ($gsf =~ /trex/i){
  97. $artist = "T-Rex";
  98. $song = $name[2];
  99. }
  100.  
  101. #$song =~ s{\.mp3}{}gi; # NT ONLY: I know I did this above, but that doesnt seem to work on my old NT workstation here at work, so I'm stripping the file extension again...
  102.  
  103. # set_mp3tag (FILE, TITLE, ARTIST, ALBUM, YEAR, COMMENT, GENRE [, TRACKNUM]) is the syntax used by the MP3-Info module for defining the ID3v1 tags on an MP3 file
  104.  
  105. set_mp3tag ($_, $song, $artist, "", "", "I love music!"); # write the track and artist names to the mp3 file and that's it!
  106.  
  107. }
  108.  
  109. }

Report this snippet 

You need to login to post a comment.