This repository has been archived on 2019-12-04. You can view files and clone it, but cannot push or open issues or pull requests.
simple-audiotools/tagfromfilename

147 lines
4.8 KiB
Plaintext
Raw Normal View History

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
# -0: Leading 0s in track number are preserved
# -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.
# Files should be in Patrick's arbitrary file name standard, which is:
# artist/album/artist-tracknum-title.(ogg|flac)
#
# 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:
# 0.2.4: added -T option
# 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
$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
chomp($COMMENTFILENAME = '/tmp/vcfromfilename-' . `whoami`);
# Look for options
foreach (@ARGV)
{
if (/^(-h)|(--help)$/) { die $HELP_MSG; }
elsif (/^-n$/) { $tracknumonly = true; s/.*//g; }
elsif (/^-T$/) { $captagnames = true; s/.*//g; }
elsif (/^-0$/) { $extrazero = true; s/.*//g; }
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 /
$album = formatName($album, 'album=', $captagnames);
2009-03-20 05:54:35 +00:00
# Parse the file list, set vorbiscomments
foreach (@ARGV)
{
next if (/^$/);
# check filename format
next unless (/^\w+-\d{1,2}-(\w|-|\d)+\.(ogg|flac)$/);
$isflac = true if (/\.flac$/);
open COMMENTFILE, '>' . $COMMENTFILENAME;
# Remove old flac comments that match
if ($isflac)
{
system("metaflac --remove-tag=TRACKNUMBER $_");
2009-03-20 05:54:35 +00:00
system("metaflac --remove-tag=tracknumber $_");
if (!$tracknumonly)
{
system("metaflac --remove-tag=TITLE --remove-tag=ARTIST --remove-tag=ALBUM $_");
2009-03-20 05:54:35 +00:00
system("metaflac --remove-tag=title --remove-tag=artist --remove-tag=album $_");
}
}
# Preserve existing comments, except the ones we'll replace
else
{
foreach (`vorbiscomment -l $_`)
{
if ($tracknumonly) { print COMMENTFILE $_ unless (/^tracknumber/i); }
else { print COMMENTFILE $_ unless (/^(title)|(artist)|(tracknumber)|(album)/i); }
2009-03-20 05:54:35 +00:00
}
}
# Create new comments from the filename
if (!$tracknumonly)
{
# Add the artist
my $artist = $_;
$artist =~ s/^(.*?)-\d{2}-.*/\1/; # grab everything to the track number
$artist = formatName($artist, 'artist=', $captagnames);
2009-03-20 05:54:35 +00:00
print COMMENTFILE $artist;
# Add the title
my $title = $_;
$title =~ s/.*-\d{2}-(.*)\.(ogg|flac)$/\1/; # grab everything after the track number
$title = formatName($title, 'title=', $captagnames);
2009-03-20 05:54:35 +00:00
print COMMENTFILE $title;
# And the album
print COMMENTFILE $album;
}
# 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
$tracknum = formatName($tracknum, 'tracknumber=', $captagnames);
2009-03-20 05:54:35 +00:00
print COMMENTFILE $tracknum;
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
2009-03-20 05:54:35 +00:00
sub formatName
{
my $name = shift;
my $prepend = shift;
my $upper = shift;
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
$name = $prepend . $name . "\n";
}
# Copyright (c) 2005 - 2008 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
# 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