Advertisement
Guest User

exscape

a guest
Jun 2nd, 2009
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.64 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use MP3::Tag;
  5. use Data::Dumper;
  6. use Getopt::Long;
  7.  
  8. my ($dump, $dumpraw, $listframes, $dumpframe, $deletetag, $listtags) = (0,0,0,0,undef,0); # $deletetag = undef = important
  9. GetOptions("dump" => \$dump, "dump-raw" => \$dumpraw, "list-frames" => \$listframes, "dump-frame=s" => \$dumpframe, "delete-tag=i" => \$deletetag, "list-tags" => \$listtags);
  10.  
  11. if (!$dump && !$listframes && !$dumpframe && !defined $deletetag && !$listtags && !$dumpraw) {
  12.     die "You need to specify either --dump, --dump-raw, --list-frames, --dump-frame XXXX, --list-tags or --delete-tag (1 or 2)";
  13. }
  14.  
  15. if (!(scalar @ARGV)) {
  16.     die "You need to specify at least one file!";
  17. }
  18.  
  19. for my $file (@ARGV) {
  20.     my $mp3 = MP3::Tag->new($file);
  21.     die "Unable to open file $file, exiting!" unless $mp3;
  22.  
  23.     $mp3->get_tags() or die "Unable to read any ID3 tags!";
  24.  
  25.     if ($dumpraw) {
  26.         print Dumper ( \$mp3 );
  27.         exit 0;
  28.     }
  29.  
  30.     if ($dump) {
  31.         if (exists $mp3->{ID3v1}) {
  32.             my $id3v1 = $mp3->{ID3v1};
  33.             print "File has an ID3v1 tag:\n";
  34.             print " Artist: " .$id3v1->artist . "\n";
  35.             print "  Title: " .$id3v1->title . "\n";
  36.             print "  Album: " .$id3v1->album . "\n";
  37.             print "   Year: " .$id3v1->year . "\n";
  38.             print "  Genre: " .$id3v1->genre . "\n";
  39.             print "Comment: " .$id3v1->comment . "\n";
  40.             print "  Track: " .$id3v1->track . "\n";
  41.             print "\n";
  42.         }
  43.         if (exists $mp3->{ID3v2}) {
  44.             my $id3v2 = $mp3->{ID3v2};
  45.             print "File has an ID3v2 tag with the following frames/values:\n";
  46.            
  47.             my $frames = $id3v2->get_frame_ids();
  48.  
  49.             $Data::Dumper::Terse = 1;
  50.  
  51.             foreach my $frame (keys %$frames) {
  52.                 my ($info, $name, @rest) = $id3v2->get_frame($frame);
  53.                 if (ref $info) {
  54.                     print "$frame: " . Dumper ( $info );
  55.                 }
  56.                 else {
  57.                     print "$frame: $info\n";
  58.                 }
  59.             }      
  60.         }
  61.         exit 0;
  62.     }
  63.  
  64.     if ($listtags) {
  65.         if (exists $mp3->{ID3v1}) {
  66.             print "$file has an ID3v1 tag\n";
  67.         }
  68.         if (exists $mp3->{ID3v2}) {
  69.             my $v2ver = $mp3->{ID3v2}->version();
  70.             print "$file has an ID3v2.$v2ver tag\n";
  71.         }
  72.         exit 0;
  73.     }
  74.  
  75.     if ($deletetag) {
  76.         if ($deletetag != 1 && $deletetag != 2) {
  77.             die "--delete-tag needs either 1 (ID3v1) or 2 (ID3v2) as an argument!";
  78.         }
  79.  
  80.         if ($deletetag == 1) {
  81.             if (exists $mp3->{ID3v1}) {
  82.                 if ($mp3->{ID3v1}->remove_tag()) {
  83.                     print "Removed ID3v1 tag successfully\n";
  84.                     exit 0;
  85.                 }
  86.                 else {
  87.                     print "Failed to remove ID3v1 tag!\n";
  88.                     exit 1;
  89.                 }
  90.             }
  91.             else {
  92.                 print "Track has no ID3v1 tag to remove!\n";
  93.                 exit 1;
  94.             }
  95.         }
  96.  
  97.         elsif ($deletetag == 2) {
  98.             if (exists $mp3->{ID3v2}) {
  99.                 if ($mp3->{ID3v2}->remove_tag()) {
  100.                     print "Removed ID3v2 tag successfully\n";
  101.                     exit 0;
  102.                 }
  103.                 else {
  104.                     print "Failed to remove ID3v2 tag!\n";
  105.                     exit 1;
  106.                 }
  107.             }
  108.             else {
  109.                 print "Track has no ID3v2 tag to remove!\n";
  110.                 exit 1;
  111.             }
  112.         }
  113.    
  114.     }
  115.     if ($dumpframe) {
  116.         my $v2 = $mp3->{ID3v2} if exists $mp3->{ID3v2};
  117.         if (!defined $v2) {
  118.             die "No ID3v2 tag found!";
  119.         }
  120.    
  121.         my $frame = $v2->get_frame($dumpframe);
  122.         if (!defined $frame || length $frame < 1) {
  123.             die "File $file doesn't appear to contain any frame named $dumpframe";
  124.         }
  125.  
  126.         print "$dumpframe: ";
  127.         binmode STDOUT;
  128.         $Data::Dumper::Terse = 1;
  129.         print Dumper ( \$frame );
  130.         exit 0;
  131.     }
  132.  
  133.     if ($listframes) {
  134.         my $v2 = $mp3->{ID3v2} if exists $mp3->{ID3v2};
  135.         if (!defined $v2) {
  136.             die "No ID3v2 tag found!";
  137.         }
  138.         my $frames = $v2->get_frame_ids();
  139.         #while (my ($k, $v) = each(%{$frames})) {
  140.         print "File $file contains the following ID3v2 frames:\n";
  141.         for my $k (sort keys %{$frames}) {
  142.             print "$k: " . $frames->{$k} . "\n";
  143.         }
  144.         exit 0;
  145.     }
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement