pastebin - collaborative debugging tool
kpaste.net RSS


add tags script
Posted by Anonymous on Mon 10th Sep 2012 06:34
raw | new post

  1. #!/usr/bin/perl
  2.  
  3. use 5.14.0;
  4. use strict;
  5. use warnings;
  6.  
  7. use File::Spec qw(splitdir);
  8. use Cwd qw(abs_path);
  9. use File::Basename qw(basename);
  10.  
  11. if (!$ARGV[0]) { exit; }
  12.  
  13. my $arg = abs_path($ARGV[0]);
  14.  
  15. sub getdirs {
  16.  
  17.         my $dn = shift;
  18.         my @dirs;
  19.  
  20.         open(FIND, '-|', qq(find -L "$dn" -name "*" -type d))
  21.         or die "Can't run 'find': $!";
  22.         push(@dirs, (<FIND>));
  23.         chomp(@dirs);
  24.         close(FIND) or die "Can't close 'find': $!";
  25.         return(@dirs);
  26. }
  27.  
  28. sub getfiles {
  29.         my $dn = shift;
  30.         my @files;
  31.         opendir(my $dh, $dn) or die "Can't open directory '$dn': $!";
  32.         foreach (sort(readdir $dh)) {
  33.                 my $fn = "$dn/$_";
  34.                 if (/.mp3$/ && -f $fn) {
  35.                         push(@files, $fn);
  36.                 }
  37.         }
  38.         closedir $dh or die "Can't close directory '$dn': $!";
  39.         return @files;
  40. }
  41.  
  42. foreach my $dn (getdirs($arg)) {
  43.         my @dirs = File::Spec->splitdir( $dn );
  44.         my $el = scalar(@dirs);
  45.         my @tags;
  46.         my @write;
  47.         foreach (1..2) {
  48.                 push(@tags, $dirs[--$el]);
  49.         }
  50.         my @files = getfiles($dn);
  51.         foreach my $fn (@files) {
  52.                 my $temp = basename($fn);
  53.                 $temp =~ s/^1-0*//;
  54.                 $temp =~ s/.mp3$//;
  55.                 my @split = split('\. ', $temp);
  56.                 push(@tags, $split[0]);
  57.                 push(@tags, $split[1]);
  58.                 #@write = ( '--artist', "\"$tags[1]\"", '--album', "\"$tags[0]\"", '--song', "\"$split[1]\"", '--track', "\"$split[0]\"" );
  59.                 @write = ( "--artist=\"$tags[1]\"", "--album=\"$tags[0]\"", "--song=\"$split[1]\"", "--track=\"$split[0]\"" );
  60.                 system(qq{mid3v2 @write "$fn"});
  61.                 if ($? != 0) {
  62.                         say "Something went wrong with 'mid3v2': $!";
  63.                         exit;
  64.                 }
  65.         }
  66. }

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