Created
August 10, 2012 16:45
-
-
Save jacoby/3315484 to your computer and use it in GitHub Desktop.
My perl script for renaming MP3s to a more sane naming convention
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict ; | |
use warnings ; | |
use 5.010 ; | |
use subs qw( rename_mp3s rename_mp3 ) ; | |
use File::Copy ; | |
use MP3::Tag ; | |
print qq{RENAMING\n} ; | |
my %artists ; | |
my %artists2 ; | |
my $count = 0 ; | |
rename_mp3s '.' ; | |
print qq{\n\n} ; | |
sub rename_mp3s { | |
my $dir = shift ; | |
if ( opendir my $dh, $dir ) { | |
for my $f ( sort readdir $dh ) { | |
next if $f =~ m{^\.}mx ; | |
print qq{# $f\n} ; | |
my $d = join '/', $dir, $f ; | |
if ( -f $d and $d =~ m/mp3$/i ) { | |
my $mp3 = MP3::Tag->new( $d ) ; | |
my ( $title, $track, $artist, $album, $comment, $year, $genre ) = | |
$mp3->autoinfo() ; | |
$mp3->close() ; | |
if ( ( defined $artist ) | |
&& ( defined $track ) | |
&& ( defined $title ) | |
&& ( $artist =~ /\w/ ) | |
&& ( $track =~ /\d/ ) | |
&& ( $title =~ /\w/ ) ) { | |
$track = sprintf '%02d', $track ; | |
my $e = qq{$track - $artist - $title.mp3} ; | |
$e =~ s/\"//g ; | |
$e =~ s/\'//g ; | |
$e =~ s/\*//g ; | |
$e =~ s/\?//g ; | |
$e =~ s/\://g ; | |
$e =~ s/\[[^\]]*\]//g ; | |
$e =~ s/\&/ and /g ; | |
$e =~ s/\// and /g ; | |
next if $f eq $e ; | |
print qq{ $f\n} ; | |
move( $f, $e ) or die qq{Can't move file "$f": $!} ; | |
} | |
elsif ( ( defined $artist ) | |
&& ( defined $title ) | |
&& ( $artist =~ /\w/ ) | |
&& ( $title =~ /\w/ ) ) { | |
my $e = qq{$artist - $title.mp3} ; | |
$e =~ s/\"//g ; | |
$e =~ s/\'//g ; | |
$e =~ s/\?//g ; | |
$e =~ s/\://g ; | |
$e =~ s/\[[^\]]*\]//g ; | |
$e =~ s/\&/ and /g ; | |
$e =~ s/\// and /g ; | |
next if $d eq $e ; | |
print qq{ $f\n} ; | |
say "\t" . $d ; | |
say "\t" . $e ; | |
move( $d, $e ) or die qq{Can't move file \n\t$d\n\t $!} ; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment