tagged

 view release on metacpan or  search on metacpan

examples/tagit.pl  view on Meta::CPAN

#!/usr/bin/perl -w

use MP3::Tag;

# some settings for getting command-line options
use Getopt::Long;
Getopt::Long::Configure(qw/no_ignore_case_always ignore_case bundling/);
my %options = ( '--song=s'    => "Name of the song",
		'--album=s'   => "Album",
		'--artist=s'  => "Artist",
		'--comment=s' => "Comment",
		'--track=i'   => "Track",
		'--genre=s'   => "Genre",
		'--year=i'    => "Year",
		'--removetag' => "Removes an existing tag",
		'-q'          => "Be quiet",
		'-v'          => "Be verbose",
                '-f'          => "Force",
		'--show'      => "Show the existing tag",
		'--showgenres'=> "Show the existing genres (!!not yet!!)",
		'--setfilename' => "Set filename from tag (according to format string)",
		'--getfilename' => "Set tag according to filename (and format string)",
		'--format=s'  => "Set the format string for get/setfilenam (default: %a - %s.mp3)",
		'--nospaces'  => "Replace Spaces through _ in filenames",
		'--test'      => "Do NOT change the files. Only print which changes would be made",
		'--skipwithoutv1' => "Don't do anything if no ID3v1 tag exists",
	       );

# get the command line options
my %opt;
getoptions(\%opt, %options);

if (exists $opt{showgenres}) {
  my $genres = MP3::Tag::genres();
  print join (", ", @$genres) ."\n";
}

unless ($#ARGV >=0) {
  print "error: Filename(s) missing\n" unless exists $opt{showgenres};
  exit 0 if exists $opt{showgenres};
  exit 1;
}

# is there only one or more files to work with?
$opt{single}=1 if $#ARGV==0;

# prepare v and q flag (v has higher priority)
delete $opt{q} if (exists $opt{v} && exists $opt{q});

# prepare for setfilename / getfilename (not allowed together)
if (exists $opt{setfilename} && exists $opt{getfilename}) {
  print "error: Cannot use --setfilename and --getfilename together\n";
  delete $opt{setfilename};
  delete $opt{getfilename};
}
$opt{format} = "%a - %s.mp3" unless exists $opt{format};
my ($stencil, $details) = formatstr ($opt{format}) if exists $opt{setfilename} || exists $opt{getfilename};

# loop for each file
chomp(@ARGV = <STDIN>) unless @ARGV;
for my $filename (@ARGV) {
  # get the tags
  $mp3 = MP3::Tag->new($filename);
  unless (defined $mp3) {
    print "Skipping $filename ...\n";
    next;
  }
  $mp3->getTags;

  unless (exists $mp3->{ID3v1}) {
    print "No ID3v1-Tag found\n" if exists $opt{show} || exists $opt{v};
    next if exists $opt{skipwithoutv1};
    $mp3->newTag("ID3v1");
  }

  # deletet tag if wanted (option: --removetag)
  $mp3->{ID3v1}->removeTag if $opt{removetag};

  # set tag if this is wanted
  # option: --song, --artist, --album, --comment, --year, --genre, --track
  $mp3->{ID3v1}->song($opt{song}) if exists $opt{song};
  $mp3->{ID3v1}->artist($opt{artist}) if exists $opt{artist}; 
  $mp3->{ID3v1}->album($opt{album}) if exists $opt{album};
  $mp3->{ID3v1}->comment($opt{comment}) if exists $opt{comment};
  $mp3->{ID3v1}->year($opt{year}) if exists $opt{year};
  $mp3->{ID3v1}->genre($opt{genre}) if exists $opt{genre};
  $mp3->{ID3v1}->track($opt{track}) if exists $opt{track};
  
  if (exists $opt{song} || exists $opt{artist} || exists $opt{album} || 
      exists $opt{comment} || exists $opt{year} || exists $opt{genre} || exists $opt{track}) {
    if (exists $opt{test}) {
      # do nothing, but show new tag
      $opt{show} = 1;
    } else {
      if ($mp3->{ID3v1}->writeTag()) {
	print "Tag written\n" unless exists $opt{q};
      } else {
	print "Couldn't write tag\n" unless exists $opt{q};
      }
    }
  }
    
  # show tag (option --show)
  if ($opt{show}) {
    print "\nID3v1-Tag: $filename\n" unless exists $opt{single};
    print "New tag would be:\n" if exists $opt{test};
    print "   Song: " .$mp3->{ID3v1}->song . "\n";
    print " Artist: " .$mp3->{ID3v1}->artist . "\n";
    print "  Album: " .$mp3->{ID3v1}->album . "\n";
    print "Comment: " .$mp3->{ID3v1}->comment . "\n";
    print "   Year: " .$mp3->{ID3v1}->year . "\n";
    print "  Genre: " .$mp3->{ID3v1}->genre . "\n";
    print "  Track: " .$mp3->{ID3v1}->track . "\n";
  }

  # set filename from tag (option: --setfilename)
  # with --format the format of the new filename can be set (see formatstr below)
  if (exists $opt{setfilename}) {
    unless (exists $opt{test}) { # check if there really exists a tag
      $mp3->getTags;
      unless (exists $mp3->{ID3v1}) {
	print "No ID3v1 Tag exists. Can't change $filename\n";
	exit -1;



( run in 0.828 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )