Category: | Audio Related Programs |
Author/Contact Info | The Mad Hatter |
Description: | Given a directory, this will go thru and try to figure out artist, album, and title from the directory structure and filename. A few options allow you to tweak how it gets the information. Once it has gathered the info, it then writes an ID3v2 tag. I would support Ogg Vorbis, but I couldn't find a module on CPAN that could write tags to Oggs. Requires MP3::Tag, File::Find::Rule, File::Spec, and Cwd. (The last two are in the core.) Nota Bene: Use at your own risk. If this messes up your pristine music collection, makes your socks smell, computer implode, or generally screws up anything, I am not liable. |
#!/usr/bin/perl # # "Intelligent" ID3v2 tagger for MP3s # our $VERSION = 0.04; use strict; use warnings; use MP3::Tag; use File::Find::Rule; use File::Spec; use Cwd; my $dir = ""; # Search by default nowhere my $ext = '.mp3'; # Grab files with the extension of .mp +3 my $noSplit = 0; # If the file does NOT contain a hyph +en, split into artist/title my $titleFirst = 0; # Format: title - artist instead of: a +rtist - title my $artistFromName = 0; # Grab artist from filename my $noOverwrite = 0; # Don't overwrite existing tags my $help = 0; # Show help my $cwd = Cwd::getcwd(); use Getopt::Long; GetOptions("dir=s" => \$dir, "ext=s" => \$ext, "no-split" => \$noSplit, "title-first" => \$titleFirst, "artist-from-name" => \$artistFromName, "no-overwrite" => \$noOverwrite, "help" => \$help); if ($help || !$dir) { print <<" HELP"; $0 - "intelligent" ID3v2 tagger for MP3s, version $VERSION Usage (defaults in brackets): -d, --directory=DIR directory to start search in [$dir] -e, --extension=EXT extension to search for [$ext] --no-split do NOT split the filename into: artist - titl +e [$noSplit] -t, --title-first if not --no-split, use: title - artist instea +d [$titleFirst] -a, --artist-from-name get the artist from the filename [$artistFrom +Name] --no-overwrite don't overwrite any existing info [$noOverwri +te] HELP exit; } my @mp3s = File::Find::Rule->file() ->name("*$ext") ->in($dir); foreach my $mp3 (@mp3s) { my @spec = File::Spec->splitdir(File::Spec->abs2rel($mp3, $dir)); my %file; $file{name} = $spec[$#spec]; if (@spec >= 4) { $file{genre} = $spec[$#spec - 3]; $file{artist} = $spec[$#spec - 2]; $file{album} = $spec[$#spec - 1]; } elsif (@spec == 3) { $file{artist} = $spec[$#spec - 2]; $file{album} = $spec[$#spec - 1]; } elsif (@spec == 2) { $file{artist} = $spec[$#spec - 1]; } my $sansExtension = $file{name}; $sansExtension =~ s/\Q$ext\E\z//; if (!$noSplit && $sansExtension =~ /\s*-\s*/) { my ($artist, $title); unless ($titleFirst) { ($artist, $title) = split /\s*-\s*/, $sansExtension, 2; } else { ($title, $artist) = split /\s*-\s*/, $sansExtension, 2; } $file{title} = $title; $file{artist} = $artist if !$file{artist} || $artistFromName; } else { $file{title} = $sansExtension; } my $tag = MP3::Tag->new($mp3); # read an existing tag $tag->get_tags(); my $id3v2; $id3v2 = exists $tag->{ID3v2} ? $tag->{ID3v2} : $tag->new_tag("ID3v2"); unless ($noOverwrite) { $id3v2->remove_tag; $id3v2 = $tag->new_tag("ID3v2"); $id3v2->add_frame("TIT2", $file{title}) if $file{title}; $id3v2->add_frame("TPE1", $file{artist}) if $file{artist}; $id3v2->add_frame("TALB", $file{album}) if $file{album}; $id3v2->add_frame("TCON", $file{genre}) if $file{genre}; $id3v2->write_tag; } else { $id3v2->remove_frame($_) for qw/TIT2 TPE1 TALB TCON/; $id3v2->add_frame("TIT2", $file{title}) if $file{title}; $id3v2->add_frame("TPE1", $file{artist}) if $file{artist}; $id3v2->add_frame("TALB", $file{album}) if $file{album}; $id3v2->add_frame("TCON", $file{genre}) if $file{genre}; } $id3v2->write_tag; } |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: "Intelligent" ID3v2 Tagger
by Mr_Person (Hermit) on Jul 21, 2003 at 13:14 UTC | |
by The Mad Hatter (Priest) on Jul 21, 2003 at 13:18 UTC | |
Re: "Intelligent" ID3v2 Tagger
by Chady (Priest) on Jul 21, 2003 at 13:44 UTC | |
by barbie (Deacon) on Jul 21, 2003 at 17:52 UTC | |
by The Mad Hatter (Priest) on Jul 21, 2003 at 18:34 UTC | |
Re: "Intelligent" ID3v2 Tagger
by Anonymous Monk on Dec 23, 2004 at 11:53 UTC |