package Music::Tag; our $VERSION = 0.33; # Copyright (c) 2007,2008 Edward Allen III. Some rights reserved. # ## This program is free software; you can redistribute it and/or ## modify it under the terms of the Artistic License, distributed ## with Perl. # =pod =for changes stop =head1 NAME Music::Tag - Interface for collecting information about music files. =for readme stop =head1 SYNOPSIS use Music::Tag; my $info = Music::Tag->new($filename); # Read basic info $info->get_tag(); print "Performer is ", $info->artist(); print "Album is ", $info->album(); print "Release Date is ", $info->releasedate(); # Change info $info->artist('Throwing Muses'); $info->album('University'); # Augment info from an online database! $info->add_plugin("MusicBrainz"); $info->add_plugin("Amazon"); $info->get_tag; print "Record Label is ", $info->label(); # Save back to file $info->set_tag(); $info->close(); =for readme continue =head1 DESCRIPTION Extendable module for working with Music Tags. Music::Tag Is powered by various plugins that collect data about a song based on whatever information has already been discovered. The motivation behind this was to provide a convenient method for fixing broken tags in music files. This developed into a universal interface to various music file tagging schemes and a convenient way to augment this from online databases. Several plugin modules to find information about a music file and write it back into the tag are available. These modules will use available information (B and B) and set various data values back to the tag. =begin readme =head1 INSTALLATION To install this module type the following: perl Makefile.PL make make test make install =head2 IMPORTANT NOTE If you have installed older versions (older than .25) PLEASE delete the following scripts from your bin folder: autotag, safetag, quicktag, musicsort, musicinfo. If you used any of these scripts, create a symbolic link to musictag for each. =head2 QUICK INSTALL OF ALL PACKAGES A bundle is available to quickly install Music::Tag with all plugins. To install it use: perl -MCPAN -eshell At the cpan shell prompt type: install Bundle::Music::Tag =head1 DEPENDENCIES This module requires these other modules and libraries: Encode File::Spec Locale::Country Digest::SHA1 Config::Options I strongly recommend the following to improve web searches: Lingua::EN::Inflect Lingua::Stem Text::LevenshteinXS Text::Unaccent The following just makes things pretty: Term::ANSIColor =end readme =head1 EXECUTABLE SCRIPT An executable script, L is allows quick tagging of MP3 files. To learn more, use: musictag --help musictag --longhelp =cut use strict qw(vars); use Carp; use Locale::Country; use File::Spec; use Encode; use Config::Options; use Digest::SHA1; use utf8; use vars qw($AUTOLOAD %DataMethods); =for readme stop =head1 METHODS =over 4 =item B Takes a filename, an optional hashref of options, and an optional first plugin and returns a new Music::Tag object. For example: my $info = Music::Tag->new($filename, { quiet => 1 }, "MP3" ) ; If no plugin is listed, then it will automatically add the appropriate file plugin based on the extension. It does this by using the L plugin. If no plugin is appropriate, it will return undef. Options are global (apply to all plugins) and default (can be overridden by a plugin). Plugin specific options can be applied here, if you wish. They will be ignored by plugins that don't know what to do with them. See the POD for each of the plugins for more details on options a particular plugin accepts. B =over 4 =item B Default is false. Setting this to true causes plugin to generate a lot of noise. =item B Default is false. Setting this to true prevents the plugin from giving status messages. =item B Option is a hash reference mapping file extensions to plugins. Technically, this option is for the L plugin. Default is: { mp3 => "MP3", m4a => "M4A", m4p => "M4A", mp4 => "M4A", m4b => "M4A", '3gp' => "M4A", ogg => "OGG", flac => "FLAC" } =item B Array reference of files to load options from. Default is: [ "/etc/musictag.conf", $ENV{HOME} . "/.musictag.conf" ] Note that this is only used if the "load_options" method is called. Option file is a pure perl config file using L. =item B Default false. Set to true to enable color status messages. =item B Default true. Set to true to use Text::LevenshteinXS to allow approximate matching with Amazon and MusicBrainz Plugins. Will reset to false if module is missing. =item B Default true. Same as LevenshteinXS, but with Text::Levenshtein. Will not use if Text::Levenshtein can be loaded. Will reset to false if module is missing. =item B Default true. When true, allows accent-neutral matching with Text::Unaccent. Will reset to false if module is missing. =item B Default false. When true, uses Linque::EN::Inflect to perform approximate matches. Will reset to false if module is missing. =item B Default false. When true, uses Linqua::Stem to perform approximate matches. Will reset to false if module is missing. =item B When true, uses Time::Local to perform date calculations. Defaults true. Will reset to false if module is missing. =back =cut BEGIN { $Music::Tag::DefaultOptions = Config::Options->new( { verbose => 0, quiet => 0, ANSIColor => 0, LevenshteinXS => 1, Levenshtein => 1, TimeLocal => 1, Unaccent => 1, Inflect => 0, Stem => 0, StemLocale => "en-us", optionfile => [ "/etc/musictag.conf", $ENV{HOME} . "/.musictag.conf" ], } ); my @datamethods = qw(albkey album album_type albumartist albumartist_sortname albumid appleid artist artist_end artist_start artist_type artistid artkey asin bitrate booklet bytes codec comment compilation composer copyright country countrycode disc discnum disctitle duration encoded_by encoder filename frames framesize frequency gaplessdata genre ipod ipod_dbid ipod_location ipod_trackid label lastplayed lyrics mb_albumid mb_artistid mb_trackid mip_puid mtime originalartist path picture playcount postgap pregap rating recorddate recordtime releasedate releasetime samplecount secs songid songkey sortname stereo tempo title totaldiscs totaltracks track tracknum url user vbr year upc ean jan); %Music::Tag::DataMethods = map { $_ => 1 } @datamethods; %Music::Tag::AUTOPLUGINS = (); @Music::Tag::PLUGINS = (); my $myname = __PACKAGE__; my $me = $myname; $me =~ s/\:\:/\//g; foreach my $d (@INC) { chomp $d; if ( -d "$d/$me/" ) { local (*F_DIR); opendir( *F_DIR, "$d/$me/" ); while ( my $b = readdir(*F_DIR) ) { next unless $b =~ /^(.*)\.pm$/; my $mod = $1; push @Music::Tag::PLUGINS, $mod; } } } } =item B Class method. Returns list of available plugins. For example: foreach (Music::Tag->availble_plugins) { if ($_ eq "Amazon") { print "Amazon is available!\n"; $info->add_plugin("Amazon", { locale => "uk" }); } } =cut sub available_plugins { my $self = shift; my $check = shift; if ($check) { foreach (@Music::Tag::PLUGINS) { if ( $check eq $_ ) { return 1; } } return 0; } return @Music::Tag::PLUGINS; } =item B Class method. Returns default options as a Config::Options method. =cut sub default_options { my $self = shift; return $Music::Tag::DefaultOptions; } =item B Load options stated in optionfile from file. Default locations are /etc/musictag.conf and ~/.musictag.conf. Can be called as class method or object method. If called as a class method the default values for all future Music::Tag objects are changed. =cut sub LoadOptions { my $self = shift; my $optfile = shift; if ( ref $self ) { return $self->options->fromfile_perl($optfile); } elsif ($self) { return $Music::Tag::DefaultOptions->fromfile_perl($optfile); } } sub new { my $class = shift; my $filename = shift; my $options = shift || {}; my $plugin = shift || "Auto"; my $data = shift || {}; my $self = {}; $self->{data} = $data; if ( ref $class ) { my $clone = {%$class}; bless $clone, ref $class; return $clone; } else { bless $self, $class; $self->{_plugins} = []; $self->options($options); $self->filename($filename); } if ( ( $self->options->{ANSIColor} ) && ( $self->_has_module("Term::ANSIColor") ) ) { $self->options->{ANSIColor} = 1; } else { $self->options->{ANSIColor} = 0; } if ( ( $self->options->{LevenshteinXS} ) && ( $self->_has_module("Text::LevenshteinXS") ) ) { $self->options->{LevenshteinXS} = 1; } elsif ( ( $self->options->{Levenshtein} ) && ( $self->_has_module("Levenshtein") ) ) { $self->options->{Levenshtein} = 1; } else { $self->options->{LevenshteinXS} = 0; $self->options->{Levenshtein} = 0; } if ( ( $self->options->{Unaccent} ) && ( not $self->_has_module("Text::Unaccent") ) ) { $self->options->{Unaccent} = 0; } if ( ( $self->options->{Inflect} ) && ( not $self->_has_module("Lingua::EN::Inflect") ) ) { $self->options->{Inflect} = 0; } if ( ( $self->options->{Stem} ) && ( not $self->_has_module("Lingua::Stem") ) ) { $self->options->{Stem} = 0; } if ( ( $self->options->{TimeLocal} ) && ( not $self->_has_module("Time::Local") ) ) { $self->options->{TimeLocal} = 0; } if ($plugin) { $self->add_plugin( $plugin, $options ); return $self; } #else { # return $self->auto_plugin($options); #} } sub _has_module { my $self = shift; my $module = shift; my $modfile = $module . ".pm"; $modfile =~ s/\:\:/\//g; no warnings; eval { require $modfile }; if ($@) { $self->status( 1, "Not loading $module: " . $@ ); return 0; } else { return 1; } } =pod =item B Takes a plugin name and optional set of options and it to a the Music::Tag object. Returns reference to a new plugin object. For example: my $plugin = $info->add_plugin("MusicBrainz", { preferred_country => "UK" }); $options is a hashref that can be used to override the global options for a plugin. First option can be an string such as "MP3" in which case Music::Tag::MP3->new($self, $options) is called, an object name such as "Music::Tag::Custom::MyPlugin" in which case Music::Tag::MP3->new($self, $options) is called or an object, which is added to the list. Current plugins include L, L, L, L, L, L, L, L and l, Additional plugins can be created and may be available on CPAN. See for information. Options can also be included in the string, as in Amazon;locale=us;trust_title=1. =cut sub add_plugin { my $self = shift; my $object = shift; my $opts = shift || {}; my $options = $self->options->clone; $options->merge($opts); my $type = shift || 0; my $ref; if ( ref $object ) { $ref = $object; $ref->info($self); $ref->options($options); } else { my ( $plugin, $popts ) = split( ":", $object ); if ( $self->available_plugins($plugin) ) { if ($popts) { my @opts = split( /[;]/, $popts ); foreach (@opts) { my ( $k, $v ) = split( "=", $_ ); $options->options( $k, $v ); } } eval { unless ( $plugin =~ /::/ ) { $plugin = "Music::Tag::" . $plugin; } if ( $self->_has_module($plugin) ) { $ref = $plugin->new( $self, $options ); } }; croak "Error loading plugin ${plugin}: $@" if $@; } else { croak "Error loading plugin ${plugin}: Not Found"; } } if ($ref) { push @{ $self->{_plugins} }, $ref; } return $ref; } =pod =item B my $plugin = $item->plugin("MP3")->strip_tag(); The plugin method takes a regular expression as a string value and returns the first plugin whose package name matches the regular expression. Used to access package methods directly. Please see section for more details on standard plugin methods. =cut sub plugin { my $self = shift; my $plugin = shift; if ( defined $plugin ) { foreach ( @{ $self->{_plugins} } ) { if ( ref($_) =~ /$plugin$/ ) { return $_; } } } else { return $self->{_plugins}; } } =pod =item B get_tag applies all active plugins to the current Music::Tag object in the order that the plugin was added. Specifically, it runs through the list of plugins and performs the get_tag() method on each. For example: $info->get_tag(); =cut sub get_tag { my $self = shift; foreach ( @{ $self->{_plugins} } ) { if ( ref $_ ) { $_->get_tag(); } else { $self->error("Invalid Plugin in list: $_"); } } return $self; } =pod =item B set_tag writes info back to disk for all Music::Tag plugins, or submits info if appropriate. Specifically, it runs through the list of plugins and performs the set_tag() method on each. For example: $info->set_tag(); =cut sub set_tag { my $self = shift; foreach ( @{ $self->{_plugins} } ) { if ( ref $_ ) { $_->set_tag(); } else { $self->error("Invalid Plugin in list!"); } } return $self; } =pod =item B strip_tag removes info from on disc tag for all plugins. Specifically, it performs the strip_tag method on all plugins in the order added. For example: $info->strip_tag(); =cut sub strip_tag { my $self = shift; foreach ( @{ $self->{_plugins} } ) { if ( ref $_ ) { $_->strip_tag(); } else { $self->error("Invalid Plugin in list!"); } } return $self; } =pod =item B closes active filehandles on all plugins. Should be called before object destroyed or frozen. For example: $info->close(); =cut sub close { my $self = shift; foreach ( @{ $self->{_plugins} } ) { if ( ref $_ ) { $_->close(@_); $_->{info} = undef; $_ = undef; } else { $self->error("Invalid Plugin in list!"); } } $self = undef; } =pod =item B Returns true if changed. Optional value $new sets changed set to True of $new is true. A "change" is any data-value additions or changes done by MusicBrainz, Amazon, File, or Lyrics plugins. For example: # Check if there is a change: $ischanged = $info->changed(); # Force there to be a change $info->changed(1); =cut sub changed { my $self = shift; my $new = shift; if ( defined $new ) { $self->{changed}++; } return $self->{changed}; } =item B Returns a reference to the hash which stores all data about a track and optionally sets it. This is useful if you want to freeze and recreate a track, or use a shared data object in a threaded environment. For example; use Data::Dumper; my $bighash = $info->data(); print Dumper($bighash); =cut sub data { my $self = shift; my $new = shift; if ( defined $new ) { $self->{data} = $new; } return $self->{data}; } =pod =item B This method is used to access or change the options. When called with no options, returns a reference to the options hash. When called with one string option returns the value for that key. When called with one hash value, merges hash with current options. When called with 2 options, the first is a key and the second is a value and the key gets set to the value. This method is for global options. For example: # Get value for "verbose" option my $verbose = $info->options("verbose"); # or... my $verbose = $info->options->{verbose}; # Set value for "verbose" option $info->options("verbose", 0); # or... $info->options->{verbose} = 0; =cut sub options { my $self = shift; unless ( exists $self->{_options} ) { $self->{_options} = Config::Options->new( $self->default_options ); } return $self->{_options}->options(@_); } =item B Sets the mtime and bytes attributes for you from filename. =cut sub setfileinfo { my $self = shift; if ( $self->filename ) { my @stat = stat $self->filename; $self->mtime( $stat[9] ); $self->bytes( $stat[7] ); } } =item B Returns a sha1 digest of the first 16K of the music file. =cut sub sha1 { my $self = shift; return unless ( ( $self->filename ) && ( -e $self->filename ) ); my $maxsize = 4 * 4096; open( IN, $self->filename ) or die "Bad file: $self->filename\n"; my @stat = stat $self->filename; my $sha1 = Digest::SHA1->new(); $sha1->add( pack( "V", $stat[7] ) ); my $d; if ( read( IN, $d, $maxsize ) ) { $sha1->add($d); } CORE::close(IN); return $sha1->hexdigest; } =pod =item B Returns an array reference of all data methods supported. Optionally takes a method which is added. Data methods should be all lower case and not conflict with existing methods. Data method additions are global, and not tied to an object. Array reference should be considered read only. For example: # Print supported data methods: my $all_methods = Music::Tag->datamethods(); foreach (@{$all_methods}) { print '$info->'. $_ . " is supported\n"; } # Add is_hairband data method: Music::Tag->datamethods("is_hairband"); =cut sub datamethods { my $self = shift; my $new = shift; if ($new) { $DataMethods{$new} = 1; } return [ keys %DataMethods ]; } =pod =item B Returns an array reference of all data methods that will not return undef. For example: my $info = Music::Tag->new($filename); $info->get_tag(); foreach (@{$info->used_datamethods}) { print $_ , ": ", $info->$_, "\n"; } =cut sub used_datamethods { my $self = shift; my @ret = (); foreach my $m ( @{ $self->datamethods } ) { if ( $m eq "picture" ) { if ( $self->picture_exists ) { push @ret, $m; } } else { if ( defined $self->$m ) { push @ret, $m; } } } return \@ret; } =back =head2 Data Access Methods These methods are used to access the Music::Tag data values. Not all methods are supported by all plugins. In fact, no single plugin supports all methods (yet). Each of these is an accessor function. If you pass it a value, it will set the variable. It always returns the value of the variable. It can return undef. =cut # This method is far from perfect. It can't be perfect. # It won't mangle valid UTF-8, however. # Just be sure to always return perl utf8 in plugins when possible. sub _isutf8 { my $self = shift; my $in = shift; # If it is a proper utf8, with tag, just return it. if ( Encode::is_utf8( $in, 1 ) ) { return $in; } my $has7f = 0; foreach ( split( //, $in ) ) { if ( ord($_) >= 0x7f ) { $has7f++; } } # No char >7F it is prob. valid ASCII, just return it. unless ($has7f) { return $in; } # See if it is a valid UTF-16 encoding. #my $out; #eval { # $out = decode("UTF-16", $in, 1); #}; #return $out unless $@; # See if it is a valid UTF-16LE encoding. #my $out; #eval { # $out = decode("UTF-16LE", $in, 1); #}; #return $out unless $@; # See if it is a valid UTF-8 encoding. my $out; eval { $out = decode( "UTF-8", $in, 1 ); }; return $out unless $@; # Finally just give up and return it. return $in; } sub _accessor { my $self = shift; my $attr = shift; my $value = shift; my $default = shift; unless ( exists $self->{data}->{ uc($attr) } ) { $self->{data}->{ uc($attr) } = undef; } if ( defined $value ) { $value = $self->_isutf8($value); if ( $self->options('verbose') ) { $self->status( 1, "Setting $attr to ", ( defined $value ) ? $value : "UNDEFINED" ); } $self->{data}->{ uc($attr) } = $value; } if ( ( defined $default ) && ( not defined $self->{data}->{ uc($attr) } ) ) { $self->{data}->{ uc($attr) } = $default; } return $self->{data}->{ uc($attr) }; } sub _timeaccessor { my $self = shift; my $attr = shift; my $value = shift; my $default = shift; if ( defined $value ) { if ( $value =~ /^(\d\d\d\d)[ \-]?(\d\d)?[ \-]?(\d\d)?[ \-]?(\d\d)?[ \-:]?(\d\d)?[ \-:]?(\d\d)?/ ) { $value = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $1, $2 || 1, $3 || 1, $4 || 12, $5 || 0, $6 || 0 ); if ( ( $1 == 0 ) || ( $1 eq "0000" ) || ( ( $1 == 1900 ) && ( $2 == 0 ) && ( $3 == 0 ) ) || ( ( $1 == 1900 ) && ( $2 == 1 ) && ( $3 == 1 ) ) ) { $self->status( 0, "Invalid date set for ${attr}: ${value}" ); $value = undef; } } else { $self->status( 0, "Invalid date set for ${attr}: ${value}" ); $value = undef; } } $self->_accessor( $attr, $value, $default ); } sub _epochaccessor { my $self = shift; my $attr = shift; my $value = shift; my $set = undef; return undef unless ( $self->options('TimeLocal') ); if ( defined($value) ) { my @tm = gmtime($value); $set = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $tm[5] + 1900, $tm[4] + 1, $tm[3], $tm[2], $tm[1], $tm[0] ); } my $v = $self->_timeaccessor( $attr, $set ); my $ret = undef; if ( ( defined $v ) && ( $v =~ /^(\d\d\d\d)[ \-]?(\d\d)?[ \-]?(\d\d)?[ \-]?(\d\d)?[ \-:]?(\d\d)?[ \-:]?(\d\d)?/ ) ) { eval { $ret = Time::Local::gmtime( $6 || 0, $5 || 0, $4 || 12, $3 || 1, $2 || 0, $1 ); }; $self->error($@) if $@; } return $ret; } sub _dateaccessor { my $self = shift; my $attr = shift; my $value = shift; my $set = undef; return undef unless ( $self->options('TimeLocal') ); if ( defined($value) ) { $set = $value; } my $v = $self->_timeaccessor( $attr, $set ); my $ret = undef; if ( ( defined $v ) && ( $v =~ /^(\d\d\d\d)[ \-]?(\d\d)?[ \-]?(\d\d)?[ \-]?(\d\d)?[ \-:]?(\d\d)?[ \-:]?(\d\d)?/ ) ) { return sprintf( "%04d-%02d-%02d", $1, $2, $3 ); } else { return undef; } } sub _ordinalaccessor { my $self = shift; my $attr = shift; my $pos = shift; my $total = shift; my $new = shift; if ( defined($new) ) { my ( $t, $tt ) = split( "/", $new ); my $r = ""; if ($t) { $self->_accessor( $pos, $t ); $r .= $t; } if ($tt) { $self->_accessor( $total, $t ); $r .= "/" . $tt; } } my $ret = $self->_accessor($pos); if ( $self->_accessor($total) ) { $ret .= "/" . $self->_accessor($total); } return $ret; } =pod =over 4 =item B The title of the release. =item B The type of the release. Specifically, the MusicBrainz type (ALBUM OFFICIAL, etc.) =item B The artist responsible for the album. Usually the same as the artist, and will return the value of artist if unset. =cut sub albumartist { my $self = shift; my $new = shift; return $self->_accessor( "albumartist", $new, $self->artist() ); } =item B The name of the sort-name of the albumartist (e.g. Hersh, Kristin or Throwing Muses, The) =cut sub albumartist_sortname { my $self = shift; my $new = shift; return $self->_accessor( "albumartist_sortname", $new, $self->sortname() ); } =pod =item B The artist responsible for the track. =item B The type of artist. Usually Group or Person. =item B The Amazon ASIN number for this album. =item B Bitrate of file (average). =item B URL to a digital booklet. Usually in PDF format. iTunes passes these out sometimes, or you could scan a booklet and use this to store value. URL is assumed to be relative to file location. =item B A comment about the track. =item B True if album is Various Artist, false otherwise. Don't set to true for Best Hits. =item B Composer of song. =item B A copyright message can be placed here. =cut =item B Return the country that the track was released in. =cut sub country { my $self = shift; my $new = shift; if ( defined($new) ) { $self->_accessor( "COUNTRYCODE", country2code($new) ); } if ( $self->countrycode ) { return code2country( $self->countrycode ); } return undef; } =pod =item B In a multi-volume set, the disc number. =item B In a multi-volume set, the title of a disc. =item B The disc number and optionally the total number of discs, seperated by a slash. Setting it sets the disc and totaldiscs values. =cut sub discnum { my $self = shift; my $new = shift; $self->_ordinalaccessor( "DISCNUM", "DISC", "TOTALDISCS", $new ); } =pod =item B The length of the track in milliseconds. Returns secs * 1000 if not set. Changes the value of secs when set. =cut sub duration { my $self = shift; my $new = shift; if ( defined($new) ) { $self->_accessor( "DURATION", $new ); $self->_accessor( "SECS", int( $new / 1000 ) ); } if ( $self->_accessor("DURATION") ) { return $self->_accessor("DURATION"); } elsif ( $self->_accessor("SECS") ) { return $self->_accessor("SECS") * 1000; } } =pod =item B The European Article Number on the package of product. Must be the EAN-13 (13 digits 0-9). =cut sub ean { my $self = shift; my $new = shift; if ( ($new) && ( $new =~ /\d{13}/ ) ) { return $self->_accessor( "EAN", $new ); } elsif ($new) { $self->status( 0, "Not setting EAN to invalid value: $new\n" ); } return $self->_accessor("EAN"); } =item B The codec used to encode the song. =item B The filename of the track. =cut sub filename { my $self = shift; my $new = shift; if ( defined($new) ) { my $file = $new; if ($new) { $file = File::Spec->rel2abs($new); } if ( $self->options('verbose') ) { $self->status( 1, "Setting filename to ", ( defined $file ) ? $file : "UNDEFINED" ); } $self->_accessor( "FILENAME", $file ); } return $self->_accessor("FILENAME"); } =item B The path that music file is located in. =cut sub filedir { my $self = shift; if ( $self->filename ) { my ( $vol, $path, $file ) = File::Spec->splitpath( $self->filename ); return File::Spec->catpath( $vol, $path, "" ); } return undef; } =pod =item B The frequency of the recording (in Hz). =item B The genre of the song. Various music tagging schemes use this field differently. It should be text and not a code. As a result, some plugins may be more restrictive in what can be written to disk, =item B Same as ean. =cut sub jan { my $self = shift; $self->ean(@_); } =item B