Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Mp3 Renamer

by Anonymous Monk
on Jan 07, 2001 at 09:59 UTC ( [id://50319]=CUFP: print w/replies, xml ) Need Help??

I am not sure about the rest of you, but I like to have my collection of Mp3's all in the same format (which makes sorting much easier). By this I mean having all mp3's named 'artist - name.mp3'. Here is some code to rename a directory of Mp3's into that format.
#!/usr/bin/perl print "Enter the <dir> where the mp3's are found: "; chomp ($dir = <stdin>); chdir($dir) or die "Canot change $dir :$!\n"; opendir(DIR, $dir) or die "Cannot open $dir:$!\n"; @songs = readdir(DIR); closedir(DIR); @songs=grep(/(.mp3)|(.MP3)|(.mP3)|(.Mp3)/,@songs); foreach(@songs) { if($_ =~ m/^(.*)\s*-\s*(.*).mp3/i) { my $end = $2; my $start = $1; $start =~ s/\s*$//; s/{|}//g for ($start, $end); rename $_,"$start - $end.mp3"; print "$start - $end.mp3\n"; } }

Replies are listed 'Best First'.
Re: Mp3 Renamer
by myocom (Deacon) on Jan 07, 2001 at 10:43 UTC

    This works, but it assumes that your files at least are in a format similar to artist-title.mp3. You might consider adding (artist)_title.mp3 or other common formats.

    Even more interesting would be to use MP3::Info to get information out of the ID3 tag to help determine if there's an artist/title in the filename, then rename it accordingly.

      Good idea - I wanted to point out that when checking ID3 tags using MP3::Info that you should always first make sure that there is one:
      while(<FILES>) { my $tag = get_mp3tag($file) or next; # do stuff }
      otherwise you could get some nasty results. ;)

      Jeff

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      F--F--F--F--F--F--F--F--
      (the triplet paradiddle)
      
      Thanks for the replies. Here is an updated snippet:
      #!/usr/bin/perl print "Enter the <dir> where the mp3's are found: "; chomp ($dir = <stdin>); chdir($dir) or die "Canot change $dir :$!\n"; opendir(DIR, $dir) or die "Cannot open $dir:$!\n"; @songs = readdir(DIR); closedir(DIR); @songs=grep(/(.mp3)|(.MP3)|(.mP3)|(.Mp3)/,@songs); foreach(@songs) { @id = id3check($_); if($id[0] && $id[1]) { rename $_,"$id[0] - $id[1].mp3"; print "$id[0] - $id[1].mp3\n"; } elsif($_ =~ m/(\s*[^-]*)-\s*([^.]*).mp3/i) { my @id; $id[0] = $1; $id[1] = $2; s/\s*$|{|}//g for ($id[0], $id[1]); rename $_,"$id[0] - $id[1].mp3"; print "$id[0] - $id[1]\n"; } else { print "Could not rename $_\n"; } } sub id3check { if (open(FILE, '+<' . $_[0])) { my @id; binmode FILE; seek FILE,-128,2; read FILE,$tag,3; if($tag eq "TAG") { read FILE,$id[0],30; read FILE,$id[1],30; s/\s*$|{|}//g for ($id[0],$id[1]); return @id; } } }
                @songs=grep(/(.mp3)|(.MP3)|(.mP3)|(.Mp3)/,@songs); That's a good spot to use the /i regex modifier, which specifies case-insensitive matching:     @songs = grep /\.mp3$/i, @songs Escaping the period and adding the $ anchor are important to make sure you don't match files like 'abcmp3' or 'foo.mp3.txt'.
Re: Mp3 Renamer
by AltBlue (Chaplain) on Jan 16, 2001 at 23:10 UTC
    neh, for simple things there always are simple solutions!
    as myocom already adviced you, you should have taken a look to MP3::Info :(
    here is a simple oneliner that does all you wanted:

    find2perl . -depth -eval 'my $t; -f and /\.mp3$/i and $t = get_mp3tag( +$_) and rename $_, "$t->{ARTIST} - $t->{TITLE}.mp3"' |perl -MMP3::Inf +o

    --
    AltBlue.

(redmist) Re: Mp3 Renamer
by redmist (Deacon) on Jan 07, 2001 at 16:13 UTC
      What is the reason for this rule?

      While I agree that it is good to understand everything that Ovid said in that node, I don't agree that .* (or .*?) should be avoided as a matter of religious principle.

      But what do I know? I have been known to use goto. :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-28 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found