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