#!/usr/bin/perl # # Vorbiscomment from Filename # usage: vcfromfilename [options] # 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 # # 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 # # 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 # 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"; $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"; 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; } } # Set the album name $album = `pwd`; # We WANT the newline here $album =~ s/^.*\///; # Remove everything up to the last / $album = formatName($album, 'album=', $captagnames); # 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 $_"); if (!$tracknumonly) { 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/); } else { print COMMENTFILE $_ unless (/^(title)|(artist)|(tracknumber)|(album)/); } } } # 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); 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); print COMMENTFILE $title; # And the album print COMMENTFILE $album; } # Now get the track number my $tracknum = $_; $tracknum =~ s/.*-(\d{2})-.*/\1/; if (!$extrazeros) { $tracknum =~ s/\b0*//; } # removing leading zeros $tracknum = formatName($tracknum, 'tracknumber=', $captagnames); 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 sub formatName { my $name = shift; my $prepend = shift; my $upper = 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"; } # 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