2009-03-20 05:54:35 +00:00
#!/usr/bin/perl
#
# Vorbiscomment from Filename
# usage: vcfromfilename [options] <files>
# options:
# -n: Only set the track number
2009-03-26 13:45:25 +00:00
# -0: Leading 0s in track number are preserved
2009-03-27 21:44:17 +00:00
# -T: Tag names (not tag content) written to the metadata in all caps
2009-03-20 05:54:35 +00:00
#
# Adds Vorbiscomment data to ogg vorbis and flac files based on the filename.
2011-06-16 21:09:31 +00:00
# Files should be in Anna's arbitrary file name standard, which is:
2010-09-21 13:40:41 +00:00
# artist/album/artist-tracknum-title.(ogg|flac|mp3)
2009-03-26 13:45:25 +00:00
#
# The top-level directory (artist) is optional. Filenames should contain no
# spaces; underscores in the filename are converted to spaces in the resulting
# metadata
2009-03-20 05:54:35 +00:00
#
# Requires vorbiscomment and metaflac
#
# Changelog:
2009-03-27 21:44:17 +00:00
# 0.2.4: added -T option
2010-03-25 04:04:27 +00:00
# added releasedate support through MusicBrainz
2010-09-21 13:40:41 +00:00
# added mp3 support and refactored code heavily
# added -y option to explicitly look for the year
2009-03-26 13:45:25 +00:00
# 0.2.3: added documentation, -0 command-line option
# 0.2.2: added flac support, documentation
2009-03-20 05:54:35 +00:00
# 0.2.1: fixed version in help message
# fixed handling of dashes in artist name and title
# 0.2: aligned version with ds-audiotools
# 0.1.1: added version variable
# 0.1: initial implementation
2009-03-27 21:44:17 +00:00
$VERSION = "0.2.4";
2009-03-27 22:00:23 +00:00
$HELP_MSG = "Vorbiscomment from Filename $VERSION\n\tusage: vcfromfilename [options] <files>\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";
2009-03-20 05:54:35 +00:00
2010-03-25 04:04:27 +00:00
use Music::Tag;
2009-03-20 05:54:35 +00:00
chomp($COMMENTFILENAME = '/tmp/vcfromfilename-' . `whoami`);
# Look for options
foreach (@ARGV)
{
if (/^(-h)|(--help)$/) { die $HELP_MSG; }
2009-03-27 21:44:17 +00:00
elsif (/^-n$/) { $tracknumonly = true; s/.*//g; }
elsif (/^-T$/) { $captagnames = true; s/.*//g; }
elsif (/^-0$/) { $extrazero = true; s/.*//g; }
2010-09-21 13:40:41 +00:00
elsif (/^-y$/) { $get_year = true; s/.*//g; }
else { push @tracks, $_; }
2009-03-20 05:54:35 +00:00
}
2010-09-21 13:40:41 +00:00
# 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"; }
2009-03-20 05:54:35 +00:00
# Set the album name
$album = `pwd`; # We WANT the newline here
$album =~ s/^.*\///; # Remove everything up to the last /
2010-09-21 13:40:41 +00:00
$album = formatName($album, 'album=', $captagnames, $mode);
# 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")); }
2009-03-20 05:54:35 +00:00
2010-09-21 13:40:41 +00:00
$mbinfo->add_plugin("MusicBrainz");
$mbinfo->get_tag();
2010-03-25 04:04:27 +00:00
2010-09-21 13:40:41 +00:00
$year = formatName($mbinfo->year, 'year=', $captagnames, $mode);
}
2010-03-25 04:04:27 +00:00
2010-09-21 13:40:41 +00:00
# Parse the file list, set comments
2009-03-20 05:54:35 +00:00
foreach (@ARGV)
{
next if (/^$/);
# check filename format
2010-09-21 13:40:41 +00:00
next unless (/^\w+-\d{1,2}-(\w|-|\d)+\.(ogg|flac|mp3)$/);
2009-03-20 05:54:35 +00:00
2010-09-21 13:40:41 +00:00
if ($mode eq 'flac' || $mode eq 'ogg')
{
open COMMENTFILE, '>' . $COMMENTFILENAME;
}
2009-03-20 05:54:35 +00:00
2010-09-21 13:40:41 +00:00
# Remove old comments
if ($mode eq 'flac')
2009-03-20 05:54:35 +00:00
{
2009-03-27 21:57:41 +00:00
system("metaflac --remove-tag=TRACKNUMBER $_");
2009-03-20 05:54:35 +00:00
system("metaflac --remove-tag=tracknumber $_");
if (!$tracknumonly)
{
2010-03-25 18:58:39 +00:00
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 $_");
2009-03-20 05:54:35 +00:00
}
}
2010-09-21 13:40:41 +00:00
elsif ($mode eq 'mp3')
{
system("id3v2 -D $_");
}
# For ogg, preserve existing comments, except the ones we'll replace
2009-03-20 05:54:35 +00:00
else
{
foreach (`vorbiscomment -l $_`)
{
2009-03-27 21:57:41 +00:00
if ($tracknumonly) { print COMMENTFILE $_ unless (/^tracknumber/i); }
2010-03-25 18:58:39 +00:00
else { print COMMENTFILE $_ unless (/^(title)|(artist)|(tracknumber)|(album)|(year)/i); }
2009-03-20 05:54:35 +00:00
}
}
# Create new comments from the filename
if (!$tracknumonly)
{
# Add the artist
2010-09-21 13:40:41 +00:00
$artist = $_;
2009-03-20 05:54:35 +00:00
$artist =~ s/^(.*?)-\d{2}-.*/\1/; # grab everything to the track number
2010-09-21 13:40:41 +00:00
$artist = formatName($artist, 'artist=', $captagnames, $mode);
2009-03-20 05:54:35 +00:00
# Add the title
2010-09-21 13:40:41 +00:00
$title = $_;
$title =~ s/.*-\d{2}-(.*)\.(ogg|flac|mp3)$/\1/; # grab everything after the track number
$title = formatName($title, 'title=', $captagnames, $mode);
2009-03-20 05:54:35 +00:00
2010-09-21 13:40:41 +00:00
# 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;
}
2009-03-20 05:54:35 +00:00
}
# Now get the track number
my $tracknum = $_;
$tracknum =~ s/.*-(\d{2})-.*/\1/;
2009-03-27 22:00:23 +00:00
if (!$extrazero) { $tracknum =~ s/\b0*//; } # removing leading zeros
2010-09-21 13:40:41 +00:00
$tracknum = formatName($tracknum, 'tracknumber=', $captagnames, $mode);
2010-03-25 04:04:27 +00:00
2010-09-21 13:40:41 +00:00
if ($mode eq 'flac' || $mode eq 'ogg')
2010-03-25 04:04:27 +00:00
{
2010-09-21 13:40:41 +00:00
print COMMENTFILE $tracknum;
close COMMENTFILE;
2010-03-25 04:04:27 +00:00
}
2010-09-21 13:40:41 +00:00
if ($mode eq 'flac') { system("metaflac --import-tags-from=$COMMENTFILENAME $_"); }
elsif ($mode eq 'mp3')
{
$tags = "";
$tags .= "-T $tracknum" if $tracknum;
2010-03-25 04:04:27 +00:00
2010-09-21 13:40:41 +00:00
if (!$tracknumonly)
2010-03-25 04:04:27 +00:00
{
2010-09-21 13:40:41 +00:00
$tags .= " -a '$artist'" if $artist;
$tags .= " -t '$title'" if $title;
$tags .= " -A '$album'" if $album;
$tags .= " -y $year" if $year;
2010-03-25 04:04:27 +00:00
}
2010-09-21 13:40:41 +00:00
system("id3v2 $tags $_");
}
elsif ($mode eq 'ogg')
{
system("vorbiscomment -c $COMMENTFILENAME -w $_");
}
2010-03-25 04:04:27 +00:00
2010-09-21 13:40:41 +00:00
if ($mode eq 'flac' || $mode eq 'ogg') { system('rm', $COMMENTFILENAME); }
2010-03-25 04:04:27 +00:00
}
2009-03-20 05:54:35 +00:00
# Formats the first argument, adds the second argument to it
2009-03-27 21:44:17 +00:00
# If the third argument is true, the second argument is converted
# to upper case
2009-03-20 05:54:35 +00:00
sub formatName
{
my $name = shift;
my $prepend = shift;
2009-03-27 21:44:17 +00:00
my $upper = shift;
2010-09-21 13:40:41 +00:00
my $mode = shift;
2009-03-27 21:44:17 +00:00
if ($upper) { $prepend = uc($prepend); }
2009-03-20 05:54:35 +00:00
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
2010-09-21 13:40:41 +00:00
if ($mode eq 'flac' || $mode eq 'ogg')
{
$name = $prepend . $name . "\n";
}
return $name;
2009-03-20 05:54:35 +00:00
}
# 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
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA