Huge refactor of tagfromfilename. We now support mp3 tags

This commit is contained in:
Anna Rose 2010-09-21 09:40:41 -04:00 committed by Anna
parent 6b4e37c667
commit 4124511783
2 changed files with 101 additions and 71 deletions

23
INSTALL
View File

@ -9,20 +9,27 @@ export PATH=$PATH:~/bin
Install ALL of the scripts, since some of them depend on others.
You will need the following perl modules:
CDDB (for cdripper)
CDDB_get (for cdripper)
Music::Tag::OGG (for tagfromfilename)
Music::Tag::FLAC (for tagfromfilename)
Music::Tag::MusicBrainz (for tagfromfilename)
You will need the following perl modules for cdripper to work:
CDDB
CDDB_get
These are only needed if you want to pass '-y' to tagfromfilename:
Music::Tag::OGG
Music::Tag::FLAC
Music::Tag::MP3
Music::Tag::MusicBrainz
You will also need the following programs:
cdparanoia
mplayer, mpg321, or mpg123
oggenc
cdrecord
oggenc
vorbiscomment
metaflac (for flac support)
metaflac
id3v2

View File

@ -9,7 +9,7 @@
#
# Adds Vorbiscomment data to ogg vorbis and flac files based on the filename.
# Files should be in Patrick's arbitrary file name standard, which is:
# artist/album/artist-tracknum-title.(ogg|flac)
# artist/album/artist-tracknum-title.(ogg|flac|mp3)
#
# The top-level directory (artist) is optional. Filenames should contain no
# spaces; underscores in the filename are converted to spaces in the resulting
@ -20,6 +20,8 @@
# Changelog:
# 0.2.4: added -T option
# added releasedate support through MusicBrainz
# added mp3 support and refactored code heavily
# added -y option to explicitly look for the year
# 0.2.3: added documentation, -0 command-line option
# 0.2.2: added flac support, documentation
# 0.2.1: fixed version in help message
@ -43,31 +45,57 @@ foreach (@ARGV)
elsif (/^-n$/) { $tracknumonly = true; s/.*//g; }
elsif (/^-T$/) { $captagnames = true; s/.*//g; }
elsif (/^-0$/) { $extrazero = true; s/.*//g; }
elsif (/^-y$/) { $get_year = true; s/.*//g; }
else { push @tracks, $_; }
}
# Set the mode
if ($tracks[0] =~ /\.flac$/) { $mode = 'flac'; }
elsif ($tracks[0] =~ /\.mp3$/) { $mode = 'mp3'; }
elsif ($tracks[0] =~ /\.ogg$/) { $mode = 'ogg'; }
else { die "Couldn't detect mode"; }
# Set the album name
$album = `pwd`; # We WANT the newline here
$album =~ s/^.*\///; # Remove everything up to the last /
$album = formatName($album, 'album=', $captagnames);
$album = formatName($album, 'album=', $captagnames, $mode);
# Get the release date, just use the first track
my $year;
# Try to find the release date
$year;
if ($get_year)
{
$mbinfo = Music::Tag->new($tracks[0]);
if ($mode eq 'ogg') { $mbinfo->add_plugin($mbinfo->plugin("OGG")); }
elsif ($mode eq 'flac') { $mbinfo->add_plugin($mbinfo->plugin("FLAC")); }
elsif ($mode eq 'mp3') { $mbinfo->add_plugin($mbinfo->plugin("MP3")); }
$mbinfo->add_plugin("MusicBrainz");
$mbinfo->get_tag();
$year = formatName($mbinfo->year, 'year=', $captagnames, $mode);
}
# Parse the file list, set vorbiscomments
# Parse the file list, set comments
foreach (@ARGV)
{
next if (/^$/);
# check filename format
next unless (/^\w+-\d{1,2}-(\w|-|\d)+\.(ogg|flac)$/);
next unless (/^\w+-\d{1,2}-(\w|-|\d)+\.(ogg|flac|mp3)$/);
$isflac = true if (/\.flac$/);
if ($mode eq 'flac' || $mode eq 'ogg')
{
open COMMENTFILE, '>' . $COMMENTFILENAME;
}
open COMMENTFILE, '>' . $COMMENTFILENAME;
# Remove old flac comments that match
if ($isflac)
# Remove old comments
if ($mode eq 'flac')
{
system("metaflac --remove-tag=TRACKNUMBER $_");
system("metaflac --remove-tag=tracknumber $_");
@ -77,7 +105,11 @@ foreach (@ARGV)
system("metaflac --remove-tag=title --remove-tag=artist --remove-tag=album --remove-tag=year $_");
}
}
# Preserve existing comments, except the ones we'll replace
elsif ($mode eq 'mp3')
{
system("id3v2 -D $_");
}
# For ogg, preserve existing comments, except the ones we'll replace
else
{
foreach (`vorbiscomment -l $_`)
@ -91,75 +123,59 @@ foreach (@ARGV)
if (!$tracknumonly)
{
# Add the artist
my $artist = $_;
$artist = $_;
$artist =~ s/^(.*?)-\d{2}-.*/\1/; # grab everything to the track number
$artist = formatName($artist, 'artist=', $captagnames);
print COMMENTFILE $artist;
$artist = formatName($artist, 'artist=', $captagnames, $mode);
# Add the title
my $title = $_;
$title =~ s/.*-\d{2}-(.*)\.(ogg|flac)$/\1/; # grab everything after the track number
$title = formatName($title, 'title=', $captagnames);
print COMMENTFILE $title;
$title = $_;
$title =~ s/.*-\d{2}-(.*)\.(ogg|flac|mp3)$/\1/; # grab everything after the track number
$title = formatName($title, 'title=', $captagnames, $mode);
# And the album
print COMMENTFILE $album;
# For some modes, print everything to a comment file
if ($mode eq 'flac' || $mode eq 'ogg')
{
print COMMENTFILE $year;
print COMMENTFILE $artist;
print COMMENTFILE $title;
print COMMENTFILE $album;
}
}
# Now get the track number
my $tracknum = $_;
$tracknum =~ s/.*-(\d{2})-.*/\1/;
if (!$extrazero) { $tracknum =~ s/\b0*//; } # removing leading zeros
$tracknum = formatName($tracknum, 'tracknumber=', $captagnames);
print COMMENTFILE $tracknum;
close COMMENTFILE;
$tracknum = formatName($tracknum, 'tracknumber=', $captagnames, $mode);
if ($isflac) { system("metaflac --import-tags-from=$COMMENTFILENAME $_"); }
else { system("vorbiscomment -c $COMMENTFILENAME -w $_"); }
system('rm', $COMMENTFILENAME);
# Finally, try to find the release date, if we haven't yet
if (!$year)
if ($mode eq 'flac' || $mode eq 'ogg')
{
my $mbinfo = Music::Tag->new($_);
if (/\.ogg$/) { $mbinfo->add_plugin($mbinfo->plugin("OGG")); }
elsif (/\.flac$/) { $mbinfo->add_plugin($mbinfo->plugin("FLAC")); }
$mbinfo->add_plugin("MusicBrainz");
$mbinfo->get_tag();
$year = formatName($mbinfo->year, 'year=', $captagnames);
print COMMENTFILE $tracknum;
close COMMENTFILE;
}
}
if ($mode eq 'flac') { system("metaflac --import-tags-from=$COMMENTFILENAME $_"); }
elsif ($mode eq 'mp3')
{
$tags = "";
$tags .= "-T $tracknum" if $tracknum;
if ($tracknumonly || $year =~ /^year=\s*$/) { exit; }
# Loop back through the files again, adding the release date
foreach(@ARGV)
{
my $isflac = true if (/\.flac$/);
open COMMENTFILE, '>' . $COMMENTFILENAME;
# Preserve existing comments
if (!$isflac)
{
foreach (`vorbiscomment -l $_`)
if (!$tracknumonly)
{
print COMMENTFILE $_ unless (/^year/i);
$tags .= " -a '$artist'" if $artist;
$tags .= " -t '$title'" if $title;
$tags .= " -A '$album'" if $album;
$tags .= " -y $year" if $year;
}
system("id3v2 $tags $_");
}
elsif ($mode eq 'ogg')
{
system("vorbiscomment -c $COMMENTFILENAME -w $_");
}
print COMMENTFILE $year;
close COMMENTFILE;
if ($isflac) { system("metaflac --import-tags-from=$COMMENTFILENAME $_"); }
else { system("vorbiscomment -c $COMMENTFILENAME -w $_"); }
system('rm', $COMMENTFILENAME);
if ($mode eq 'flac' || $mode eq 'ogg') { system('rm', $COMMENTFILENAME); }
}
@ -172,13 +188,20 @@ sub formatName
my $name = shift;
my $prepend = shift;
my $upper = shift;
my $mode = shift;
if ($upper) { $prepend = uc($prepend); }
chomp $name; # we add a newline ourselves
$name =~ s/_/ /g; # underscore - space
$name =~ s/^\s*//; # Remove leading space
$name =~ s/\s*$//; # Remove trailing space
$name =~ s/\b(\w)/uc($1)/eg; # Cap the words
$name = $prepend . $name . "\n";
if ($mode eq 'flac' || $mode eq 'ogg')
{
$name = $prepend . $name . "\n";
}
return $name;
}