#!/usr/bin/perl use strict; use Audio::FLAC::Header; my @fields = qw/Title Artist Album Year Comment Genre Tracknumber/; my ($f, $ok, @files); # config/defaults my %config = ( rename => 1, ); # process args while ($f = shift @ARGV) { $ok = 0; if (-f $f ) { push @files, $f; $ok = 1} for (@fields) { if ($f =~ /^\-\-no\-$_/i) { $config{skip}{$_} = 1; $ok = 1; } elsif ($f =~ /^\-\-$_/i) { $config{default}{$_} = shift @ARGV; $ok = 1; } } if ($f eq '--no-rename') { $config{rename} = 0; $ok = 1; } die "Unknown tag/no file $f" unless $ok; } for (@files) { edit_tag($_); } sub edit_tag { my $file = shift; print "$file:\n"; my ($f, $ftag, $flac, $src, $dst, $newtag, $changed, $skip); $flac = Audio::FLAC::Header->new($file); die "Can't get FLAC header - $!" unless $flac; $ftag = $flac->tags(); die "Can't get flac tags - $!" unless $ftag; $changed = 0; for $f (@fields) { $skip = exists $config{skip}{$f} ? 1 : 0; $f = ((grep /^$f$/i,keys %$ftag)[0] || $f); print "\t[$f] "; if ($config{default}{$f} || '') { print "$config{default}{$f}\n"; $$ftag{$f} = $config{default}{$f}; $changed = 1; next; } if (! length($$ftag{$f} || '')) { print '("") '; if ($skip ) { $newtag = ''; print "\n"; } else { print '---> '; $newtag = <>; } chop($newtag); if ($newtag =~/\w+/) { $$ftag{$f} = $newtag; $changed = 1; } } else { print "$$ftag{$f}\n"; } #$$f{ucfirst(lc($_))} = $$m{$_} } if ($changed) { print "*** NEW TAG WRITTEN ***\n"; $flac->write() || warn $!; } $dst = $$ftag{Title}; $dst =~s/[ \:]/_/og; $dst =~s/[^\w\-\_]//og; $dst = ucfirst(lc($dst)) .'.flac'; if ($config{rename} && $file ne $dst && ! -e $dst) { print "$file -> $dst"; rename $file, $dst; } else { print "$dst exists; not renaming\n"; } print "\n"; }