
jwhit61
Novice
Dec 26, 2008, 5:55 AM
Post #6 of 11
(4101 views)
|
|
Re: [KevinR] Track. Title format for audio files
[In reply to]
|
Can't Post
|
|
The season has created some delays on getting back to you. First, thanks for your input. I took the code you provided and below is my current process to 'standardize' my audio system. The structure for my files is: Artist/Album/Track. Title. I ran into some scenarios that were outside my original description. I also decided to run the directory names through the same code to ensure they were more controlled. Again, you provided me with some pieces (particularly the boundary code) that I had not seen before. Like many things, I'll keep playing around with this as I encounter other situations. This script below is modifying files that are already in place. I've got another script that copies from a source to the destination. The source files are less structured and may need more attention than we are doing here.
#!/usr/bin/perl use Audio::FLAC::Header; use MP3::Info; use MP3::Tag; sub GetArtistAlbum { $MyPath = `pwd`; #Get the number of slashes in the pwd my $I = $MyPath =~ tr /\//\//; my $J = $I+1; ($Artist,$Album) = split(/\//,`pwd | cut -d/ -f $I,$J`); chomp($Artist); chomp($Album); return ($Artist, $Album); } sub GetTrackTitle { my $file = $_[0] or exit 1; ($Track,$Title) = split(/[\s.-]/,$file,2); $Title =~ s/^[\s.-]+(.)/$1/; unless ($Track=~ /^\d+$/) { $Track=1; $Title=$file; } return ($Track, $Title); } sub ProcessFiles { my @MP3Files = `ls *.mp3 2>/dev/null`; my @FLACFiles = `ls *.flac 2>/dev/null`; my $NumMP3Files = @MP3Files; my $NumFLACFiles = @FLACFiles; my ($Artist, $Album) = GetArtistAlbum; if ($NumMP3Files > 0) { foreach my $MP3File (@MP3Files) { chomp $MP3File; if ($MP3File =~ /^(.*)(\.mp3)/) { ($file, $ext) = ($1, $2); $file = FormatText ($file); } my ($Track, $Title) = GetTrackTitle ($file); my $NewFile = "$Track. $Title$ext"; rename ($MP3File, $NewFile) unless $MP3File eq $NewFile; remove_mp3tag($NewFile, 'ALL'); $mp3 = MP3::Tag->new($NewFile); $id3v1 = $mp3->new_tag("ID3v1"); $id3v1->all($Title,$Artist,$Album,"","",$Track,"Rock"); $id3v1->write_tag; $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 ($NumFLACFiles > 0) { foreach my $FLACFile (@FLACFiles) { chomp $FLACFile; if ($FLACFile =~ /^(.*)(\.flac)/) { ($file, $ext) = ($1, $2); $file = FormatText ($file); } my ($Track, $Title) = GetTrackTitle ($file); my $NewFile = "$Track. $Title$ext"; rename ($FLACFile, $NewFile) unless $FLACFile eq $NewFile; my $flac = Audio::FLAC::Header->new($NewFile); my $tags = $flac->tags(); $tags->{TRACKNUMBER} = $Track; $tags->{TITLE} = $Title; $tags->{ARTIST} = $Artist; $tags->{ALBUM} = $Album; $flac->write(); } } } sub FormatText { my $NewText = $_[0] or exit 1; $NewText =~ tr/[A-Z]/[a-z]/; #Make everything lowercase $NewText =~ tr/_/ /; #Remove underscores $NewText =~ tr/ / /s; #Remove unnecessary spaces $NewText =~ s/\b([a-z]+)\B/ucfirst(lc($1))/eg; #Boundary ends with a Number $NewText =~ s/\b([a-z]+)\b/ucfirst(lc($1))/eg; #Boundary ends with a Char $NewText =~ s/([a-z])\.([A-Z])/$1 $2/g; #Remove unnecessary periods $NewText =~ s/('[A-Z])/lc($1)/eg; #Possesive nouns and others need lc $NewText =~ s/(\W'\S)/uc($1)/eg; #Some items following ' should be uc @x = $NewText =~ m/\(/g; #Count open parans @y = $NewText =~ m/\)/g; #Count closing parans unless (@x == @y) { $NewText = $NewText.")"; } return ($NewText); } chdir "/mp3"; my @Artists = `ls -F | grep / 2>/dev/null`; foreach my $Artist (@Artists) { chomp $Artist; $Artist =~ s/(.)\//$1/; my $NewArtist = FormatText ($Artist); rename ($Artist, $NewArtist) unless $Artist eq $NewArtist; # print "New$Artist \n"; chdir $NewArtist or warn "Cannot change to $NewArtist\n"; my @Albums = `ls -F | grep / 2>/dev/null`; foreach my $Album (@Albums) { chomp $Album; $Album =~ s/(.)\//$1/; my $NewAlbum = FormatText ($Album); rename ($Album, $NewAlbum) unless $Album eq $NewAlbum; # print "\t $NewAlbum \n"; chdir $NewAlbum or warn "Cannot change to $NewArtist-$NewAlbum\n"; ProcessFiles; chdir ".."; } chdir ".."; # print "\n"; }
|