$Artist = encode_utf8($Artist);
$Album = encode_utf8($Album);
####
print "* Song: ", decode_utf8($info[0]), "\n";
print "* Artist: ", decode_utf8($info[2]), "\n";
print "* Album: ", decode_utf8($info[3]), "\n";
####
#!/us/bin/perl
use strict;
use warnings;
use Cwd;
use Audio::FLAC::Header;
use MP3::Info;
use MP3::Tag;
use sort '_qsort';
use File::Glob qw(:globally :nocase);
#use Encode qw(encode decode);
#use utf8;
use Encode;
our $DEBUG = 0;
sub Get_Artist_Album {
return ( split m!/!, cwd() )[-2,-1];
}
sub Process_Files {
my @MP3_Files = <*.mp3> ;
my @FLAC_Files = <*.flac> ;
my $Num_MP3_Files = @MP3_Files;
my $Num_FLAC_Files = @FLAC_Files;
my ($Artist, $Album) = Get_Artist_Album;
if ($Num_MP3_Files > 0) {
# $Artist = encode_utf8($Artist);
# $Album = encode_utf8($Album);
foreach my $MP3_File (@MP3_Files) {
chomp $MP3_File;
if ($MP3_File =~ /^(\d+[\.\_\-\ ]+)(.*)(\.\w+)$/) {
my ($Track, $Title, $Ext) = ($1, $2, lc($3));
$Track =~ s/^(\d+)(.*)/$1/;
$Track = sprintf("%02d", $Track);
$Title = Format_Text ($Title);
# $Title = encode_utf8($Title);
my $New_File = "$Track. $Title$Ext";
if ($DEBUG) { print "\t$New_File\n"; }
rename ($MP3_File, $New_File) unless $MP3_File eq $New_File;
remove_mp3tag($New_File,'ALL');
my $mp3 = MP3::Tag->new($New_File);
# my $id3v1 = $mp3->new_tag("ID3v1");
# $id3v1->all($Title,$Artist,$Album,"","",$Track,"Rock");
# $id3v1->write_tag;
my $id3v2 = $mp3->new_tag("ID3v2");
$id3v2->add_frame('TRCK',$Track);
$id3v2->add_frame('TIT2',$Title);
$id3v2->add_frame('TPE1',$Artist);
$id3v2->add_frame('TALB',$Album);
$id3v2->add_frame('TCON',"17");
$id3v2->write_tag;
}
}
}
if ($Num_FLAC_Files > 0) {
foreach my $FLAC_File (@FLAC_Files) {
chomp $FLAC_File;
if ($FLAC_File =~ /^(\d+[\.\_\-\ ]+)(.*)(\.\w+)$/) {
my ($Track, $Title, $Ext) = ($1, $2, lc($3));
$Track =~ s/^(\d+)(.*)/$1/;
$Track = sprintf("%02d", $Track);
$Title = Format_Text ($Title);
my $New_File = "$Track. $Title$Ext";
if ($DEBUG) { print "\t$New_File\n"; }
rename ($FLAC_File, $New_File) unless $FLAC_File eq $New_File;
my $flac = Audio::FLAC::Header->new($New_File);
my $tags = $flac->tags();
%$tags = ();
$tags->{TRACKNUMBER} = $Track;
$tags->{TITLE} = $Title;
$tags->{ARTIST} = $Artist;
$tags->{ALBUM} = $Album;
$tags->{GENRE} = "Rock";
$flac->write();
}
}
}
}
sub Format_Text {
my $Text = $_[0] or exit 1;
$Text = lc($Text); #Make everything lowercase
$Text =~ tr/_/ /; #Remove underscores
$Text =~ s/\[/\(/g;
$Text =~ s/\]/\)/g;
$Text =~ tr/ / /s; #Remove unnecessary spaces
$Text =~ s/\.$//; #Some titles have an extra period - bye
$Text =~ s/(\d)\./$1/g; #Do not need period after numbers here
my @Words = split(/ /,$Text);
foreach my $Word (@Words) {
$Word = ucfirst($Word);
}
$Text = "@Words";
$Text =~ s/([(-])([a-z])/$1\u$2/g;
$Text =~ s/(\W'\S)/uc($1)/eg; #Some items following ' should be uc
$Text =~ s/(\.)([a-z])/$1\u$2/g; #Letter.Letter.Letter... is uc
$Text =~ s/Dis[ck]\ /Cd/;
$Text =~ s/Dis[ck](\d)/Cd$1/;
$Text =~ s/Cd\ (\d)/Cd$1/;
$Text =~ s/\((Cd\d+)\)/$1/;
my $x = $Text =~ tr/(/(/; #Count open parens
my $y = $Text =~ tr/)/)/; #Count closing parens
if ($x > $y) {
$Text = $Text.")";
}
return ($Text);
}
Process_Files;
my $Artist_Dir = cwd();
opendir (Artist_DH, $Artist_Dir) || die "can't opendir $Artist_Dir: $!";
my @Albums = grep { !/^\./ && -d "$_" } sort readdir(Artist_DH);
foreach my $Album (@Albums) {
my $NewAlbum = Format_Text ($Album);
rename ($Album, $NewAlbum) unless $Album eq $NewAlbum;
if ($DEBUG) { print "$NewAlbum \n"; }
chdir $NewAlbum or warn "Cannot change to $NewAlbum\n";
Process_Files;
chdir "..";
}
closedir Artist_DH;