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. Install ALL of the scripts, since some of them depend on others.
You will need the following perl modules:
CDDB (for cdripper) You will need the following perl modules for cdripper to work:
CDDB_get (for cdripper)
Music::Tag::OGG (for tagfromfilename) CDDB
Music::Tag::FLAC (for tagfromfilename) CDDB_get
Music::Tag::MusicBrainz (for tagfromfilename)
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: You will also need the following programs:
cdparanoia cdparanoia
mplayer, mpg321, or mpg123 mplayer, mpg321, or mpg123
oggenc
cdrecord cdrecord
oggenc
vorbiscomment 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. # 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: # 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 # The top-level directory (artist) is optional. Filenames should contain no
# spaces; underscores in the filename are converted to spaces in the resulting # spaces; underscores in the filename are converted to spaces in the resulting
@ -20,6 +20,8 @@
# Changelog: # Changelog:
# 0.2.4: added -T option # 0.2.4: added -T option
# added releasedate support through MusicBrainz # 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.3: added documentation, -0 command-line option
# 0.2.2: added flac support, documentation # 0.2.2: added flac support, documentation
# 0.2.1: fixed version in help message # 0.2.1: fixed version in help message
@ -43,31 +45,57 @@ foreach (@ARGV)
elsif (/^-n$/) { $tracknumonly = true; s/.*//g; } elsif (/^-n$/) { $tracknumonly = true; s/.*//g; }
elsif (/^-T$/) { $captagnames = true; s/.*//g; } elsif (/^-T$/) { $captagnames = true; s/.*//g; }
elsif (/^-0$/) { $extrazero = 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 # Set the album name
$album = `pwd`; # We WANT the newline here $album = `pwd`; # We WANT the newline here
$album =~ s/^.*\///; # Remove everything up to the last / $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 # Try to find the release date
my $year; $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) foreach (@ARGV)
{ {
next if (/^$/); next if (/^$/);
# check filename format # 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 # Remove old comments
if ($isflac) if ($mode eq 'flac')
{ {
system("metaflac --remove-tag=TRACKNUMBER $_"); system("metaflac --remove-tag=TRACKNUMBER $_");
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 $_"); 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 else
{ {
foreach (`vorbiscomment -l $_`) foreach (`vorbiscomment -l $_`)
@ -91,75 +123,59 @@ foreach (@ARGV)
if (!$tracknumonly) if (!$tracknumonly)
{ {
# Add the artist # Add the artist
my $artist = $_; $artist = $_;
$artist =~ s/^(.*?)-\d{2}-.*/\1/; # grab everything to the track number $artist =~ s/^(.*?)-\d{2}-.*/\1/; # grab everything to the track number
$artist = formatName($artist, 'artist=', $captagnames); $artist = formatName($artist, 'artist=', $captagnames, $mode);
print COMMENTFILE $artist;
# Add the title # Add the title
my $title = $_; $title = $_;
$title =~ s/.*-\d{2}-(.*)\.(ogg|flac)$/\1/; # grab everything after the track number $title =~ s/.*-\d{2}-(.*)\.(ogg|flac|mp3)$/\1/; # grab everything after the track number
$title = formatName($title, 'title=', $captagnames); $title = formatName($title, 'title=', $captagnames, $mode);
print COMMENTFILE $title;
# And the album # For some modes, print everything to a comment file
print COMMENTFILE $album; if ($mode eq 'flac' || $mode eq 'ogg')
{
print COMMENTFILE $year;
print COMMENTFILE $artist;
print COMMENTFILE $title;
print COMMENTFILE $album;
}
} }
# Now get the track number # Now get the track number
my $tracknum = $_; my $tracknum = $_;
$tracknum =~ s/.*-(\d{2})-.*/\1/; $tracknum =~ s/.*-(\d{2})-.*/\1/;
if (!$extrazero) { $tracknum =~ s/\b0*//; } # removing leading zeros if (!$extrazero) { $tracknum =~ s/\b0*//; } # removing leading zeros
$tracknum = formatName($tracknum, 'tracknumber=', $captagnames); $tracknum = formatName($tracknum, 'tracknumber=', $captagnames, $mode);
print COMMENTFILE $tracknum;
close COMMENTFILE; if ($mode eq 'flac' || $mode eq 'ogg')
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)
{ {
my $mbinfo = Music::Tag->new($_); print COMMENTFILE $tracknum;
close COMMENTFILE;
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);
} }
}
if ($mode eq 'flac') { system("metaflac --import-tags-from=$COMMENTFILENAME $_"); }
if ($tracknumonly || $year =~ /^year=\s*$/) { exit; } elsif ($mode eq 'mp3')
# 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 $_`) $tags = "";
$tags .= "-T $tracknum" if $tracknum;
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; if ($mode eq 'flac' || $mode eq 'ogg') { system('rm', $COMMENTFILENAME); }
close COMMENTFILE;
if ($isflac) { system("metaflac --import-tags-from=$COMMENTFILENAME $_"); }
else { system("vorbiscomment -c $COMMENTFILENAME -w $_"); }
system('rm', $COMMENTFILENAME);
} }
@ -172,13 +188,20 @@ sub formatName
my $name = shift; my $name = shift;
my $prepend = shift; my $prepend = shift;
my $upper = shift; my $upper = shift;
my $mode = shift;
if ($upper) { $prepend = uc($prepend); } if ($upper) { $prepend = uc($prepend); }
chomp $name; # we add a newline ourselves chomp $name; # we add a newline ourselves
$name =~ s/_/ /g; # underscore - space $name =~ s/_/ /g; # underscore - space
$name =~ s/^\s*//; # Remove leading space $name =~ s/^\s*//; # Remove leading space
$name =~ s/\s*$//; # Remove trailing space $name =~ s/\s*$//; # Remove trailing space
$name =~ s/\b(\w)/uc($1)/eg; # Cap the words $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;
} }