在MP3文件中编辑艺术家名称Perl Script ID3V2标签

时间:2014-01-04 20:01:11

标签: perl id3v2

我正在尝试编写用于编辑ID3V2标签的Perl代码,即开始的艺术家名称,我可以查找并阅读它但是当我尝试编写新文件时,只需用新的艺术家名称替换文件的所有内容并损坏这个文件我是新来的,所以请给我一些指导,因为我不想使用下面的库是我试过的代码。

#!/usr/bin/perl

use Fcntl qw( SEEK_SET );

my($fh, $filename, $byte_position, $byte_value);

$filename = $ARGV[0];

open(IN, "+>", $filename);
#open IN, $filename;

seek(IN,0,SEEK_SET);
read IN, $temp, 128;

print $temp;
print "\n";

seek(IN,14,SEEK_SET);
read IN, $temp, 16;
print "Artist is :" .$temp;
print "\n";


sysseek(IN,14,SEEK_SET);
#want to replace the Artist Name with new one.
syswrite (IN,$newArtist);

print "\n";

close(IN);

2 个答案:

答案 0 :(得分:1)

您的open调用会截断该文件。您需要使用+<而不是+>打开文件,以便在不截断的情况下打开文件进行读写。

有关详细信息,请参阅perldoc -f open

答案 1 :(得分:0)

有些事情对我有用,以防有人在这里寻找解决方案。

my $myFile  = shift or die "Usage: <file.mp3> <artist> <title>\n";
my $newArtist   = shift or die "Usage: <file.mp3> <artist> <title>\n";
my $newTitle    = shift or die "Usage:<file.mp3> <artist> <title> \n";

#Pack data for artist name and title. creating frame of size 4byte.
my $artist_len = length($newArtist);
my $title_len = length($newTitle);
my $artist = pack('N',$artist_len);
my $title = pack('N',$title_len);

print"\n New artist name will be >>". $newArtist."\n";
print"\n New title will be >> ". $newTitle ."\n";

my $encoding = ":raw :bytes";
open myMP3, "+<$encoding",$myFile or die "Error!Can NOT open the MP3 file.\n";
open OUT_FILE, "+>$encoding", 'new.mp3' or die "Error!Can NOT open the file to write.\n";

my $length = 512;
read (myMP3, my $buffer, $length);
chomp($buffer);

#reading the first 10 bytes of the ID3v2 Header
my $tagheader = substr($buffer, 0, 10);
my ($IDtag, $version, $revision, $flags, $size) = unpack('A3 h h h N4',$tagheader);
print OUT_FILE $tagheader;

my $len = 0;
my $ptr1 = 0;
my $ptr2 = 0;

while(1)
{    
   #reading 10 bytes for each frame and adding 10 bytes for next frame  
    $ptr1 += 10+$len;
    $ptr2 = $ptr1+10;

   #reading header 
   #4bytes frame ID,
   #4bytes frame size
   #2bytes flags
    my $frameHeader = substr($buffer,$ptr1,10);
    my($frameID, $frameSize, $flag) = unpack('A4 N4 h2',$frameHeader);

    #TALB:album-name,TCON:content-type,TIT2:title,TPE1:Artist, TRCK:Track Number,TYER: year
    if (($frameID eq 'TALB') || ($frameID eq 'TCON') || ($frameID eq 'TIT2') || ($frameID eq  'TPE1') || ($frameID eq 'TRCK') || ($frameID eq  'TYER'))
    {

        my $readFrame = substr($buffer, $ptr2, $frameSize);
        my $myFrame = unpack('A*($frameSize)' ,$readFrame);
        if ($frameID eq "TPE1")
        {
                print OUT_FILE $frameID;
                print OUT_FILE $artist;
                print OUT_FILE $flag;
                print OUT_FILE $newArtist;
         }

        elsif ($frameID eq "TIT2") 
        {
                print OUT_FILE $frameID;
                print OUT_FILE $title;
                print OUT_FILE $flag;
                print OUT_FILE $newTitle;
        }

        else 
        {
            print OUT_FILE $frameHeader;
            print OUT_FILE $readFrame;
        }
    }
    else 
    {
         my $leng = length($buffer) - $ptr1;
         my $music = substr($buffer, $ptr1, $leng);
         print OUT_FILE $music;

         seek(myMP3 , 10 , 0);
         seek(OUT_FILE, 10 , 0);
         while(<OUT_FILE>)
         {
            print myMP3 $_;
         }
         unlink 'new.mp3';

        #close files
        close(myMP3);
        close(OUT_FILE);

        die "\n Update success!\n";

    }
    $len=$frameSize;
}