fREWdiculous!
31 May
I recently got a new car stereo due to the other one being stolen. I am almost entirely happy with the model that I ended up purchasing, but one thing that it does, which is really obnoxious, is that it doesn’t sort the files correctly unless the track number is early on in the file name. Even if all tracks are “FooBarBaz 01 – name.mp3″ it seems to ignore the number unless it’s the very beginning of the file name. Anyway, easy fix:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #!perl use strict; use warnings; use feature ':5.10'; use Music::Tag; use File::Find::Rule; use File::Basename "fileparse"; use File::Copy "move"; use File::Spec; my $directory = shift || '.'; my @songs = File::Find::Rule->file()->name( '*.mp3' ) ->in( $directory ); foreach my $song (@songs) { my $info = Music::Tag->new($song); $info->get_tag; my $track = $info->track; my $title = $info->title; my (undef, $dir, $suffix) = fileparse($song, qr/\.[^.]*/); $info->close; if ($track and $title) { my $newfilename = File::Spec->catfile( $dir, sprintf "%02d %s%s", $track, $title, $suffix ); if ($song ne $newfilename) { say "renaming $song to $newfilename"; move $song, $newfilename; } } } |
It doesn’t really deal with illegal characters, it just doesn’t rename those files. Eventually I’ll get around to doing that. Anyway, just figured someone might be interested/want to copy paste this.
3 Responses for "Script to Rename MP3′s"
I also had this (and other..) problems and wrote some tools to solve it. The code was just lying around on my harddisk, but I now pushed it to github (and will put it on CPAN soon):
http://github.com/domm/App-FixFilenames/
Maybe you can extract some bits you like, or join me working on my codebase
PS: Some cheap MP3 players (based on USB flash drives) do not even sort the files by name, but by inode creation time (or something similar obscure). The only way to get songs on correct order on those player is to copy them in the correct order. Which most OSes don’t do, if you just copy a whole directory. I plan to add a feature to App-FixFilenames for solving that problem (hm, maybe I’ll just f*king do it now..)
Oh, and Music::Tag looks very interesting, too…
Lost in the CPAN…
A Foolish Manifesto has an interesting post on renaming MP3 files. It’s a simple little script, the likes of which I have written several times over the years. Yet I don’t recognize any of the modul……
Leave a reply