Main Page/Stuff/Ripping Karaoke CDs/mp3tag.pl
From phurvitz
< Main Page | Stuff | Ripping Karaoke CDs
<source lang=perl>
- ! /usr/bin/perl
use MP3::Tag; use File::Basename; use Getopt::Std;
sub init() {
my $opt_string = 's:'; getopts( "$opt_string", \%opt ) or usage();
}
init();
sub usage() {
print "Tags MP3 files based on file name\n"; print "Usage: $0 -s sourcdir\n"; exit;
}
$sourcedir = $opt{s};
- read input files
opendir(SRCDIR, "$sourcedir"); @srcfiles = readdir(SRCDIR);
- if the filename is "*.bin" then push it to a new array
foreach $filename (@srcfiles) {
my ($name,$dir,$ext) = fileparse($filename ,qr/\.[^.]*/); if ($ext eq ".mp3") { print "tagging $filename\n"; tag(); }
}
sub tag() { use MP3::Tag;
# get the file and split into name pieces my ($name,$dir,$ext) = fileparse($filename ,qr/\.[^.]*/); @ns = split(" - ", $name); $album = $ns[0]; $track = $ns[1]; $artist = $ns[2]; $title = $ns[3]; # get the MP3 tag $fullfile = join("/", $sourcedir, $filename); $mp3 = MP3::Tag->new($fullfile); @tags = $mp3->get_tags(); # create tags if necessary if (!exists $mp3->{ID3v1}) { $mp3->new_tag("ID3v1"); $mp3->{ID3v1}->write_tag; } if (!exists $mp3->{ID3v2}) { $mp3->new_tag("ID3v2"); $mp3->{ID3v2}->write_tag; }
$mp3->config("autoinfo","ID3v1","ID3v2","filename"); @tags = $mp3->get_tags;
# change the tag contents $mp3->{ID3v1}->artist($artist); $mp3->{ID3v1}->album($album); $mp3->{ID3v1}->track($track); $mp3->{ID3v1}->title($title); $mp3->{ID3v1}->write_tag; $mp3->{ID3v2}->artist($artist); $mp3->{ID3v2}->album($album); $mp3->{ID3v2}->track($track); $mp3->{ID3v2}->title($title); $mp3->{ID3v2}->write_tag; print "\n"; $mp3->close;
}
exit;
</source>