#!/usr/bin/perl -w use blib 'J:/test-programs-other/.cpan/tagged-0.40'; use MP3::Tag; use Getopt::Std 'getopts'; use strict; my %opt; getopts('c:a:t:l:n:g:y:uDp:C:', \%opt); my %trans = ( 't' => 'title', 'a' => 'artist', 'l' => 'album', 'y' => 'year', 'g' => 'genre', 'c' => 'comment', 'n' => 'track' ); # Configure stuff... $opt{C} =~ s/^(\w+)=/$1,/, MP3::Tag->config(split /,/, $opt{C}) if defined $opt{C}; my %r = ( 'n' => "\n", 't' => "\t", '\\' => "\\" ); $opt{p} =~ s/\\([nt\\])/$r{$1}/g if defined $opt{p}; # E.g., to make Inf overwrite existing title, do # mp3info2.pl -C title,Inf,ID3v2,ID3v1,filename -u *.mp3 my $f; for $f (@ARGV) { print "File: $f\n" unless exists $opt{p}; if (my $mp3=MP3::Tag->new($f)) { print $mp3->interpolate(exists $opt{p} ? $opt{p} : <autoinfo('from'); my $modify; for my $k (keys %trans) { if (exists $opt{$k}) { if (exists $data->{$trans{$k}}) { if ( $data->{$trans{$k}}->[0] ne $opt{$k} or $data->{$trans{$k}}->[1] !~ /^id3/i ) { warn "Need to change $trans{$k}\n"; $data->{$trans{$k}} = [$opt{$k}, 'cmd']; $modify = 1; } } else { warn "Need to add $trans{$k}\n"; $data->{$trans{$k}} = [$opt{$k}, 'cmd']; $modify = 1; } } } if ($opt{u} and not $modify) { # Update for my $k (keys %$data) { next if $data->{$k}->[1] =~ /^ID3/; $modify = 1; warn "Need to propagate $k from $data->{$k}->[1]\n"; } } $opt{u} and warn "No update needed\n" unless $modify; next unless $modify and not $opt{D}; # Dry run $mp3->new_tag("ID3v1") unless exists $mp3->{ID3v1}; my $elt; for $elt (qw/title artist album year comment track genre/) { $mp3->{ID3v1}->$elt( $data->{$elt}->[0] ) if defined $data->{$elt} and $data->{$elt}->[1] ne 'ID3v1'; } # Skip what is already there... $mp3->{ID3v1}->write_tag; next if $mp3->{ID3v1}->fits_tag($data) and not exists $mp3->{ID3v2}; $mp3->new_tag("ID3v2") unless exists $mp3->{ID3v2}; for $elt (qw/title artist album year comment track genre/) { $mp3->{ID3v2}->$elt( $data->{$elt}->[0] ) if defined $data->{$elt} and $data->{$elt}->[1] ne 'ID3v2'; } # Skip what is already there... # $mp3->{ID3v2}->comment($data->{comment}->[0]); $mp3->{ID3v2}->write_tag; } else { print "Not found...\n"; } } =head1 NAME mp3info2 - get/set MP3 tags =head1 SYNOPSIS mp3info2 -y 1981 *.mp3 mp3info2 -C artist=CDDB_File -u *.mp3 mp3info2.pl -C title=Inf,ID3v2,ID3v1,filename -u *.mp3 mp3info2 -p "lame -h --vbr-new --tt '%t' --tn %n --ta '%a' --tc '%c' --tl '%l' --ty '%y' '%f'\n" *.wav >xxx.sh =head1 DESCRIPTION The program prints a message summarizing tag info (obtained via L module) for specified files. It may also update the information in MP3 tags. This happens in two different cases. =over =item * If the information supplied in command-line options differs from the content of the corresponding ID3 tags (or there is no corresponding ID3 tags). =item * if C obtains the info from other means than MP3 tags, and C<-u> forces the update of the ID3 tags. =back (Both ways are disabled by C<-D> option.) ID3v2 tag is written if needed. The option C<-C> sets C configuration data (separated by commas) as L. The option C<-u> writes (Cpdates) the fetched information to the MP3 ID3 tags. The option C<-p> prints a message using the next argument as format (with C<\\>, C<\t>, C<\n> replaces by backslash, tab and newline); see L for details of the format of sprintf()-like escapes. With option C<-D> (dry run) no update is performed. Use options t a l y g c n to overwrite the information (title artist album year genre comment track-number) obtained via C heuristics (use with C<-u> switch). =cut