- #!/usr/bin/perl
- use 5.14.0;
- use strict;
- use warnings;
- #my $library = "/home/sir/Music/Songbird Music";
- my $library = "/home/sir/Music/Songbird Music";
- my (%t, %files, @files, @dirs, @wrong_case);
- my $flac_version = '1.2.1';
- getdirs($library);
- #my $ino = (stat($fn))[1];
- #say "$dn has $ino";
- getfiles($dn);
- my $fc = @files;
- if ($fc > 0) {
- replaygain($dn);
- for (my $n = 0; $n < $fc; $n++) {
- my $fn = $files[$n];
- $t{$tag} = $files{$fn}{$tag}->[0];
- #say "$tag = $t{$tag}";
- }
- vendor($fn);
- rmtag($fn, 'rating');
- albumartist($fn, $fc);
- track($fn);
- spaces($fn);
- totaltracks($fn, $fc);
- # empty($fn);
- duplicate($fn);
- discnum($fn, $n, $fc);
- }
- }
- }
- # 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).
- # This means it won't have to look up the tags again for the albumartist subroutine.
- sub gettags {
- my (%alltags, @mflac);
- foreach (@mflac) {
- my (@tag, $tagname);
- if (/^reference/) {
- $tagname = 'vendor_ref';
- $tag[1] = $tag[2];
- } else {
- }
- if ($tag[1]) {
- }
- }
- sub vendor {
- if ($t{vendor_ref} ne $flac_version) {
- say "$fn: old encoder ($t{vendor_ref}), re-encoding...";
- if ($? == 0) {
- } elsif ($? == 2) {
- say "Interrupted by user!";
- exit;
- }
- }
- }
- sub getdirs {
- }
- sub getfiles {
- my $fn;
- $fn = $dn . '/' . $_;
- if (/.flac$/ && -f $fn) {
- $files{$fn} = { gettags($fn) };
- }
- }
- }
- sub replaygain {
- my %replaygain;
- #foreach ('REFERENCE_LOUDNESS', 'TRACK_GAIN', 'TRACK_PEAK', 'ALBUM_GAIN', 'ALBUM_PEAK') {
- # my $tag = 'REPLAYGAIN_' . $_;
- # system('metaflac', '--remove-tag=' . $tag, keys(%files));
- #}
- if ($files{$fn}{replaygain_album_gain}) {
- $replaygain{$files{$fn}{replaygain_album_gain}->[0]}++;
- }
- }
- say " done";
- }
- }
- sub albumartist {
- if (! $t{albumartist} || $t{albumartist} eq 'null') {
- my %artist;
- my $max;
- if ($tracks == 1) { $max = $tracks } else { $max = $tracks / 2 }
- $artist{$t{artist}} = 1;
- }
- $t{albumartist} = 'Various Artists';
- } else { $t{albumartist} = $t{artist} };
- say "$fn: adding albumartist tag";
- }
- }
- sub empty {
- foreach (@{$files{$fn}{$tag}}) {
- if ($_ eq 'null') {
- say "$fn: WARNING: empty $tag tag";
- }
- }
- }
- }
- sub duplicate {
- foreach my $tag (@{$files{$fn}{$field}}) {
- if ($exists{$field} && $exists{$field} eq $tag) {
- say "$fn: removing duplicate $field tag";
- } else {
- $exists{$field} = $tag;
- }
- }
- }
- }
- sub track {
- if (!$t{tracknumber}) {
- say "$fn: doesn't have any tracknumber tag"
- }
- elsif ($t{tracknumber} =~ m/^0/ && $t{tracknumber} != 0) {
- say "$fn: fixing tracknumber tag";
- $t{tracknumber} =~ s/^0+//;
- }
- }
- sub spaces {
- my %temp;
- $temp{$tag} = $t{$tag};
- $temp{$tag} =~ s/(^\s*)|(\s*$)//g;
- }
- if ($temp{$tag} ne $t{$tag} && "$temp{$tag}") {
- say "$fn: removing leading and trailing whitespaces from tags";
- #say $temp{$tag};
- }
- }
- }
- sub totaltracks {
- if (
- !$t{totaltracks} &&
- !$t{tracktotal}
- ) {
- say "$fn: adding totaltracks tag";
- }
- elsif (
- $t{tracktotal} &&
- !$t{totaltracks}
- ) {
- say "$fn: replacing tracktotal tag with totaltracks";
- }
- }
- sub discnum {
- my $dn = dirname($fn);
- my ($match, $newfn);
- my $regex = '[[:punct:]]?(cd|disc) ?[0-9]+[[:punct:]]?$';
- if ($t{album} =~ /$regex/i) {
- $t{album} =~ s/.?(${^MATCH}).?//;
- }
- if (!$t{discnumber}) {
- if (basename($fn) =~ /^[0-9]+-/) {
- $match = ${^MATCH};
- ${^MATCH} =~ /([0-9]+)/;
- }
- elsif ($dn =~ /$regex/i) {
- $match = ${^MATCH};
- ${^MATCH} =~ /([0-9]+)/;
- $dn =~ s/ .?($match).?//;
- make_path($dn) if (! -d $dn);
- $newfn = "${dn}/" . $t{discnumber} . '-' . basename($fn);
- unless (-f $newfn) {
- }
- }
- else {
- $t{discnumber} = 1;
- $newfn = "${dn}/" . $t{discnumber} . '-' . basename($fn);
- unless (-f $newfn) {
- }
- }
- if (!$newfn) { $newfn = $fn }
- say "$newfn: adding discnumber tag";
- }
- }
- sub rmtag {
- foreach my $field (@_) {
- if ($t{$field}) {
- say "$fn: removing $field tag";
- }
- }
- }
replaygain, system() without using the shell
Posted by Anonymous on Mon 24th Sep 2012 00:54
raw | new post
modification of post by Anonymous (view diff)
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.