Difference between
modified post 42d8059b by Anonymous on Wed 5th Sep 2012 14:55
original post 8a by Anonymous on Wed 5th Sep 2012 10:09
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | #!/usr/bin/perl | |
| 2 | 2 | ||
| 3 | 3 | use 5.14.0; | |
| 4 | 4 | use strict; | |
| 5 | 5 | use warnings; | |
| 6 | 6 | #use File::Find qw(finddepth); | |
| 7 | 7 | use File::Copy qw(move); | |
| 8 | 8 | use File::Basename qw(basename dirname); | |
| 9 | 9 | use File::Path qw(make_path); | |
| 10 | 10 | ||
| 11 | 11 | #my $library = "/home/sir/Music/Songbird Music"; | |
| 12 | 12 | my $library = "/home/sir/Music/Songbird Music"; | |
| 13 | 13 | my (%files, @files, @dirs); | |
| 14 | 14 | ||
| 15 | 15 | getdirs($library); | |
| 16 | 16 | ||
| 17 | - | foreach my $dn (@dirs) { | |
| 17 | + | foreach my $dn (sort(@dirs)) { | |
| 18 | - | chomp(my $ls = `ls "$dn"/*.flac 2>- | head -n 1`); | |
| 18 | + | chomp(my $ls = `ls "$dn"/*.flac 2> /dev/null | head -n 1`); | |
| 19 | 19 | if (-f $ls) { | |
| 20 | 20 | # my @stat = stat($dn); | |
| 21 | 21 | # my $ino = $stat[1]; | |
| 22 | 22 | # say "$dn has $ino"; | |
| 23 | 23 | undef %files; | |
| 24 | 24 | undef @files; | |
| 25 | 25 | getfiles($dn); | |
| 26 | 26 | my $fc = scalar(@files); | |
| 27 | 27 | if ($fc > 0) { | |
| 28 | - | replaygain($dn); | |
| 28 | + | #replaygain($dn); | |
| 29 | 29 | for (my $n = 0; $n < $fc; $n++) { | |
| 30 | 30 | #say $files[$n]; | |
| 31 | 31 | my $fn = $files[$n]; | |
| 32 | 32 | ||
| 33 | 33 | #rmtag($fn, 'rating'); | |
| 34 | 34 | #albumartist($fn, $fc); | |
| 35 | 35 | #track($fn); | |
| 36 | 36 | #totaltracks($fn, $fc); | |
| 37 | 37 | # empty($fn); | |
| 38 | 38 | # duplicate($fn); | |
| 39 | 39 | discnum($fn, $n, $fc); | |
| 40 | 40 | } | |
| 41 | 41 | } | |
| 42 | 42 | } | |
| 43 | 43 | } | |
| 44 | 44 | # 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). | |
| 45 | 45 | # This means it won't have to look up the tags again for the albumartist subroutine. | |
| 46 | 46 | ||
| 47 | 47 | sub gettags { | |
| 48 | 48 | my $fn = quotemeta(shift); | |
| 49 | 49 | my (%alltags, @mflac); | |
| 50 | 50 | open(OUTPUT, '-|', qq{metaflac --no-utf8-convert --export-tags-to=- $fn}) || die "can't run metaflac: $!"; | |
| 51 | 51 | chomp(@mflac = (<OUTPUT>)); | |
| 52 | 52 | foreach (@mflac) { | |
| 53 | 53 | my @tag = split('='); | |
| 54 | 54 | my $tagname = lc($tag[0]) or say $fn; | |
| 55 | 55 | if ($tag[1]) { | |
| 56 | 56 | push @{$alltags{$tagname}}, $tag[1]; | |
| 57 | 57 | } else { | |
| 58 | 58 | push @{$alltags{$tagname}}, 'null'; | |
| 59 | 59 | } | |
| 60 | 60 | } | |
| 61 | 61 | close(OUTPUT) || die "couldn't close metaflac: $!"; | |
| 62 | 62 | return %alltags; | |
| 63 | 63 | } | |
| 64 | 64 | ||
| 65 | 65 | sub getdirs { | |
| 66 | 66 | ||
| 67 | 67 | my $dn = shift; | |
| 68 | 68 | ||
| 69 | 69 | open(FIND, '-|', qq(find -L "$dn" -name "*" -type d)) | |
| 70 | 70 | or die "Can't run 'find': $!"; | |
| 71 | 71 | chomp(@dirs = (<FIND>)); | |
| 72 | 72 | close(FIND) or die "Can't close 'find': $!"; | |
| 73 | 73 | #@dirs = `find -L "$dn" -name "*" -type d -mindepth 1`; | |
| 74 | 74 | } | |
| 75 | 75 | ||
| 76 | 76 | sub getfiles { | |
| 77 | 77 | my $dn = shift; | |
| 78 | 78 | opendir(my $dh, $dn) or die "Can't open directory '$dn': $!"; | |
| 79 | 79 | foreach (readdir $dh) { | |
| 80 | 80 | my $fn = "$dn/$_"; | |
| 81 | 81 | if (/.flac$/ && -f $fn) { | |
| 82 | 82 | push(@files, $fn); | |
| 83 | 83 | $files{$fn} = { gettags($fn) }; | |
| 84 | 84 | } | |
| 85 | 85 | } | |
| 86 | 86 | closedir $dh or die "Can't close directory '$dn': $!"; | |
| 87 | 87 | } | |
| 88 | 88 | ||
| 89 | 89 | sub replaygain { | |
| 90 | 90 | my $dn = shift; | |
| 91 | 91 | my %replaygain; | |
| 92 | 92 | ||
| 93 | 93 | #foreach ('REFERENCE_LOUDNESS', 'TRACK_GAIN', 'TRACK_PEAK', 'ALBUM_GAIN', 'ALBUM_PEAK') { | |
| 94 | 94 | # my $tag = 'REPLAYGAIN_' . $_; | |
| 95 | 95 | # system('metaflac', "--remove-tag=$tag", keys(%files)); | |
| 96 | 96 | #} | |
| 97 | 97 | ||
| 98 | 98 | foreach my $fn (sort(keys %files)) { | |
| 99 | 99 | if ($files{$fn}{replaygain_album_gain}) { | |
| 100 | 100 | $replaygain{$files{$fn}{replaygain_album_gain}->[0]}++; | |
| 101 | 101 | } | |
| 102 | 102 | } | |
| 103 | 103 | if (keys(%replaygain) != 1) { | |
| 104 | 104 | print "$dn: adding ReplayGain..."; | |
| 105 | 105 | system('metaflac', '--add-replay-gain', keys(%files)); | |
| 106 | 106 | say " done"; | |
| 107 | 107 | } | |
| 108 | 108 | } | |
| 109 | 109 | ||
| 110 | 110 | sub albumartist { | |
| 111 | 111 | my $fn = shift; | |
| 112 | 112 | my $tracks = shift; | |
| 113 | 113 | ||
| 114 | 114 | if (!$files{$fn}{albumartist}) { | |
| 115 | 115 | ||
| 116 | 116 | my $albumartist = $files{$fn}{artist}->[0]; | |
| 117 | 117 | my %artist; | |
| 118 | 118 | my $max; | |
| 119 | 119 | if ($tracks == 1) { $max = $tracks } else { $max = $tracks / 2 } | |
| 120 | 120 | foreach my $fn (keys %files) { $artist{$files{$fn}{artist}->[0]}++ } | |
| 121 | 121 | if (keys(%artist) > $max) { | |
| 122 | 122 | $albumartist = 'Various Artists'; | |
| 123 | 123 | } | |
| 124 | 124 | say "$fn: adding albumartist tag"; | |
| 125 | 125 | system('metaflac', "--set-tag=ALBUMARTIST=$albumartist", $fn); | |
| 126 | 126 | } | |
| 127 | 127 | } | |
| 128 | 128 | ||
| 129 | 129 | sub empty { | |
| 130 | 130 | my $fn = shift; | |
| 131 | 131 | foreach my $k (keys %{$files{$fn}}) { | |
| 132 | 132 | foreach (@{$files{$fn}{$k}}) { | |
| 133 | 133 | if ($_ eq 'null') { | |
| 134 | 134 | say "$fn: WARNING: empty $k tag"; | |
| 135 | 135 | } | |
| 136 | 136 | } | |
| 137 | 137 | } | |
| 138 | 138 | } | |
| 139 | 139 | ||
| 140 | 140 | sub duplicate { | |
| 141 | 141 | my $fn = shift; | |
| 142 | 142 | my %exists; | |
| 143 | 143 | foreach my $k (keys %{$files{$fn}}) { | |
| 144 | 144 | foreach (@{$files{$fn}{$k}}) { | |
| 145 | 145 | my $tag = quotemeta($k . '=' . $_); | |
| 146 | 146 | if ($exists{$tag}) { | |
| 147 | 147 | say "$fn: WARNING: duplicate $k tag"; | |
| 148 | 148 | } else { | |
| 149 | 149 | $exists{$tag} = 1; | |
| 150 | 150 | } | |
| 151 | 151 | } | |
| 152 | 152 | } | |
| 153 | 153 | } | |
| 154 | 154 | ||
| 155 | 155 | sub track { | |
| 156 | 156 | my $fn = shift; | |
| 157 | 157 | my $track = $files{$fn}{tracknumber}->[0]; | |
| 158 | 158 | if (!$track) { | |
| 159 | 159 | say "$fn: doesn't have any tracknumber tag" | |
| 160 | 160 | } | |
| 161 | 161 | elsif ($track =~ m/^0/ && $track != 0) { | |
| 162 | 162 | say "$fn: fixing tracknumber tag"; | |
| 163 | 163 | $track =~ s/^0+//; | |
| 164 | 164 | system('metaflac', '--remove-tag=TRACKNUMBER', "--set-tag=TRACKNUMBER=$track", $fn); | |
| 165 | 165 | } | |
| 166 | 166 | } | |
| 167 | 167 | ||
| 168 | 168 | sub totaltracks { | |
| 169 | 169 | my $fn = shift; | |
| 170 | 170 | my $tracks = shift; | |
| 171 | 171 | if ( | |
| 172 | 172 | !$files{$fn}{totaltracks} && | |
| 173 | 173 | !$files{$fn}{tracktotal} | |
| 174 | 174 | ) { | |
| 175 | 175 | say "$fn: adding totaltracks tag"; | |
| 176 | 176 | system('metaflac', "--set-tag=TOTALTRACKS=$tracks", $fn); | |
| 177 | 177 | } | |
| 178 | 178 | elsif ( | |
| 179 | 179 | $files{$fn}{tracktotal} && | |
| 180 | 180 | !$files{$fn}{totaltracks} | |
| 181 | 181 | ) { | |
| 182 | 182 | say "$fn: replacing tracktotal tag with totaltracks"; | |
| 183 | 183 | system('metaflac', '--remove-tag=TRACKTOTAL', "--set-tag=TOTALTRACKS=$files{$fn}{tracktotal}->[0]", $fn); | |
| 184 | 184 | } | |
| 185 | 185 | } | |
| 186 | 186 | ||
| 187 | 187 | sub discnum { | |
| 188 | 188 | my $fn = shift; | |
| 189 | 189 | my $n = shift; | |
| 190 | 190 | my $fc = shift; | |
| 191 | 191 | ||
| 192 | 192 | my $dn = dirname($fn); | |
| 193 | 193 | my $discnumber = $files{$fn}{discnumber}->[0]; | |
| 194 | 194 | my $album = $files{$fn}{album}->[0]; | |
| 195 | 195 | my ($match, $newfn); | |
| 196 | 196 | my $regex = '[[:punct:]]?(cd|disc) ?[0-9]+[[:punct:]]?$' ; | |
| 197 | 197 | ||
| 198 | 198 | if ($album =~ /$regex/i) { | |
| 199 | 199 | $album =~ s/.?${^MATCH}.?//; | |
| 200 | 200 | system('metaflac', '--remove-tag=ALBUM', $fn); | |
| 201 | 201 | system('metaflac', "--set-tag=ALBUM=$album", $fn); | |
| 202 | 202 | } | |
| 203 | 203 | ||
| 204 | 204 | if (!$discnumber) { | |
| 205 | 205 | if (basename($fn) =~ /^[0-9]+-/) { | |
| 206 | 206 | $match = ${^MATCH}; | |
| 207 | 207 | ${^MATCH} =~ /([0-9]+)/; | |
| 208 | - | $discnumber = $1; | |
| 208 | + | $discnumber = eval { $1 =~ s/^0+//; }; | |
| 210 | 209 | } | |
| 211 | 210 | elsif ($dn =~ /$regex/i) { | |
| 212 | 211 | $match = ${^MATCH}; | |
| 213 | 212 | ${^MATCH} =~ /([0-9]+)/; | |
| 214 | - | $discnumber = $1; | |
| 213 | + | $discnumber = eval { $1 =~ s/^0+//; }; | |
| 216 | 214 | ||
| 217 | 215 | $dn =~ s/ .?($match).?//; | |
| 218 | 216 | $newfn = "${dn}/" . "${discnumber}-" . basename($fn); | |
| 219 | 217 | make_path($dn) if (! -d $dn); | |
| 220 | 218 | move($fn, $newfn) or die "Couldn't rename '$fn': $!"; | |
| 221 | 219 | } | |
| 222 | 220 | else { | |
| 223 | 221 | $discnumber = 1; | |
| 224 | 222 | $newfn = "${dn}/" . "${discnumber}-" . basename($fn); | |
| 225 | 223 | move($fn, $newfn) or die "Couldn't rename '$fn': $!"; | |
| 226 | 224 | } | |
| 227 | 225 | ||
| 228 | 226 | if (!$newfn) { $newfn = $fn } | |
| 229 | 227 | say "$newfn: adding discnumber tag"; | |
| 230 | 228 | system('metaflac', "--set-tag=DISCNUMBER=$discnumber", $newfn); | |
| 231 | 229 | } | |
| 232 | 230 | } | |
| 233 | 231 | ||
| 234 | 232 | sub rmtag { | |
| 235 | 233 | my $fn = shift; | |
| 236 | 234 | foreach my $tag (@_) { | |
| 237 | 235 | if ($files{$fn}{$tag}) { | |
| 238 | 236 | say "$fn: removing $tag tag"; | |
| 239 | 237 | system('metaflac', "--remove-tag=$tag", $fn); | |
| 240 | 238 | } | |
| 241 | 239 | } | |
| 242 | 240 | } | |
| 243 | 241 | ||
| 244 | 242 |
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