pastebin - collaborative debugging tool
kpaste.net RSS

Difference between
modified post e2bd75f8 by Admin on Sun 3rd Apr 2011 21:04
original post 0f1a00 by Admin on Thu 17th Mar 2011 15:11
Download diff
Show old version | new version | both versions

    
11
#!/opt/local/bin/perl
22
33
use 5.010;
44
use strict;
55
use warnings;
66
use File::Find qw(find);
77
88
my $library = "F:/Music";
99
#$library = "F:/FLAC/Downloaded/New";
1010
my %files;
1111
1212
find({ wanted => \&action, no_chdir => 1 }, $library);
1313
1414
sub action {
1515
	if (-d) {
1616
		my $dn = $File::Find::name;
1717
		%files = getfiles($dn);
1818
		my $fc = keys %files;
1919
#		say $fc;
2020
		unless ($fc == 0) {
21+
			replaygain($dn);
2122
			foreach my $fn (sort(keys %files)) {
2223
				rmtag($fn, 'discnumber', 'disctotal', 'rating', 'comment');
23-
				replaygain($fn);
2424
				albumartist($fn, $fc);
2525
				track($fn);
2626
				totaltracks($fn, $fc);
2727
#				empty($fn);
2828
#				duplicate($fn);
2929
			}
3030
		}
3131
3232
3333
		undef %files;
3434
	}
3535
}
3636
# 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).
3737
# This means it won't have to look up the tags again for the albumartist subroutine.
3838
3939
sub gettags {
4040
	my $fn = shift;
4141
	my %alltags;
4242
	open(OUTPUT, '-|', qq{metaflac --no-utf8-convert --export-tags-to=- "$fn"}) || die "can\'t run metaflac: $!";
4343
	local $_;
4444
	while (<OUTPUT>) {
4545
		chomp;
4646
		my @tag = split('=');
4747
		my $tagname = lc($tag[0]);
4848
		if ($tag[1]) {
4949
			push @{$alltags{$tagname}}, $tag[1];
5050
		} else {
5151
			push @{$alltags{$tagname}}, 'null';
5252
		}
5353
	}
5454
	close(OUTPUT) || die "couldn\'t close metaflac: $!";
5555
	return %alltags;
5656
}
5757
5858
sub getfiles {
5959
	my $dn = shift;
6060
	my %files;
6161
	opendir(my $dh, $dn) or die "Can\'t open directory '$dn': $!";
6262
	foreach (readdir $dh) {
6363
		my $fn = "$dn/$_";
6464
		if (/.flac$/ && -f $fn) {
6565
			$files{$fn} = { gettags($fn) };
6666
		}
6767
	}
6868
	closedir $dh or die "Can\'t close directory '$dn': $!";
6969
	return %files;
7070
}
7171
7272
sub replaygain {
73-
	my $fn = shift;
73+
	my $dn = shift;
74-
	unless ($files{$fn}{replaygain_reference_loudness}) {
74+
	my %replaygain;
7975
80-
sub albumartist {
76+
	foreach my $fn (sort(keys %files)) {
81-
	my $fn = shift;
77+
		if ($files{$fn}{replaygain_album_gain}) {
82-
	my $tracks = shift;
78+
			$replaygain{$files{$fn}{replaygain_album_gain}->[0]}++;
8979
		}
9280
		}
93-
		foreach my $fn (keys %files) {
81+
	if (keys(%replaygain) != 1) {
94-
			$artist{$files{$fn}{artist}->[0]}++
82+
		print "$dn: adding ReplayGain...";
83+
		system('metaflac', '--add-replay-gain', keys(%files));
9585
		}
10286
				}
87+

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