pastebin - collaborative debugging tool
kpaste.net RSS

Difference between
modified post 97c14d205 by Anonymous on Mon 24th Sep 2012 00:54
original post e7a4aed23 by Anonymous on Mon 24th Sep 2012 00:52
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::Copy qw(move);
77
use File::Basename qw(basename dirname);
88
use File::Path qw(make_path);
99
1010
#my $library = "/home/sir/Music/Songbird Music";
1111
my $library = "/home/sir/Music/Songbird Music";
1212
my (%t, %files, @files, @dirs, @wrong_case);
1313
my $flac_version = '1.2.1';
1414
1515
getdirs($library);
1616
1717
foreach my $dn (sort(@dirs)) {
1818
	#my $ino = (stat($fn))[1];
1919
	#say "$dn has $ino";
2020
	undef %files;
2121
	undef @files;
2222
	getfiles($dn);
2323
	my $fc = @files;
2424
	if ($fc > 0) {
2525
		replaygain($dn);
2626
		for (my $n = 0; $n < $fc; $n++) {
2727
			my $fn = $files[$n];
2828
			undef(%t);
2929
			foreach my $tag (keys($files{$fn})) {
3030
				$t{$tag} = $files{$fn}{$tag}->[0];
3131
				#say "$tag = $t{$tag}";
3232
			}
3333
			vendor($fn);
3434
			rmtag($fn, 'rating');
3535
			albumartist($fn, $fc);
3636
			track($fn);
3737
			spaces($fn);
3838
			totaltracks($fn, $fc);
3939
	#		empty($fn);
4040
			duplicate($fn);
4141
			discnum($fn, $n, $fc);
4242
		}
4343
	}
4444
}
4545
# 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).
4646
# This means it won't have to look up the tags again for the albumartist subroutine.
4747
4848
sub gettags {
4949
	my $fn = shift;
5050
	my (%alltags, @mflac);
5151
	open(OUTPUT, '-|', 'metaflac', '--no-utf8-convert', '--show-vendor-tag', '--export-tags-to=-', $fn) || die "can't run metaflac: $!";
5252
	chomp(@mflac = (<OUTPUT>));
5353
	foreach (@mflac) {
5454
		my (@tag, $tagname);
5555
		if (/^reference/) {
5656
			@tag = split(' ');
5757
			$tagname = 'vendor_ref';
5858
			$tag[1] = $tag[2];
5959
		} else {
6060
			@tag = split('=');
6161
			$tagname = lc($tag[0]) or next;
6262
		}
6363
6464
		if ($tag[1]) {
6565
			push(@{$alltags{$tagname}}, $tag[1]); }
6666
			else { push(@{$alltags{$tagname}}, 'null'); }
6767
	}
6868
	close(OUTPUT) || die "couldn't close metaflac: $!";
6969
	return %alltags;
7070
}
7171
7272
sub vendor {
7373
	my $fn = shift;
7474
7575
	if ($t{vendor_ref} ne $flac_version) {
7676
		my $newfn = $fn . '.' . int(rand(10000));
7777
		say "$fn: old encoder ($t{vendor_ref}), re-encoding...";
78-
		system('flac', '--silent', '-8', $fn, '--output-name=' . $newfn);
78+
		system('flac', '--silent', '-8', $fn, "--output-name=$newfn");
7979
		if ($? == 0) {
8080
			move($newfn, $fn) or die "Couldn't rename '$newfn': $!";
8181
		} elsif ($? == 2) {
8282
			say "Interrupted by user!";
8383
			unlink($newfn) if (-f $newfn);
8484
			exit;
8585
		}
8686
	}
8787
}
8888
8989
sub getdirs {
9090
9191
	my $dn = shift;
9292
9393
	open(FIND, '-|', 'find', $dn, '-name', '*', '-type', 'd')
9494
	or die "Can't run 'find': $!";
9595
	chomp(@dirs = (<FIND>));
9696
	close(FIND) or die "Can't close 'find': $!";
9797
}
9898
9999
sub getfiles {
100100
	my $dn = shift;
101101
	my $fn;
102102
	opendir(my $dh, $dn) or die "Can't open directory '$dn': $!";
103103
	foreach (readdir $dh) {
104104
		$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
}
112112
113113
sub replaygain {
114114
	my $dn = shift;
115115
	my %replaygain;
116116
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
	#}
121121
122122
	foreach my $fn (sort(keys %files)) {
123123
			if ($files{$fn}{replaygain_album_gain}) {
124124
				$replaygain{$files{$fn}{replaygain_album_gain}->[0]}++;
125125
			}
126126
	}
127127
128128
	if (keys(%replaygain) != 1) {
129129
		print "$dn: adding ReplayGain...";
130130
		system('metaflac', '--add-replay-gain', keys(%files));
131131
		say " done";
132132
	}
133133
}
134134
135135
sub albumartist {
136136
	my $fn = shift;
137137
	my $tracks = shift;
138138
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} };
149149
150150
		say "$fn: adding albumartist tag";
151151
		system('metaflac', '--remove-tag=ALBUMARTIST', "--set-tag=ALBUMARTIST=$t{albumartist}", $fn);
152152
	}
153153
}
154154
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
}
165165
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";
173173
					system('metaflac', "--remove-tag=${field}", "--set-tag=${field}=${tag}", $fn);
174174
				} else {
175175
					$exists{$field} = $tag;
176176
				}
177177
			}
178178
		}
179179
}
180180
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+//;
189189
		system('metaflac', '--remove-tag=TRACKNUMBER', "--set-tag=TRACKNUMBER=$t{tracknumber}", $fn);
190190
	}
191191
}
192192
193193
sub spaces {
194194
	my $fn = shift;
195195
	my %temp;
196196
197197
	foreach my $tag (sort(keys(%t))) {
198198
		$temp{$tag} = $t{$tag};
199199
		$temp{$tag} =~ s/(^\s*)|(\s*$)//g;
200200
	}
201201
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);
207207
			system('metaflac', "--remove-tag=$ufield", "--set-tag=${ufield}=${temp{$tag}}", $fn);
208208
		}
209209
	}
210210
}
211211
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";
220220
		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";
227227
		system('metaflac', '--remove-tag=TRACKTOTAL', "--set-tag=TOTALTRACKS=$t{tracktotal}", $fn);
228228
	}
229229
}
230230
231231
sub discnum {
232232
	my $fn = shift;
233233
	my $n = shift;
234234
	my $fc = shift;
235235
236236
	my $dn = dirname($fn);
237237
	my ($match, $newfn);
238238
	my $regex = '[[:punct:]]?(cd|disc) ?[0-9]+[[:punct:]]?$';
239239
240240
	if ($t{album} =~ /$regex/i) {
241241
		$t{album} =~ s/.?(${^MATCH}).?//;
242242
		system('metaflac', '--remove-tag=ALBUM', "--set-tag=ALBUM=$t{album}", $fn);
243243
	}
244244
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+//; };
255255
256256
			$dn =~ s/ .?($match).?//;
257257
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
		}
271271
272272
		if (!$newfn) { $newfn = $fn }
273273
		say "$newfn: adding discnumber tag";
274274
		system('metaflac', "--set-tag=DISCNUMBER=$t{discnumber}", $newfn);
275275
	}
276276
}
277277
278278
sub rmtag {
279279
	my $fn = shift;
280280
	foreach my $field (@_) {
281281
		if ($t{$field}) {
282282
			say "$fn: removing $field tag";
283283
			system('metaflac', "--remove-tag=${field}", $fn);
284284
		}
285285
	}
286286
}
287287

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