php: Log Library Java: Get PID perl: id3 tag snippets

Perl ID3 tagging

#! /usr/bin/perl -w

use strict;
use Cwd;

########################################
# writes ID3v1/v2-Tags to all mp3-Tracks
# using
#    id3 and id3v2  from Robert Woodcock
######
# Author: Daniel Golesny
# Homepage: www.golesny.de
######
# Tested only on GNU/Linux Woody and Sarge for i386
########################################

# ToDo ############################################
#
# Changes #########################################
# 2003-03-09
# files are renamed to: first char of every word is
#                       uppercased
# 2003-03-02
# - Helptext `Usage ....' fixed
# 2002-12-25
# - spaces around a trackname will not occur in the
#   id3-Tag
# 2002-12-24
# - RegExpr for the Track-Titel
# - add Option --help
# 2002-12-02
# - Add the Help-text
# - Use the current Dir from where the Skript is
#   called
###################################################

my $uppercasewords = 1; # 0 or 1
my $helptext = "
Usage:
perl writeid3.pl --depth=

Options:
--depth     Depth of useless Directories
            Example for --depth=2:
            - Current Directory
             '- useless            (e.g. \"Metal\")
               |- useless          (e.g. \"M\")
               | '- BAND           (e.g. \"Manowar\")
               |   '- [YYYY] Album
               |     |- [-][Track-No] Track1
               |     '- [-][Track-No] Track2
               '- useless
                 '- ...

--help      This Screen

*********************************************
 The Skript uses id3 and id3v2
 The User must have access to both programs
*********************************************
\n";


my $cwddir = cwd;
my $uldepth = 0;

@ARGV or die $helptext;

foreach my $x (@ARGV)
{
  if ($x =~ /^--depth=([0-9]+$)/)
  {
    $uldepth = $1;
  }
  if ($x =~ /^--help$/)
  {
    print $helptext;exit;
  }
}

print "Useless Depth = $uldepth\n---------\n";

readMP3Dir($cwddir, 0, $uldepth);

print "\nDone.\n";


##################################################
# Reads a directory (rekursiv)
# ####### Paramterlist #####################
# path     = dir to read
# level    = depth of directory
# uldepth  = useless depth
##################################################

sub readMP3Dir
{
  # Parameter
  my @params = @_;
  my $path = $params[0];
  my $level = $params[1];

  # Read Directory
  if (-d $path) {
    print "Directory $path\n";
    opendir(DIR, $path)||die "Error($path): $!\n";
    my @dir = readdir(DIR);
    closedir(DIR);
    @dir = sort(@dir);
    #
    foreach my $d (@dir) {
      if (substr($d,0,1) ne ".") {
        my $type = $uldepth - $level;
        if ($type > 0) { 
          # useless Directory
          readMP3Dir($path."/".$d,
                     $level+1);
        } elsif ($type == 0) {
          # BAND
          readMP3Dir($path."/".$d,
                     $level+1,
                     $d);
        } elsif ($type == -1) {
          # [YYYY] ALBUM
          my $banddir = $params[2];
          readMP3Dir($path."/".$d,
                     $level+1,
                     $banddir,
                     $d);
        } elsif ($type == -2) {
          # [-][] Track-Titel
          my $band = $params[2];
          my $albumdir = $params[3];
          my $albumname = $albumdir;
          my $albumyear = "0000";
          # Albumdir
          if ($albumdir =~ /^([0-9]{4}) (.*)/) {
            $albumname = $2;
            $albumyear = $1;
          }
          # Track analysieren (nur mp3)
          if ($d =~ /(.*)(\.mp3)$/) {
            my $cdnr = "";
            my $tracknr = 0;
            my $trackname = $1;
            my $ext = $2;
            # rename Track `01 abc cde.mp'
            #       --> `01 Abc Cde.mp3'
            if ($uppercasewords == 1) {
              my @newtracksplit = \
                           split(/ /,$trackname);
              my $newtrackname = '';
              my $trenn = '';
              foreach (@newtracksplit) {
                $newtrackname .= $trenn.ucfirst($_);
                $trenn = ' ';
              }
              if ($d ne "$newtrackname$ext") {
                # rename
                print "\tRENAME TO '$newtrackname$ext' ... ";
                qx(mv "$path/$trackname$ext" "$path/$newtrackname$ext");
                if (-f "$path/$newtrackname$ext")
                {
                  $trackname = $newtrackname;
                  $d = $newtrackname.$ext;
                  print "done.\n";
                } else {
                  print "FAILED.\n";
                }
              }
            }
            if ($trackname =~ /^([0-9]{2})\s+(.*?)\s*$/) {
              $tracknr = $1;
              $trackname = $2;
            }
            if ($trackname =~ /^([0-9]{1})-([0-9]{2})\s+(.*?)\s*$/) {
              $cdnr = " (CD $1)";
              $tracknr = $2;
              $trackname = $3;
            }
            print "\t$band\t$albumname$cdnr\t$tracknr\t$trackname\t$albumyear\n";
            # deletes existend id3-Tags
            qx(id3v2 -D "$path/$d");
            # write id3v1 Tag
            #qx(id3 -t "$trackname" -A "$albumname$cdnr" -a "$band" -y "$albumyear" "$path/$d");
            # write id3v2 Tag
            print qx(id3v2 -t "$trackname" -T $tracknr -A "$albumname$cdnr" -a "$band" -y "$albumyear" "$path/$d");
          }
        }
      }
    }
  }
}