Main Page/Stuff/Ripping Karaoke CDs/tag2filename.pl

From phurvitz
Jump to: navigation, search

<source lang="perl">

  1. ! /usr/bin/perl

use MP3::Tag; use File::Basename; use Getopt::Std;

$sourcedir = $ARGV[0];

  1. read input files

opendir(SRCDIR, "$sourcedir"); @srcfiles = readdir(SRCDIR);

  1. 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") or ($ext eq ".MP3")) {
       print "tagging $filename";
       tag();
   }

}

sub tag() { use MP3::Tag;

   # 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;

# get the artist and title tags $artist = $mp3->{ID3v1}->artist; $title = $mp3->{ID3v1}->title; if($artist eq ""){ $artist = $mp3->{ID3v2}->artist; $title = $mp3->{ID3v2}->title; }

   print "\n";
   $mp3->close;

$newname = join(" - ", $artist, $title,); $newname = "$newname.mp3";

   $newfile = join("/", $sourcedir, $newname);

($oldcdg = $fullfile) =~ s/\.mp3/\.cdg/; ($newcdg = $newfile) =~ s/\.mp3/\.cdg/;

print "rename $fullfile $newfile\n"; print "rename $oldcdg $newcdg\n"; rename ($fullfile, $newfile); rename ($oldcdg, $newcdg); }

exit;

</source>