Difference between
modified post e2bd75f8 by Admin on Sun 3rd Apr 2011 21:04
original post 0f1a00 by Admin on Thu 17th Mar 2011 15:11
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | #!/opt/local/bin/perl | |
| 2 | 2 | ||
| 3 | 3 | use 5.010; | |
| 4 | 4 | use strict; | |
| 5 | 5 | use warnings; | |
| 6 | 6 | use File::Find qw(find); | |
| 7 | 7 | ||
| 8 | 8 | my $library = "F:/Music"; | |
| 9 | 9 | #$library = "F:/FLAC/Downloaded/New"; | |
| 10 | 10 | my %files; | |
| 11 | 11 | ||
| 12 | 12 | find({ wanted => \&action, no_chdir => 1 }, $library); | |
| 13 | 13 | ||
| 14 | 14 | sub action { | |
| 15 | 15 | if (-d) { | |
| 16 | 16 | my $dn = $File::Find::name; | |
| 17 | 17 | %files = getfiles($dn); | |
| 18 | 18 | my $fc = keys %files; | |
| 19 | 19 | # say $fc; | |
| 20 | 20 | unless ($fc == 0) { | |
| 21 | + | replaygain($dn); | |
| 21 | 22 | foreach my $fn (sort(keys %files)) { | |
| 22 | 23 | rmtag($fn, 'discnumber', 'disctotal', 'rating', 'comment'); | |
| 23 | - | replaygain($fn); | |
| 24 | 24 | albumartist($fn, $fc); | |
| 25 | 25 | track($fn); | |
| 26 | 26 | totaltracks($fn, $fc); | |
| 27 | 27 | # empty($fn); | |
| 28 | 28 | # duplicate($fn); | |
| 29 | 29 | } | |
| 30 | 30 | } | |
| 31 | 31 | ||
| 32 | 32 | ||
| 33 | 33 | undef %files; | |
| 34 | 34 | } | |
| 35 | 35 | } | |
| 36 | 36 | # Make this subroutine and the whole script so that each file name is stored in a hash and that each member contains an anonymous hash with the tags (the alltags hash). | |
| 37 | 37 | # This means it won't have to look up the tags again for the albumartist subroutine. | |
| 38 | 38 | ||
| 39 | 39 | sub gettags { | |
| 40 | 40 | my $fn = shift; | |
| 41 | 41 | my %alltags; | |
| 42 | 42 | open(OUTPUT, '-|', qq{metaflac --no-utf8-convert --export-tags-to=- "$fn"}) || die "can\'t run metaflac: $!"; | |
| 43 | 43 | local $_; | |
| 44 | 44 | while (<OUTPUT>) { | |
| 45 | 45 | chomp; | |
| 46 | 46 | my @tag = split('='); | |
| 47 | 47 | my $tagname = lc($tag[0]); | |
| 48 | 48 | if ($tag[1]) { | |
| 49 | 49 | push @{$alltags{$tagname}}, $tag[1]; | |
| 50 | 50 | } else { | |
| 51 | 51 | push @{$alltags{$tagname}}, 'null'; | |
| 52 | 52 | } | |
| 53 | 53 | } | |
| 54 | 54 | close(OUTPUT) || die "couldn\'t close metaflac: $!"; | |
| 55 | 55 | return %alltags; | |
| 56 | 56 | } | |
| 57 | 57 | ||
| 58 | 58 | sub getfiles { | |
| 59 | 59 | my $dn = shift; | |
| 60 | 60 | my %files; | |
| 61 | 61 | opendir(my $dh, $dn) or die "Can\'t open directory '$dn': $!"; | |
| 62 | 62 | foreach (readdir $dh) { | |
| 63 | 63 | my $fn = "$dn/$_"; | |
| 64 | 64 | if (/.flac$/ && -f $fn) { | |
| 65 | 65 | $files{$fn} = { gettags($fn) }; | |
| 66 | 66 | } | |
| 67 | 67 | } | |
| 68 | 68 | closedir $dh or die "Can\'t close directory '$dn': $!"; | |
| 69 | 69 | return %files; | |
| 70 | 70 | } | |
| 71 | 71 | ||
| 72 | 72 | sub replaygain { | |
| 73 | - | my $fn = shift; | |
| 73 | + | my $dn = shift; | |
| 74 | - | unless ($files{$fn}{replaygain_reference_loudness}) { | |
| 74 | + | my %replaygain; | |
| 79 | 75 | ||
| 80 | - | sub albumartist { | |
| 76 | + | foreach my $fn (sort(keys %files)) { | |
| 81 | - | my $fn = shift; | |
| 77 | + | if ($files{$fn}{replaygain_album_gain}) { | |
| 82 | - | my $tracks = shift; | |
| 78 | + | $replaygain{$files{$fn}{replaygain_album_gain}->[0]}++; | |
| 89 | 79 | } | |
| 92 | 80 | } | |
| 93 | - | foreach my $fn (keys %files) { | |
| 81 | + | if (keys(%replaygain) != 1) { | |
| 94 | - | $artist{$files{$fn}{artist}->[0]}++ | |
| 82 | + | print "$dn: adding ReplayGain..."; | |
| 83 | + | system('metaflac', '--add-replay-gain', keys(%files)); | |
| 95 | 85 | } | |
| 102 | 86 | } | |
| 87 | + |
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
kpaste.net RSS