pastebin - collaborative debugging tool
kpaste.net RSS

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

    
11
#!/usr/bin/perl
33
use 5.14.0;
44
use strict;
55
use warnings;
6-
#use File::Find qw(finddepth);
76
use File::Copy qw(move);
87
use File::Basename qw(basename dirname);
98
use File::Path qw(make_path);
1110
#my $library = "/home/sir/Music/Songbird Music";
1211
my $library = "/home/sir/Music/Songbird Music";
1312
my (%t, %files, @files, @dirs);
1413
my $flac_version = '1.2.1';
1615
getdirs($library);
1817
foreach my $dn (sort(@dirs)) {
1918
	#my $ino = (stat($fn))[1];
2019
	#say "$dn has $ino";
2120
	undef %files;
2221
	undef @files;
2322
	getfiles($dn);
2423
	my $fc = @files;
2524
	if ($fc > 0) {
2625
		replaygain($dn);
2726
		for (my $n = 0; $n < $fc; $n++) {
2827
			my $fn = $files[$n];
2928
			undef(%t);
3029
			foreach my $tag (keys($files{$fn})) {
3130
				$t{$tag} = $files{$fn}{$tag}->[0];
3231
				#say "$tag = $t{$tag}";
3332
			}
3433
			vendor($fn);
3534
			rmtag($fn, 'rating');
3635
			albumartist($fn, $fc);
3736
			track($fn);
3837
			spaces($fn);
3938
			totaltracks($fn, $fc);
4039
	#		empty($fn);
4140
			duplicate($fn);
4241
			discnum($fn, $n, $fc);
4342
		}
4443
	}
4544
}
4645
# 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).
4746
# This means it won't have to look up the tags again for the albumartist subroutine.
4948
sub gettags {
50-
	my $fn = quotemeta(shift);
49+
	my $fn = shift;
5150
	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: $!";
5352
	chomp(@mflac = (<OUTPUT>));
5453
	foreach (@mflac) {
5554
		my (@tag, $tagname);
5655
		if (/^reference/) {
5756
			@tag = split(' ');
5857
			$tagname = 'vendor_ref';
5958
			$tag[1] = $tag[2];
6059
		} else {
6160
			@tag = split('=');
62-
			$tagname = lc($tag[0]) or say $fn;
61+
			$tagname = lc($tag[0]) or next;
6362
		}
6564
		if ($tag[1]) {
6665
			push(@{$alltags{$tagname}}, $tag[1]); }
6766
			else { push(@{$alltags{$tagname}}, 'null'); }
6867
	}
6968
	close(OUTPUT) || die "couldn't close metaflac: $!";
7069
	return %alltags;
7170
}
7372
sub vendor {
7473
	my $fn = shift;
7675
	if ($t{vendor_ref} ne $flac_version) {
7776
		my $newfn = $fn . '.' . int(rand(10000));
7877
		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");
8079
		if ($? == 0) {
8180
			move($newfn, $fn) or die "Couldn't rename '$newfn': $!";
8281
		} elsif ($? == 2) {
8382
			say "Interrupted by user!";
8483
			unlink($newfn) if (-f $newfn);
8584
			exit;
8685
		}
8786
	}
8887
}
9089
sub getdirs {
9291
	my $dn = shift;
9493
	open(FIND, '-|', qq(find -L "$dn" -name "*" -type d))
9594
	or die "Can't run 'find': $!";
9695
	chomp(@dirs = (<FIND>));
9796
	close(FIND) or die "Can't close 'find': $!";
9897
}
10099
sub getfiles {
101100
	my $dn = shift;
101+
	my $fn;
102102
	opendir(my $dh, $dn) or die "Can't open directory '$dn': $!";
103103
	foreach (readdir $dh) {
104-
		my $fn = "$dn/$_";
104+
		$fn = $dn . '/' . $_;
105105
		if (/.flac$/ && -f $fn) {
106106
			push(@files, $fn);
107107
			$files{$fn} = { gettags($fn) };
108108
		}
109109
	}
110110
	closedir $dh or die "Can't close directory '$dn': $!";
111111
}
113113
sub replaygain {
114114
	my $dn = shift;
115115
	my %replaygain;
117117
	#foreach ('REFERENCE_LOUDNESS', 'TRACK_GAIN', 'TRACK_PEAK', 'ALBUM_GAIN', 'ALBUM_PEAK') {
118118
	#	my $tag = 'REPLAYGAIN_' . $_;
119119
	#	system('metaflac', '--remove-tag=' . $tag, keys(%files));
120120
	#}
122122
	foreach my $fn (sort(keys %files)) {
123123
			if ($files{$fn}{replaygain_album_gain}) {
124124
				$replaygain{$files{$fn}{replaygain_album_gain}->[0]}++;
125125
			}
126126
	}
128128
	if (keys(%replaygain) != 1) {
129129
		print "$dn: adding ReplayGain...";
130130
		system('metaflac', '--add-replay-gain', keys(%files));
131131
		say " done";
132132
	}
133133
}
135135
sub albumartist {
136136
	my $fn = shift;
137137
	my $tracks = shift;
139139
	if (! $t{albumartist} || $t{albumartist} eq 'null') {
140140
		my %artist;
141141
		my $max;
142142
		if ($tracks == 1) { $max = $tracks } else { $max = $tracks / 2 }
143143
		foreach my $fn (sort(keys(%files))) {
144144
			$artist{$t{artist}} = 1;
145145
		}
146146
		if (keys(%artist) > $max) {
147147
			$t{albumartist} = 'Various Artists';
148148
		} else { $t{albumartist} = $t{artist} };
150150
		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");
152152
	}
153153
}
155155
sub empty {
156156
	my $fn = shift;
157157
	foreach my $tag (sort(keys(%{$files{$fn}}))) {
158158
		foreach (@{$files{$fn}{$tag}}) {
159159
			if ($_ eq 'null') {
160160
				say "$fn: WARNING: empty $tag tag";
161161
			}
162162
		}
163163
	}
164164
}
166166
sub duplicate {
167167
		my $fn = shift;
168168
		my %exists;
169169
		foreach my $field (sort(keys(%{$files{$fn}}))) {
170170
			foreach my $tag (@{$files{$fn}{$field}}) {
171171
				if ($exists{$field} && $exists{$field} eq $tag) {
172172
					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");
174174
				} else {
175175
					$exists{$field} = $tag;
176176
				}
177177
			}
178178
		}
179179
}
181181
sub track {
182182
	my $fn = shift;
183183
	if (!$t{tracknumber}) {
184184
		say "$fn: doesn't have any tracknumber tag"
185185
	}
186186
	elsif ($t{tracknumber} =~ m/^0/ && $t{tracknumber} != 0) {
187187
		say "$fn: fixing tracknumber tag";
188188
		$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");
190190
	}
191191
}
193193
sub spaces {
194194
	my $fn = shift;
195195
	my %temp;
197197
	foreach my $tag (sort(keys(%t))) {
198198
		$temp{$tag} = $t{$tag};
199199
		$temp{$tag} =~ s/(^\s*)|(\s*$)//g;
200200
	}
202202
	foreach my $tag (sort(keys(%t))) {
203203
		if ($temp{$tag} ne $t{$tag} && "$temp{$tag}") {
204204
			say "$fn: removing leading and trailing whitespaces from tags";
205205
			#say $temp{$tag};
206206
			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");
208208
		}
209209
	}
210210
}
212212
sub totaltracks {
213213
	my $fn = shift;
214214
	my $tracks = shift;
215215
	if (
216216
	!$t{totaltracks} &&
217217
	!$t{tracktotal}
218218
	) {
219219
		say "$fn: adding totaltracks tag";
220-
		system('metaflac', '--set-tag=TOTALTRACKS=' . $tracks, $fn);
220+
		system('metaflac', '--set-tag=TOTALTRACKS=' . $tracks, "$fn");
221221
	}
222222
	elsif (
223223
	$t{tracktotal} &&
224224
	!$t{totaltracks}
225225
	) {
226226
		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");
228228
	}
229229
}
231231
sub discnum {
232232
	my $fn = shift;
233233
	my $n = shift;
234234
	my $fc = shift;
236236
	my $dn = dirname($fn);
237237
	my ($match, $newfn);
238238
	my $regex = '[[:punct:]]?(cd|disc) ?[0-9]+[[:punct:]]?$';
240240
	if ($t{album} =~ /$regex/i) {
241241
		$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");
243243
	}
245245
	if (!$t{discnumber}) {
246246
		if (basename($fn) =~ /^[0-9]+-/) {
247247
			$match = ${^MATCH};
248248
			${^MATCH} =~ /([0-9]+)/;
249249
			$t{discnumber} = eval { $1 =~ s/^0+//; };
250250
		}
251251
		elsif ($dn =~ /$regex/i) {
252252
			$match = ${^MATCH};
253253
			${^MATCH} =~ /([0-9]+)/;
254254
			$t{discnumber} = eval { $1 =~ s/^0+//; };
256256
			$dn =~ s/ .?($match).?//;
258258
			make_path($dn) if (! -d $dn);
259259
			$newfn = "${dn}/" . $t{discnumber} . '-' . basename($fn);
260260
			unless (-f $newfn) {
261261
				move($fn, $newfn) or die "Couldn't rename '$fn': $!";
262262
			}
263263
		}
264264
		else {
265265
			$t{discnumber} = 1;
266266
			$newfn = "${dn}/" . $t{discnumber} . '-' . basename($fn);
267267
			unless (-f $newfn) {
268268
				move($fn, $newfn) or die "Couldn't rename '$fn': $!";
269269
			}
270270
		}
272272
		if (!$newfn) { $newfn = $fn }
273273
		say "$newfn: adding discnumber tag";
274-
		system('metaflac', '--set-tag=DISCNUMBER=' . $t{discnumber}, $newfn);
274+
		system('metaflac', '--set-tag=DISCNUMBER=' . $t{discnumber}, "$newfn");
275275
	}
276276
}
278278
sub rmtag {
279279
	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");
284284
		}
285285
	}
286286
}

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