diff --git a/tagfromfilename b/tagfromfilename index b1c1c06..34a4d33 100755 --- a/tagfromfilename +++ b/tagfromfilename @@ -19,6 +19,7 @@ # # Changelog: # 0.2.4: added -T option +# added releasedate support through MusicBrainz # 0.2.3: added documentation, -0 command-line option # 0.2.2: added flac support, documentation # 0.2.1: fixed version in help message @@ -30,6 +31,9 @@ $VERSION = "0.2.4"; $HELP_MSG = "Vorbiscomment from Filename $VERSION\n\tusage: vcfromfilename [options] \n\nValid options:\n\t-n\tOnly set the track numbers\n\t-0\tPreserve leading 0s in track numbers\n\t-T: Write tag names (not tag content) in all caps\n"; + +use Music::Tag; + chomp($COMMENTFILENAME = '/tmp/vcfromfilename-' . `whoami`); # Look for options @@ -46,6 +50,11 @@ $album = `pwd`; # We WANT the newline here $album =~ s/^.*\///; # Remove everything up to the last / $album = formatName($album, 'album=', $captagnames); + +# Get the release date, just use the first track +my $releasedate = 'releasedate='; + + # Parse the file list, set vorbiscomments foreach (@ARGV) { @@ -64,8 +73,8 @@ foreach (@ARGV) system("metaflac --remove-tag=tracknumber $_"); if (!$tracknumonly) { - system("metaflac --remove-tag=TITLE --remove-tag=ARTIST --remove-tag=ALBUM $_"); - system("metaflac --remove-tag=title --remove-tag=artist --remove-tag=album $_"); + system("metaflac --remove-tag=TITLE --remove-tag=ARTIST --remove-tag=ALBUM --remove-tag=RELEASEDATE $_"); + system("metaflac --remove-tag=title --remove-tag=artist --remove-tag=album --remove-tag=releasedate $_"); } } # Preserve existing comments, except the ones we'll replace @@ -74,7 +83,7 @@ foreach (@ARGV) foreach (`vorbiscomment -l $_`) { if ($tracknumonly) { print COMMENTFILE $_ unless (/^tracknumber/i); } - else { print COMMENTFILE $_ unless (/^(title)|(artist)|(tracknumber)|(album)/i); } + else { print COMMENTFILE $_ unless (/^(title)|(artist)|(tracknumber)|(album)|(releasedate)/i); } } } @@ -109,8 +118,52 @@ foreach (@ARGV) 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 ($releasedate =~ /^releasedate=\s*$/) + { + 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(); + $releasedate = formatName($mbinfo->releasedate, 'releasedate=', $captagnames); + } } + +if ($tracknumonly || $releasedate =~ /^releasedate=\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 $_`) + { + print COMMENTFILE $_ unless (/^releasedate/i); + } + } + + print COMMENTFILE $releasedate; + close COMMENTFILE; + + if ($isflac) { system("metaflac --import-tags-from=$COMMENTFILENAME $_"); } + else { system("vorbiscomment -c $COMMENTFILENAME -w $_"); } + system('rm', $COMMENTFILENAME); +} + + + # Formats the first argument, adds the second argument to it # If the third argument is true, the second argument is converted # to upper case @@ -129,7 +182,7 @@ sub formatName } -# Copyright (c) 2005 - 2008 John Wiggins +# Copyright (c) 2005 - 2010 John Wiggins # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by