我正在编写一个小的perl脚本,根据文件名重新标记我的MP3集。
#!/usr/bin/perl
use strict;
use warnings;
use MP3::Tag;
use File::Find;
MP3::Tag->config(write_v24 => 1);
my $dirpath = "../MP3s/";
finddepth(\&wanted, $dirpath);
sub wanted {
unless (-d $_) {
my ($track,$artist,$title);
if(($track,$artist,$title) = ($_ =~ m/(\d+) - (.+?) - (.+)\.mp3$/g)){
#handle songs with a track number
my $mp3 = MP3::Tag->new($_) or die $!;
$mp3->track_set($track);
$mp3->artist_set($artist);
$mp3->title_set($title);
$File::Find::dir =~ m/.*\/(.*)/;
$mp3->album_set($1);
$mp3->update_tags();
$mp3->close();
print "$track - $artist - $title\n";
} elsif(($artist,$title) = ($_ =~ m/(.+?) - (.+)\.mp3$/g)){
#handle songs without a track number
my $mp3 = MP3::Tag->new($_) or die $!;
$mp3->track_set("");
$mp3->artist_set($artist);
$mp3->title_set($title);
$mp3->update_tags();
$mp3->close();
print "$artist - $title\n";
}
}
}
这样可以正常工作,但是有些文件会使脚本因此错误而崩溃:
UTF-16:Unrecognised BOM 3100 at C:/strawberry/perl/lib/Encode.pm line 175.
导致此错误的原因是什么?文件名没有特殊字符,如德语变音符号(ä,ö,ü)。
如何解决此问题或跳过此类文件?