播出软件的音频解码单元无法处理一些特殊码率的mp3文件,为了保证播放的安全和流畅,需要将这些特殊码率的文件剔除掉。使用诸如千千静听和foobar等播放器都可以查看文件的码率,但是如果文件数量很多,这种人工查看的办法非常费时。所以用脚本来完成一项工作。
1use strict;
2use File::Find;
3use MP3::Tag;
4
5my ($path, $delete) = @ARGV;
6
7find(\&wanted, $path);
8
9sub wanted{
10 /\.mp3$/ && do {
11 my $mp3 = MP3::Tag->new("$_");
12 if (defined($mp3) && is_normal_bitrate($mp3->bitrate_kbps())){
13 remove($_);
14 }
15 };
16}
17
18sub is_normal_bitrate{
19 my $bitrate = shift;
20 for (64, 128, 192, 256){
21 if($bitrate == $_) {
22 return 1;
23 }
24 }
25 return 0;
26}
27
28sub remove{
29 my $filename = shift;
30 my @paths = split(/\//, $path);
31 my $recycled = $paths[0]."/$filename";
32
33 open(FILE, $filename);
34 open(ANO, ">".$recycled) or die $!;
35
36 binmode FILE;
37 binmode ANO;
38
39 my $offset = 0;
40 my $buffer = undef;
41 my $number = 0;
42 while(($number = sysread(FILE, $buffer, 1024, $offset)) != 0){
43 syswrite(ANO, $buffer, $number, $offset);
44 $offset += $number;
45 }
46
47 close FILE;
48 close ANO;
49
50 if($delete eq "-d") {unlink $_;}
51}