summaryrefslogtreecommitdiffstats
path: root/bin/mp3q
blob: 17831b8057316086ffcca729d7b27cab606a521f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env perl
use strict;
use warnings;
use File::Next;
use File::Basename;
use MP3::Info;
use MP4::Info;
use Ogg::Vorbis::Header::PurePerl;

sub color {
    my ($bitrate) = @_;
    $bitrate ||= 0;
    if ($bitrate >= 192) {
        return "1;32";
    }
    elsif ($bitrate >= 160) {
        return "32";
    }
    elsif ($bitrate >= 128) {
        return "33";
    }
    elsif ($bitrate >= 96) {
        return "31";
    }
    elsif ($bitrate == 0) {
        return "1;30";
    }
    else {
        return "37;41";
    }
}

sub bitrate {
    my ($file) = @_;
    if ($file =~ /\.mp3$/i) {
        return MP3::Info->new($file)->bitrate;
    }
    elsif ($file =~ /\.m4a$/i) {
        return MP4::Info->new($file)->bitrate;
    }
    elsif ($file =~ /\.ogg$/i) {
        return Ogg::Vorbis::Header::PurePerl->new($file)->info->{bitrate_nominal} / 1000.0;
    }
    elsif ($file =~ /\.(?:m4p|jpg|ini|db|exe|wma|nfo|m3u|zip|m4v|txt|DS_Store|mpe?g|mov|cue|ncd|xml|rar|png)$/i) {
        return 0;
    }
    elsif ($file =~ /\.(?:wav|flac)$/i) {
        return 1440;
    }
    elsif ($file !~ /\./) {
        return 0;
    }
    else {
        die "unknown file type: $file";
    }
}

my @dirs = @ARGV;
push @dirs, '.' unless @dirs;

my $files = File::Next::files({sort_files => 1}, @dirs);

#mkdir 'unnecessary';

while (defined (my $file = $files->())) {
    my $bitrate = bitrate($file);
    print "\e[", color($bitrate), "m";
    print "$file: $bitrate";
    print "\e[m\n";
    if ($bitrate < 96) {
        my $new_name = fileparse $file;
        $new_name = 'unnecessary/'.$new_name;
        #rename $file, $new_name;
    }
}