All pastes #1585982 Raw Edit

mp3tag-enc.pl

public text v1 · immutable
#1585982 ·published 2009-09-30 13:47 UTC
rendered paste body
#!/usr/bin/perl

use Encode 'from_to';
use MP3::Tag;

$from_enc = (shift or 'shiftjis');
$to_enc   = (shift or 'euc-jp');

while($filename = shift) {
    mp3tag_enc($filename);
}

sub mp3tag_enc {
    my $filename = shift or return;

    $mp3 = MP3::Tag->new($filename);

    # scan file for existing tags
    $mp3->get_tags;

    if (exists $mp3->{ID3v1}) {
	# read some information from the tag
	$id3v1 = $mp3->{ID3v1};  # $id3v1 is only a shortcut for $mp3->{ID3v1}
	@tagdata = $id3v1->all();
	foreach $tag (@tagdata) {
	    from_to($tag, $from_enc, $to_enc, 1);
	}

	# change the tag contents
	$id3v1->all(@tagdata);
	$id3v1->write_tag;
    }

    if (exists $mp3->{ID3v2}) {
	$id3v2 = $mp3->{ID3v2};
	$frameIDs_hash = $id3v2->get_frame_ids;

	foreach my $frame (keys %$frameIDs_hash) {
	    my ($info, $name) = $id3v2->get_frame($frame);
	    if (ref $info) {
		print "$name ($frame):\n";
		while(my ($key,$val)=each %$info) {
		    if ($key ne '_Data') {
			print " * $key => $val\n";
		    } else {
			print " * $key => *\n";
		    }
		}
	    } else {
		if ($frame =~ /^T/) {
		    from_to($info, $from_enc, $to_enc, 1);
		    $id3v2->change_frame($frame, $info);
		} else {
		    print "$name ($frame): $info\n";
		}
	    }
	}
	# update the tag
	$id3v2->write_tag();
    }

    $mp3->close();
}