pastebin - collaborative debugging tool
kpaste.net RSS

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

    
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
1515
getdirs($library);
1616
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`);
1919
		if (-f $ls) {
2020
	#		my @stat = stat($dn);
2121
	#		my $ino = $stat[1];
2222
	#		say "$dn has $ino";
2323
			undef %files;
2424
			undef @files;
2525
			getfiles($dn);
2626
			my $fc = scalar(@files);
2727
			if ($fc > 0) {
28-
				replaygain($dn);
28+
				#replaygain($dn);
2929
				for (my $n = 0; $n < $fc; $n++) {
3030
					#say $files[$n];
3131
					my $fn = $files[$n];
3232
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 --export-tags-to=- $fn}) || die "can't run metaflac: $!";
5151
	chomp(@mflac = (<OUTPUT>));
5252
	foreach (@mflac) {
5353
		my @tag = split('=');
5454
		my $tagname = lc($tag[0]) or say $fn;
5555
		if ($tag[1]) {
5656
			push @{$alltags{$tagname}}, $tag[1];
5757
		} else {
5858
			push @{$alltags{$tagname}}, 'null';
5959
		}
6060
	}
6161
	close(OUTPUT) || die "couldn't close metaflac: $!";
6262
	return %alltags;
6363
}
6464
6565
sub getdirs {
6666
6767
	my $dn = shift;
6868
6969
	open(FIND, '-|', qq(find -L "$dn" -name "*" -type d))
7070
	or die "Can't run 'find': $!";
7171
	chomp(@dirs = (<FIND>));
7272
	close(FIND) or die "Can't close 'find': $!";
7373
	#@dirs = `find -L "$dn" -name "*" -type d -mindepth 1`;
7474
}
7575
7676
sub getfiles {
7777
	my $dn = shift;
7878
	opendir(my $dh, $dn) or die "Can't open directory '$dn': $!";
7979
	foreach (readdir $dh) {
8080
		my $fn = "$dn/$_";
8181
		if (/.flac$/ && -f $fn) {
8282
			push(@files, $fn);
8383
			$files{$fn} = { gettags($fn) };
8484
		}
8585
	}
8686
	closedir $dh or die "Can't close directory '$dn': $!";
8787
}
8888
8989
sub replaygain {
9090
        my $dn = shift;
9191
        my %replaygain;
9292
9393
	#foreach ('REFERENCE_LOUDNESS', 'TRACK_GAIN', 'TRACK_PEAK', 'ALBUM_GAIN', 'ALBUM_PEAK') {
9494
	#	my $tag = 'REPLAYGAIN_' . $_;
9595
	#	system('metaflac', "--remove-tag=$tag", keys(%files));
9696
	#}
9797
9898
        foreach my $fn (sort(keys %files)) {
9999
                if ($files{$fn}{replaygain_album_gain}) {
100100
                        $replaygain{$files{$fn}{replaygain_album_gain}->[0]}++;
101101
                }
102102
        }
103103
        if (keys(%replaygain) != 1) {
104104
                print "$dn: adding ReplayGain...";
105105
                system('metaflac', '--add-replay-gain', keys(%files));
106106
                say " done";
107107
        }
108108
}
109109
110110
sub albumartist {
111111
	my $fn = shift;
112112
	my $tracks = shift;
113113
114114
	if (!$files{$fn}{albumartist}) {
115115
116116
		my $albumartist = $files{$fn}{artist}->[0];
117117
		my %artist;
118118
		my $max;
119119
		if ($tracks == 1) { $max = $tracks } else { $max = $tracks / 2 }
120120
		foreach my $fn (keys %files) { $artist{$files{$fn}{artist}->[0]}++ }
121121
		if (keys(%artist) > $max) {
122122
			$albumartist = 'Various Artists';
123123
		}
124124
		say "$fn: adding albumartist tag";
125125
		system('metaflac', "--set-tag=ALBUMARTIST=$albumartist", $fn);
126126
	}
127127
}
128128
129129
sub empty {
130130
	my $fn = shift;
131131
	foreach my $k (keys %{$files{$fn}}) {
132132
		foreach (@{$files{$fn}{$k}}) {
133133
			if ($_ eq 'null') {
134134
				say "$fn: WARNING: empty $k tag";
135135
			}
136136
		}
137137
	}
138138
}
139139
140140
sub duplicate {
141141
	my $fn = shift;
142142
	my %exists;
143143
	foreach my $k (keys %{$files{$fn}}) {
144144
		foreach (@{$files{$fn}{$k}}) {
145145
			my $tag = quotemeta($k . '=' . $_);
146146
			if ($exists{$tag}) {
147147
				say "$fn: WARNING: duplicate $k tag";
148148
			} else {
149149
				$exists{$tag} = 1;
150150
			}
151151
		}
152152
	}
153153
}
154154
155155
sub track {
156156
	my $fn = shift;
157157
	my $track = $files{$fn}{tracknumber}->[0];
158158
	if (!$track) {
159159
		say "$fn: doesn't have any tracknumber tag"
160160
	}
161161
	elsif ($track =~ m/^0/ && $track != 0) {
162162
		say "$fn: fixing tracknumber tag";
163163
		$track =~ s/^0+//;
164164
		system('metaflac', '--remove-tag=TRACKNUMBER', "--set-tag=TRACKNUMBER=$track", $fn);
165165
	}
166166
}
167167
168168
sub totaltracks {
169169
	my $fn = shift;
170170
	my $tracks = shift;
171171
	if (
172172
	!$files{$fn}{totaltracks} &&
173173
	!$files{$fn}{tracktotal}
174174
	) {
175175
		say "$fn: adding totaltracks tag";
176176
		system('metaflac', "--set-tag=TOTALTRACKS=$tracks", $fn);
177177
	}
178178
	elsif (
179179
	$files{$fn}{tracktotal} &&
180180
	!$files{$fn}{totaltracks}
181181
	) {
182182
		say "$fn: replacing tracktotal tag with totaltracks";
183183
		system('metaflac', '--remove-tag=TRACKTOTAL', "--set-tag=TOTALTRACKS=$files{$fn}{tracktotal}->[0]", $fn);
184184
	}
185185
}
186186
187187
sub discnum {
188188
	my $fn = shift;
189189
	my $n = shift;
190190
	my $fc = shift;
191191
192192
	my $dn = dirname($fn);
193193
	my $discnumber = $files{$fn}{discnumber}->[0];
194194
	my $album = $files{$fn}{album}->[0];
195195
	my ($match, $newfn);
196196
	my $regex = '[[:punct:]]?(cd|disc) ?[0-9]+[[:punct:]]?$' ;
197197
198198
	if ($album =~ /$regex/i) {
199199
		$album =~ s/.?${^MATCH}.?//;
200200
		system('metaflac', '--remove-tag=ALBUM', $fn);
201201
		system('metaflac', "--set-tag=ALBUM=$album", $fn);
202202
	}
203203
204204
	if (!$discnumber) {
205205
		if (basename($fn) =~ /^[0-9]+-/) {
206206
			$match = ${^MATCH};
207207
			${^MATCH} =~ /([0-9]+)/;
208-
			$discnumber = $1;
208+
			$discnumber = eval { $1 =~ s/^0+//; };
210209
		}
211210
		elsif ($dn =~ /$regex/i) {
212211
			$match = ${^MATCH};
213212
			${^MATCH} =~ /([0-9]+)/;
214-
			$discnumber = $1;
213+
			$discnumber = eval { $1 =~ s/^0+//; };
216214
217215
			$dn =~ s/ .?($match).?//;
218216
			$newfn = "${dn}/" . "${discnumber}-" . basename($fn);
219217
			make_path($dn) if (! -d $dn);
220218
			move($fn, $newfn) or die "Couldn't rename '$fn': $!";
221219
		}
222220
		else {
223221
			$discnumber = 1;
224222
			$newfn = "${dn}/" . "${discnumber}-" . basename($fn);
225223
			move($fn, $newfn) or die "Couldn't rename '$fn': $!";
226224
		}
227225
228226
		if (!$newfn) { $newfn = $fn }
229227
		say "$newfn: adding discnumber tag";
230228
		system('metaflac', "--set-tag=DISCNUMBER=$discnumber", $newfn);
231229
	}
232230
}
233231
234232
sub rmtag {
235233
	my $fn = shift;
236234
	foreach my $tag (@_) {
237235
		if ($files{$fn}{$tag}) {
238236
			say "$fn: removing $tag tag";
239237
			system('metaflac', "--remove-tag=$tag", $fn);
240238
		}
241239
	}
242240
}
243241
244242

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