Linux
Apache
MySQL
PHP

CSS
XHTML1.1
XML/RSS

Creative Commons

2007-09-03 21:40:55

Use Gripfoo to Fix Stupid Filenames

Since I totally switched to Linux, I haven't done any cd ripping. I used to use CDex on Windows and really liked it. That, however, isn't available for Linux, but there are a bingbobbitybillion other programs. After a minute of using my GooFu (a new word I learned that means using Google to solve a problem) I decided on grip as my cd ripping program.
I'll tell ya right now, it's a bear to use. This program fits the true definition of "user friendly" (it makes perfect sense to the programmer, but not to anyone else). But once I got my settings right, it works just as easily as any other program. I thought I had the naming convention set to my liking. I told it to use the artist, then a hyphen, then the track. What I didn't know was that grip converts everything to lowercase and replaces all spaces with underscores. 10 cds into the ripping process, I find this problem. :(
I took a few minutes and whipped up a Perl script to fix my filenames from the crappy way grip made them to my preferred way.
#!/usr/bin/perl ########################################################################## # gripfoo.pl # # This script is a quick and dirty way to change stupid filenames created # by grip to my normal naming standard. By quick and dirty, I really # mean super un-optimized, but it gets the job done. # # Usage: # Place this script in the same directory as the mp3s and execute # # Before: # artist_name - track_name.mp3 # # After: # Artist Name - Track Name.mp3 # ########################################################################## # Create an array with every mp3 file in the current directory @file_list = `ls -1 *mp3`; # Find out how many items are in the array $file_list_size = @file_list; for ($x = 0; $x < $file_list_size; $x++) { # Store each item into a variable, one at a time $mp3 = $file_list[$x]; # Create an array with the SPACE delimited tokens @mp3_words = split(" ", $mp3); # Find out how many tokens are in each item $mp3_words_size = @mp3_words; # Make sure the item only has 3 tokens (artist, separator, track) if ($mp3_words_size == 3) { # Place each of the 3 tokens into their own variable $artist = $mp3_words[0]; $separator = $mp3_words[1]; $track = $mp3_words[2]; # START WORK ON ARTIST # Assume double underscores used to be ampersands $artist =~ s/__/ & /g; # Replace single underscores with spaces $artist =~ s/_/ /g; # Create an array with the SPACE delimited tokens for $artist @artist_words = split(" ", $artist); # Clear the variable $artist = ""; # Supersize the first letter of each word in the array foreach $val (@artist_words) { $artist .= ucfirst($val) . " "; } # START WORK ON TRACK # Assume double underscores used to be ampersands $track =~ s/__/ & /g; # Replace single underscores with spaces $track =~ s/_/ /g; # Create an array with the SPACE delimited tokens for $artist @track_words = split(" ", $track); # Clear the variable $track = ""; # Supersize the first letter of each word in the array foreach $val (@track_words) { $track .= ucfirst($val) . " "; } # Compile the 3 tokens into the new filename $song = $artist . $separator . " " . $track; # Create the command to rename the file $command = "mv \"" . $mp3 . "\" \"" . $song . "\""; # Since the original filename contains a newline, remove it $command =~ s/\n//; # Since we put a space after each token, remove the last one $command =~ s/mp3 "/mp3"/; # Execute the command system($command); } else { print "Irregular match: $mp3\n"; } }


Back


Post a comment!

Name:
Comment: