pastebin - collaborative debugging tool
kpaste.net RSS

Difference between
modified post 11ff6428 by Anonymous on Thu 6th Sep 2012 21:06
original post 1e by Anonymous on Thu 6th Sep 2012 09:53
Download diff
Show old version | new version | both versions

    
11
#!/usr/bin/perl
22
33
use 5.14.0;
44
use strict;
55
use warnings;
66
#use File::Find qw(finddepth);
77
use File::Copy qw(move);
88
use File::Basename qw(basename dirname);
99
use File::Path qw(make_path);
1010
1111
#my $library = "/home/sir/Music/Songbird Music";
1212
my $library = "/home/sir/Music/Songbird Music";
1313
my (%files, @files, @dirs);
1414
my $flac_version = '1.2.1';
1515
1616
getdirs($library);
1717
1818
foreach my $dn (sort(@dirs)) {
1919
		chomp(my $ls = `ls "$dn"/*.flac 2> /dev/null | head -n 1`);
2020
		if (-f $ls) {
2121
	#		my @stat = stat($dn);
2222
	#		my $ino = $stat[1];
2323
	#		say "$dn has $ino";
2424
			undef %files;
2525
			undef @files;
2626
			getfiles($dn);
2727
			my $fc = scalar(@files);
2828
			if ($fc > 0) {
2929
				#replaygain($dn);
3030
				for (my $n = 0; $n < $fc; $n++) {
3131
					my $fn = $files[$n];
3232
					vendor($fn);
3333
					rmtag($fn, 'rating');
3434
					albumartist($fn, $fc);
3535
					track($fn);
3636
					totaltracks($fn, $fc);
3737
	#				empty($fn);
3838
	#				duplicate($fn);
3939
					discnum($fn, $n, $fc);
4040
				}
4141
			}
4242
		}
4343
}
4444
# 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).
4545
# This means it won't have to look up the tags again for the albumartist subroutine.
4646
4747
sub gettags {
4848
	my $fn = quotemeta(shift);
4949
	my (%alltags, @mflac);
5050
	open(OUTPUT, '-|', qq{metaflac --no-utf8-convert --show-vendor-tag --export-tags-to=- $fn}) || die "can't run metaflac: $!";
5151
	chomp(@mflac = (<OUTPUT>));
5252
	foreach (@mflac) {
5353
		my (@tag, $tagname);
5454
		if (/^reference/) {
5555
			@tag = split(' ');
5656
			$tagname = 'vendor_ref';
5757
			$tag[1] = $tag[2];
5858
		} else {
5959
			@tag = split('=');
6060
			$tagname = lc($tag[0]) or say $fn;
6161
		}
6262
6363
		if ($tag[1]) {
6464
			push @{$alltags{$tagname}}, $tag[1];
6565
		} else {
6666
			push @{$alltags{$tagname}}, 'null';
6767
		}
6868
	}
6969
	close(OUTPUT) || die "couldn't close metaflac: $!";
7070
	return %alltags;
7171
}
7272
7373
sub vendor {
7474
	my $fn = shift;
7575
	my $vendor_ref = $files{$fn}{vendor_ref}->[0];
7676
7777
	if ($vendor_ref ne $flac_version) {
7878
		my $newfn = $fn . '.' . int(rand(10000));
7979
		say "$fn: old encoder ($vendor_ref), re-encoding...";
8080
		system('flac', '--silent', '-8', $fn, "--output-name=$newfn");
8181
		if ($? == 0) {
8282
			move($newfn, $fn) or die "Couldn't rename '$newfn': $!";
8383
		} elsif ($? == 2) {
8484
			say "Interrupted by user!";
8585
			unlink($newfn) if (-f $newfn);
8686
			exit;
8787
		}
8888
	}
8989
}
9090
9191
sub getdirs {
9292
9393
	my $dn = shift;
9494
9595
	open(FIND, '-|', qq(find -L "$dn" -name "*" -type d))
9696
	or die "Can't run 'find': $!";
9797
	chomp(@dirs = (<FIND>));
9898
	close(FIND) or die "Can't close 'find': $!";
9999
	#@dirs = `find -L "$dn" -name "*" -type d -mindepth 1`;
100100
}
101101
102102
sub getfiles {
103103
	my $dn = shift;
104104
	opendir(my $dh, $dn) or die "Can't open directory '$dn': $!";
105105
	foreach (readdir $dh) {
106106
		my $fn = "$dn/$_";
107107
		if (/.flac$/ && -f $fn) {
108108
			push(@files, $fn);
109109
			$files{$fn} = { gettags($fn) };
110110
		}
111111
	}
112112
	closedir $dh or die "Can't close directory '$dn': $!";
113113
}
114114
115115
sub replaygain {
116116
        my $dn = shift;
117117
        my %replaygain;
118118
119119
	#foreach ('REFERENCE_LOUDNESS', 'TRACK_GAIN', 'TRACK_PEAK', 'ALBUM_GAIN', 'ALBUM_PEAK') {
120120
	#	my $tag = 'REPLAYGAIN_' . $_;
121121
	#	system('metaflac', "--remove-tag=$tag", keys(%files));
122122
	#}
123123
124124
        foreach my $fn (sort(keys %files)) {
125125
                if ($files{$fn}{replaygain_album_gain}) {
126126
                        $replaygain{$files{$fn}{replaygain_album_gain}->[0]}++;
127127
                }
128128
        }
129129
        if (keys(%replaygain) != 1) {
130130
                print "$dn: adding ReplayGain...";
131131
                system('metaflac', '--add-replay-gain', keys(%files));
132132
                say " done";
133133
        }
134134
}
135135
136136
sub albumartist {
137137
	my $fn = shift;
138138
	my $tracks = shift;
139139
140140
	if (!$files{$fn}{albumartist}) {
141141
142142
		my $albumartist = $files{$fn}{artist}->[0];
143143
		my %artist;
144144
		my $max;
145145
		if ($tracks == 1) { $max = $tracks } else { $max = $tracks / 2 }
146146
		foreach my $fn (keys %files) { $artist{$files{$fn}{artist}->[0]}++ }
147147
		if (keys(%artist) > $max) {
148148
			$albumartist = 'Various Artists';
149149
		}
150150
		say "$fn: adding albumartist tag";
151151
		system('metaflac', "--set-tag=ALBUMARTIST=$albumartist", $fn);
152152
	}
153153
}
154154
155155
sub empty {
156156
	my $fn = shift;
157157
	foreach my $k (keys %{$files{$fn}}) {
158158
		foreach (@{$files{$fn}{$k}}) {
159159
			if ($_ eq 'null') {
160160
				say "$fn: WARNING: empty $k tag";
161161
			}
162162
		}
163163
	}
164164
}
165165
166166
sub duplicate {
167167
	my $fn = shift;
168168
	my %exists;
169169
	foreach my $k (keys %{$files{$fn}}) {
170170
		foreach (@{$files{$fn}{$k}}) {
171171
			my $tag = quotemeta($k . '=' . $_);
172172
			if ($exists{$tag}) {
173173
				say "$fn: WARNING: duplicate $k tag";
174174
			} else {
175175
				$exists{$tag} = 1;
176176
			}
177177
		}
178178
	}
179179
}
180180
181181
sub track {
182182
	my $fn = shift;
183183
	my $track = $files{$fn}{tracknumber}->[0];
184184
	if (!$track) {
185185
		say "$fn: doesn't have any tracknumber tag"
186186
	}
187187
	elsif ($track =~ m/^0/ && $track != 0) {
188188
		say "$fn: fixing tracknumber tag";
189189
		$track =~ s/^0+//;
190190
		system('metaflac', '--remove-tag=TRACKNUMBER', "--set-tag=TRACKNUMBER=$track", $fn);
191191
	}
192192
}
193193
194194
sub totaltracks {
195195
	my $fn = shift;
196196
	my $tracks = shift;
197197
	if (
198198
	!$files{$fn}{totaltracks} &&
199199
	!$files{$fn}{tracktotal}
200200
	) {
201201
		say "$fn: adding totaltracks tag";
202202
		system('metaflac', "--set-tag=TOTALTRACKS=$tracks", $fn);
203203
	}
204204
	elsif (
205205
	$files{$fn}{tracktotal} &&
206206
	!$files{$fn}{totaltracks}
207207
	) {
208208
		say "$fn: replacing tracktotal tag with totaltracks";
209209
		system('metaflac', '--remove-tag=TRACKTOTAL', "--set-tag=TOTALTRACKS=$files{$fn}{tracktotal}->[0]", $fn);
210210
	}
211211
}
212212
213213
sub discnum {
214214
	my $fn = shift;
215215
	my $n = shift;
216216
	my $fc = shift;
217217
218218
	my $dn = dirname($fn);
219219
	my $discnumber = $files{$fn}{discnumber}->[0];
220220
	my $album = $files{$fn}{album}->[0];
221221
	my ($match, $newfn);
222-
	my $regex = '[[:punct:]]?(cd|disc) ?[0-9]+[[:punct:]]?$' ;
222+
	my $regex = '[[:punct:]]?(cd|disc) ?[0-9]+[[:punct:]]?$';
223223
224224
	if ($album =~ /$regex/i) {
225-
		$album =~ s/.?${^MATCH}.?//;
225+
		$album =~ s/.?(${^MATCH}).?//;
226226
		system('metaflac', '--remove-tag=ALBUM', $fn);
227227
		system('metaflac', "--set-tag=ALBUM=$album", $fn);
228228
	}
229229
230230
	if (!$discnumber) {
231231
		if (basename($fn) =~ /^[0-9]+-/) {
232232
			$match = ${^MATCH};
233233
			${^MATCH} =~ /([0-9]+)/;
234234
			$discnumber = eval { $1 =~ s/^0+//; };
235235
		}
236236
		elsif ($dn =~ /$regex/i) {
237237
			$match = ${^MATCH};
238238
			${^MATCH} =~ /([0-9]+)/;
239239
			$discnumber = eval { $1 =~ s/^0+//; };
240240
241241
			$dn =~ s/ .?($match).?//;
242242
			$newfn = "${dn}/" . "${discnumber}-" . basename($fn);
243243
			make_path($dn) if (! -d $dn);
244244
			move($fn, $newfn) or die "Couldn't rename '$fn': $!";
245245
		}
246246
		else {
247247
			$discnumber = 1;
248248
			$newfn = "${dn}/" . "${discnumber}-" . basename($fn);
249249
			move($fn, $newfn) or die "Couldn't rename '$fn': $!";
250250
		}
251251
252252
		if (!$newfn) { $newfn = $fn }
253253
		say "$newfn: adding discnumber tag";
254254
		system('metaflac', "--set-tag=DISCNUMBER=$discnumber", $newfn);
255255
	}
256256
}
257257
258258
sub rmtag {
259259
	my $fn = shift;
260260
	foreach my $tag (@_) {
261261
		if ($files{$fn}{$tag}) {
262262
			say "$fn: removing $tag tag";
263263
			system('metaflac', "--remove-tag=$tag", $fn);
264264
		}
265265
	}
266266
}
267267
268268

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at