Guest User

Untitled

a guest
Oct 21st, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. #!/opt/local/bin/perl
  2.  
  3. use 5.010;
  4. use strict;
  5. use warnings;
  6. use File::Find qw(find);
  7.  
  8. my $library = "/Volumes/Drobo/Backups/Music/Songbird Music";
  9. #my $library = "$ENV{HOME}/test";
  10. my %files;
  11.  
  12. find({ wanted => \&action, no_chdir => 1 }, $library);
  13.  
  14. sub action {
  15. if (-d) {
  16. my $dn = $File::Find::name;
  17. %files = getfiles($dn);
  18. my $fc = keys %files;
  19. # say $fc;
  20. unless ($fc == 0) {
  21. foreach my $fn (sort(keys %files)) {
  22. rmtag($fn, 'discnumber', 'disctotal', 'rating');
  23. replaygain($dn);
  24. albumartist($fn, $fc);
  25. track($fn);
  26. totaltracks($fn, $fc);
  27. # empty($fn);
  28. # duplicate($fn);
  29. }
  30. }
  31.  
  32.  
  33. undef %files;
  34. }
  35. }
  36. # 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).
  37. # This means it won't have to look up the tags again for the albumartist subroutine.
  38.  
  39. sub gettags {
  40. my $fn = quotemeta(shift);
  41. my %alltags;
  42. open(OUTPUT, '-|', qq{metaflac --no-utf8-convert --export-tags-to=- $fn}) || die "can\'t run metaflac: $!";
  43. local $_;
  44. while (<OUTPUT>) {
  45. chomp;
  46. my @tag = split('=');
  47. my $tagname = lc($tag[0]);
  48. if ($tag[1]) {
  49. push @{$alltags{$tagname}}, $tag[1];
  50. } else {
  51. push @{$alltags{$tagname}}, 'null';
  52. }
  53. }
  54. close(OUTPUT) || die "couldn\'t close metaflac: $!";
  55. return %alltags;
  56. }
  57.  
  58. sub getfiles {
  59. my $dn = shift;
  60. my %files;
  61. opendir(my $dh, $dn) or die "Can\'t open directory '$dn': $!";
  62. foreach (readdir $dh) {
  63. my $fn = "$dn/$_";
  64. if (/.flac$/ && -f $fn) {
  65. $files{$fn} = { gettags($fn) };
  66. }
  67. }
  68. closedir $dh or die "Can\'t close directory '$dn': $!";
  69. return %files;
  70. }
  71.  
  72. sub replaygain {
  73. my $dn = shift;
  74. my %replaygain;
  75.  
  76. foreach my $fn (sort(keys %files)) {
  77. if ($files{$fn}{replaygain_album_gain}) {
  78. $replaygain{$files{$fn}{replaygain_album_gain}->[0]}++;
  79. }
  80. }
  81. if (keys(%replaygain) != 1) {
  82. print "$dn: adding ReplayGain...";
  83. system('metaflac', '--add-replay-gain', keys(%files));
  84. say " done";
  85. }
  86. }
  87.  
  88. sub albumartist {
  89. my $fn = shift;
  90. my $tracks = shift;
  91. if (!$files{$fn}{albumartist}) {
  92. my $albumartist = 'Various Artists';
  93. my %artist;
  94. my $max;
  95. if ($tracks == 1) { $max = $tracks } else { $max = $tracks / 2 }
  96. foreach my $fn (keys %files) { $artist{$files{$fn}{artist}->[0]}++ }
  97. if (keys(%artist) <= $max) {
  98. my $ac = 0;
  99. foreach my $a (keys %artist) {
  100. if ($artist{$a} > $ac) {
  101. $ac = $artist{$a};
  102. $albumartist = $a;
  103. }
  104. }
  105. }
  106. say "$fn: adding albumartist tag";
  107. system('metaflac', "--set-tag=ALBUMARTIST=$albumartist", $fn);
  108. }
  109. }
  110.  
  111. sub empty {
  112. my $fn = shift;
  113. foreach my $k (keys %{$files{$fn}}) {
  114. foreach (@{$files{$fn}{$k}}) {
  115. if ($_ eq 'null') {
  116. say "$fn: WARNING: empty $k tag";
  117. }
  118. }
  119. }
  120. }
  121.  
  122. sub duplicate {
  123. my $fn = shift;
  124. my %exists;
  125. foreach my $k (keys %{$files{$fn}}) {
  126. foreach (@{$files{$fn}{$k}}) {
  127. my $tag = quotemeta($k . '=' . $_);
  128. if ($exists{$tag}) {
  129. say "$fn: WARNING: duplicate $k tag";
  130. } else {
  131. $exists{$tag} = 1;
  132. }
  133. }
  134. }
  135. }
  136.  
  137. sub track {
  138. my $fn = shift;
  139. my $track = $files{$fn}{tracknumber}->[0];
  140. if ($track =~ m/^0/ && $track != 0) {
  141. say "$fn: fixing tracknumber tag";
  142. $track =~ s/^0+//;
  143. system('metaflac', '--remove-tag=TRACKNUMBER', "--set-tag=TRACKNUMBER=$track", $fn);
  144. }
  145. }
  146.  
  147. sub totaltracks {
  148. my $fn = shift;
  149. my $tracks = shift;
  150. if (
  151. !$files{$fn}{totaltracks} &&
  152. !$files{$fn}{tracktotal}
  153. ) {
  154. say "$fn: adding totaltracks tag";
  155. system('metaflac', "--set-tag=TOTALTRACKS=$tracks", $fn);
  156. }
  157. elsif (
  158. $files{$fn}{tracktotal} &&
  159. !$files{$fn}{totaltracks}
  160. ) {
  161. say "$fn: replacing tracktotal tag with totaltracks";
  162. system('metaflac', '--remove-tag=TRACKTOTAL', "--set-tag=TOTALTRACKS=$files{$fn}{tracktotal}->[0]", $fn);
  163. }
  164. }
  165.  
  166. sub rmtag {
  167. my $fn = shift;
  168. foreach (@_) {
  169. if ($files{$fn}{$_}) {
  170. say "$fn: removing $_ tag";
  171. system('metaflac', "--remove-tag=$_", $fn);
  172. }
  173. }
  174. }
Add Comment
Please, Sign In to add comment