#!/usr/bin/perl # # Version 0.2 Jun 2009 # # Author Stephen Kingham, Stephen.Kingham@kingtech.com.au # # https://www.kingtech.com.au # # License and distributed under the GNU General Public License (GPL) # # Copyright (C) 2009 Stephen Kingham # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 3 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # This programme is just a wrapper for # lame, # flac, and # mplayer # # Versions # 0.1 May 2009 Can convert .wav, .mp3, and .flac, to mp3. # It also copies all the other files over to the specified # destination and recreate the directory structure. If Folder.jpg # does not exist it will create one by copying from an available .jpg file. # 0.2 Jun 2009 Uses mplayer to convert .wma audio files. # #use warnings; #use diagnostics; #use strict; use File::Find; use File::Basename; # use Cwd; my $RootDir = getcwd; my $debug="off"; my $srcDirAbs=$RootDir . "/" . "music-raw"; # # Adsolute root Directory to source all the music from # my $srcFileRel=" "; # this is the source file less the root path, ie relative name my $srcFileAbs=" "; # Absulte filename of the file we are processing my $srcFileExtension=" "; # Just the extension of the filename, eg .wav my $srcFilePathAbs=" "; # the full path of the source file, ie without the filename my $srcFilename=" "; # just the filname of the source file without extension or path my $srcFileDirRel=" "; # the path to the file being processed less the root part of the directory # my $dstDirAbs=$RootDir . "/" . "music-mp3"; # # Absolute root directory to put all the mp3 files # my $dstFileAbs=" "; # Absulte filename of the file we are processing my $dstFileMP3Abs=" "; # Destination filename with the extension changed to .mp3 my $dstFileWAVAbs=""; # Destination filename with .wav extension as intermediate to converting to mp3 my $dstFileImage=""; # my @Args=(); # my $Overwrite = "No"; my $Owner = "www-data"; my $Group = "www-data"; my $bitRate = 192; # my $argnum = 0; my @paths = (); my $pathPart=""; my $i=0; my $albumName=""; my $artistName=""; my $Silent="No"; # foreach $argnum (0 .. $#ARGV) { # if ( $ARGV[$argnum] eq "-f" ) { $Overwrite="yes" }; if ( $ARGV[$argnum] eq "debug" ) { $debug="on" }; if ( $ARGV[$argnum] eq "-o" ) { $Owner=$ARGV[$argnum+1] }; if ( $ARGV[$argnum] eq "-g" ) { $Group=$ARGV[$argnum+1] }; if ( $ARGV[$argnum] eq "-s" ) { $srcDirAbs=$ARGV[$argnum+1] }; if ( $ARGV[$argnum] eq "-d" ) { $dstDirAbs=$ARGV[$argnum+1] }; if ( $ARGV[$argnum] eq "-b" ) { $bitRate=$ARGV[$argnum+1] }; if ( $ARGV[$argnum] eq "-S" ) { $Silent="Yes" }; if ( $ARGV[$argnum] eq "-h" || $ARGV[$argnum] eq "-help" ) { print "Copyright (C) 2009 Stephen Kingham\n"; print "\n"; print "This program is free software; you can redistribute it and/or\n"; print "modify it under the terms of the GNU General Public License\n"; print "as published by the Free Software Foundation; either version 3\n"; print "of the License, or (at your option) any later version.\n"; print "\n"; print "This program is distributed in the hope that it will be useful,\n"; print "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"; print "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"; print "GNU General Public License for more details.\n"; print "\n"; print "Version 0.1 Jun 2009\n\n"; print "usage [-f] [-o OWNER] [-g GROUP] [-s SOURE-DIR] [-d DESTINATION DIR] [-b BITRATE] [-S]\n"; print " -f overwrite all files in the destination [do not overwrite]\n"; print " -o OWNER change the owner of the files/directories to OWNER, [www-data]\n"; print " -g GROUP change the group of the files/directories to GROUP, [www-data]\n"; print " -s DIR The root directory to find all the source audio files [./music-raw]\n"; print " -d DIR The destination root directory to put the converted audio files ./music-mp3]\n"; print " -b BITRATE What bit rate do you want the mp3, [192]\n"; print " -S Run silently, otherwise advise each action [do not run silent]\n"; print "Uses LAME (http://www.mp3dev.org/)\n"; print "mplayer (http://www.mplayerhq.hu/design7/news.html)\n"; print "and FLAC (http://flac.sourceforge.net)\n"; exit 0; } # } # # if ($debug eq "on") { print "DEBUG:\n -f=$Overwrite\n debug=$debug\n -o=$Owner\n -g=$Group\n -s=$srcDirAbs\n -d=$dstDirAbs\n -b=$bitRate\n" }; # find will find every file, we start find sub { $srcFileAbs = $File::Find::name; if ($debug eq "on") { print "DEBUG: Start of a new file\n\nsrc file Abs=$srcFileAbs\n" }; # Split the source file to find the relative path/filename of the src file # Then turn that into the absulute destination filename (undef, $srcFileRel) = split ( $srcDirAbs, $srcFileAbs ); $dstFileAbs = $dstDirAbs . "/" . $srcFileRel; $dstFileAbs =~ s/\/\//\//g; if ($debug eq "on") { print "dst file Abs=$dstFileAbs\n" }; if ( -d $srcFileAbs ) { # So $srcFileAbs is a directory, we need to create the destination directory if ($debug eq "on") { print "Directory\n" }; # # create the directory in the new location if it does not already exist # if ( -d "$dstFileAbs" ) { if ($debug eq "on") { print "destination directory already exists.\n" }; } else { @Args = ("mkdir", $dstFileAbs); system(@Args) == 0 or die "ERROR: system @Args failed: $?\n"; if ($? == -1) { print "ERROR: failed to execute @Args\n $!\n"; } elsif ($? & 127) { printf "ERROR: @Args\n child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; exit 0; } else { if ($debug eq "on") { printf "@Args\n child exited with value %d\n", $? >> 8 }; } if ($debug eq "on") { print "Created new destination directory\n" }; }; } else { # So it is a file, we need to copy the file to the new location and # convert any audio files to mp3's if ($debug eq "on") { print "File\n" }; # # Find the file's extension ($srcFilename, $srcFilePathAbs, $srcFileExtension) = fileparse( $srcFileAbs, qr/\.[^.]*/); if ($debug eq "on") { print "srcPath=$srcFilePathAbs , Filename=$srcFilename, extension=$srcFileExtension\n" }; # if ( $srcFileExtension eq ".wav" || $srcFileExtension eq ".WAV" || $srcFileExtension eq ".wma" || $srcFileExtension eq ".WMA" || $srcFileExtension eq ".m4a" || $srcFileExtension eq ".M4A" || $srcFileExtension eq ".mp4" || $srcFileExtension eq ".MP4" || $srcFileExtension eq ".mp3" || $srcFileExtension eq ".MP3" || $srcFileExtension eq ".flac" || $srcFileExtension eq ".FLAC" ) { # We found an audio file to convert if ($debug eq "on") { print "File is an audio file.\n" }; (undef, $srcFileDirRel) = split ( $srcDirAbs, $srcFilePathAbs ); # $dstFileMP3Abs = $dstDirAbs . "/" . $srcFileDirRel . "/" . $srcFilename . ".mp3"; $dstFileMP3Abs =~ s/\/\//\//g; # check if the mp3 file already exists and proceed only if user wants it overwritten if ( -f $dstFileMP3Abs && $Overwrite ne "yes" ) { if ($debug eq "on") { print "File already exists and we are going to leave it -f=$Overwrite.\n" }; } else { # Lets work out the Album and Artist name from the names of the directories # @paths = split(/\//, $srcFileDirRel); if ($debug eq "on") { print "srcFileDirRel=$srcFileDirRel all=@paths\n" }; $albumName=""; $artistName=""; $i=0; foreach $pathPart ( @paths ) { if ($debug eq "on") { print "i=$i path = $paths[$i] pathPart=$pathPart\n" }; if ($i > 0 ) { $albumName=$paths[$i]; # replace -- with - $albumName =~ s/--/-/g; $albumName =~ s/--/-/g; $albumName =~ s/--/-/g; $artistName=$paths[$i-1]; $artistName =~ s/--/-/g; $artistName =~ s/--/-/g; $artistName =~ s/--/-/g; } $i=$i+1; } if ($debug eq "on") { print "albumName=$albumName artistName=$artistName\n" }; # If the audio filw is an m4a or mp4 then we first convert it to a wav file # if ( $srcFileExtension eq ".m4a" || $srcFileExtension eq ".M4A" || $srcFileExtension eq ".mp4" || $srcFileExtension eq ".MP4" ) { # Need to a new destination filename with .wav $dstFileWAVAbs = $dstDirAbs . "/" . $srcFileDirRel . "/" . $srcFilename . ".wav"; $dstFileWAVAbs =~ s/\/\//\//g; @Args = ("faad", $srcFileAbs, "-o", $dstFileWAVAbs, "-q" ); if ($debug eq "on") {print "Going to @Args\n"}; if ($Silent eq "No") { print "Creating $dstFileWAVAbs intermediate step to make mp3\n" }; system(@Args) == 0 or die "ERROR: system @Args failed: $?\n"; if ($? == -1) { print "ERROR: failed to execute @Args\n $!\n"; } elsif ($? & 127) { printf "ERROR: @Args\n child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; exit 0; } else { if ($debug eq "on") { printf "@Args\n child exited with value %d\n", $? >> 8 }; } $srcFileAbs = $dstFileWAVAbs; if ($debug eq "on") { print "New srcFileAbs=$srcFileAbs\n" }; } if ( $srcFileExtension eq ".flac" || $srcFileExtension eq ".FLAC" ) { # If .flac we first need to convert it to a .wav file # Need to a new destination filename with .wav $dstFileWAVAbs = $dstDirAbs . "/" . $srcFileDirRel . "/" . $srcFilename . ".wav"; $dstFileWAVAbs =~ s/\/\//\//g; # -d decode # -f overwrite destination file # -s run silently # -o write ouput to file @Args = ("flac", "-d", "-f", "-s", "-o", $dstFileWAVAbs, $srcFileAbs ); if ($debug eq "on") {print "Going to @Args\n"}; if ($Silent eq "No") { print "Creating $dstFileWAVAbs intermediate step to make mp3\n" }; system(@Args) == 0 or die "ERROR: system @Args failed: $?\n"; if ($? == -1) { print "ERROR: failed to execute @Args\n $!\n"; } elsif ($? & 127) { printf "ERROR: @Args\n child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; exit 0; } else { if ($debug eq "on") { printf "@Args\n child exited with value %d\n", $? >> 8 }; } $srcFileAbs = $dstFileWAVAbs; if ($debug eq "on") { print "New srcFileAbs=$srcFileAbs\n" }; } # If audio file is .wma then we first have to convert to .wav # if ( $srcFileExtension eq ".wma" || $srcFileExtension eq ".WMA" ) { # If .wma we first need to convert it to a .wav file # Need to a new destination filename with .wav $dstFileWAVAbs = $dstDirAbs . "/" . $srcFileDirRel . "/" . $srcFilename . ".wav"; $dstFileWAVAbs =~ s/\/\//\//g; $dstFileWAVAbs =~ s/\,//g; # mplayer needs the w32codecs libdvdcss2 installed, using apt-get follow these instructions: # sudo wget http://www.medibuntu.org/sources.list.d/intrepid.list --output-document=/etc/apt/sources.list.d/medibuntu.list # sudo apt-get update # sudo apt-get install medibuntu-keyring # sudo apt-get update # sudo apt-get install w32codecs libdvdcss2 # # mplayer arguments # -vo null = no video output # -vc dummy = no video codec # -af resample=44100 = set audio filter to typical CD quality # -ao pcm:waveheader = Audio Driver is PCM with the waveheader # file= = write the output to filename # -nolirc Turns off LIRC support. # -really-quiet = run without any output to shell # look at using -lavcopts acodec=wmav1 wmav1 or wmav2 @Args = ("mplayer", "-nolirc", "-really-quiet", "-vo", "null", "-vc", "null", "-af", "resample=44100", "-ao", "pcm:waveheader:file=$dstFileWAVAbs", "$srcFileAbs" ); if ($debug eq "on") {@Args = ("mplayer", "-nolirc", "-vo", "null", "-vc", "null", "-af", "resample=44100", "-ao", "pcm:waveheader:file=$dstFileWAVAbs", "$srcFileAbs" ) }; if ($debug eq "on") {print "DEBUG: Going to @Args\n"}; if ($Silent eq "No") { print "Creating $dstFileWAVAbs intermediate step to make mp3\n" }; system(@Args) == 0 or die "ERROR: system @Args failed: $?\n"; if ($? == -1) { print "ERROR: failed to execute @Args\n $!\n"; } elsif ($? & 127) { printf "ERROR: @Args\n child died with signal %d, %s\n", ($? & 127), ($? & 128) ? 'with' : 'without'; exit 0; } else { if ($debug eq "on") { printf "@Args\n child exited with value %d\n", $? >> 8 }; } $srcFileAbs = $dstFileWAVAbs; if ($debug eq "on") { print "New srcFileAbs=$srcFileAbs\n" }; } # Now we use LAME to convert to mp3 # # -S run silent # -h highest quality # -b bit rate # (default) joint stereo if ( $artistName eq "" || $albumName eq "" ) { @Args = ("lame", "-S", "-h", "--tt", $srcFilename, "-b", $bitRate, $srcFileAbs, $dstFileMP3Abs); } else { @Args = ( "lame", "-S", "-h", "--ta", $artistName, "--tl", $albumName, "--tt", $srcFilename, "-b" , $bitRate, $srcFileAbs, $dstFileMP3Abs); } if ($debug eq "on") {print "Going to @Args\n"}; if ($Silent eq "No") { print "Creating $dstFileMP3Abs\n" }; system(@Args) == 0 or die "ERROR: system @Args failed: $?\n"; if ($? == -1) { print "ERROR: failed to execute @Args\n $!\n"; } elsif ($? & 127) { printf "ERROR: @Args\n child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; exit 0; } else { if ($debug eq "on") { printf "@Args\n child exited with value %d\n", $? >> 8 }; } if ($debug eq "on") { print "Converted file -f=$Overwrite.\n" }; # }; # So that changeing the owner and permissions work: $dstFileAbs = $dstFileMP3Abs; if ( -e $dstFileWAVAbs ) { @Args = ( "rm", $dstFileWAVAbs); system(@Args) == 0 or die "ERROR: system @Args failed: $?\n"; if ($? == -1) { print "ERROR: failed to execute @Args\n $!\n"; } elsif ($? & 127) { printf "ERROR: @Args\n child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; exit 0; } else { if ($debug eq "on") { printf "@Args\n child exited with value %d\n", $? >> 8 }; } } # } else { # This is a not an audio file, # check if the file already exists and proceed only if user wants it ovwrwritten # if ( $srcFileExtension eq ".jpg" || $srcFileExtension eq ".jpeg" || $srcFileExtension eq ".jpeg" || $srcFileExtension eq ".JPEG" ) { $dstFileImage = $dstDirAbs . "/" . $srcFileDirRel . "/" . "Folder.jpg"; $dstFileImage =~ s/\/\//\//g; if ( (! -e $dstFileImage) || ($Overwrite eq "Yes") ) { # Folder.jpg does not already exist so lets make one @Args = ("cp", "-f", $srcFileAbs, $dstFileImage); system(@Args) == 0 or die "ERROR: system @Args failed: $?\n"; if ($? == -1) { print "ERROR: failed to execute @Args\n $!\n"; } elsif ($? & 127) { printf "ERROR: @Args\n child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; exit 0; } else { if ($debug eq "on") { printf "@Args\n child exited with value %d\n", $? >> 8 }; } if ($debug eq "on") { print "Copied the file from $srcFileAbs $dstFileImage -f=$Overwrite.\n" }; if ($Silent eq "No") { print "Made $dstFileImage from $srcFileAbs\n" }; } # if ( $albumName ne "" ) { $dstFileImage = $dstDirAbs . "/" . $srcFileDirRel . "/" . $albumName . ".jpg"; $dstFileImage =~ s/\/\//\//g; if ( (! -e $dstFileImage) || ($Overwrite eq "Yes") ) { # "Album".jpg does not already exist so lets make one @Args = ("cp", "-f", $srcFileAbs, $dstFileImage); system(@Args) == 0 or die "ERROR: system @Args failed: $?\n"; if ($? == -1) { print "ERROR: failed to execute @Args\n $!\n"; } elsif ($? & 127) { printf "ERROR: @Args\n child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; exit 0; } else { if ($debug eq "on") { printf "@Args\n child exited with value %d\n", $? >> 8 }; } if ($debug eq "on") { print "Copied the file from $srcFileAbs $dstFileImage -f=$Overwrite.\n" }; if ($Silent eq "No") { print "Made $dstFileImage from $srcFileAbs\n" }; } } } if ( (-e $dstFileAbs) && ($Overwrite ne "yes") ) { if ($debug eq "on") { print "File already exists and we are going to leave it -f=$Overwrite.\n" }; if ($Silent eq "No") { print "File already exists $dstFileMP3Abs\n" }; } else { @Args = ("cp", "-f", $srcFileAbs, $dstFileAbs); system(@Args) == 0 or die "ERROR: system @Args failed: $?\n"; if ($? == -1) { print "ERROR: failed to execute @Args\n $!\n"; } elsif ($? & 127) { printf "ERROR: @Args\n child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; exit 0; } else { if ($debug eq "on") { printf "@Args\n child exited with value %d\n", $? >> 8 }; } if ($debug eq "on") { print "Copied the file from $srcFileAbs $dstFileAbs -f=$Overwrite.\n" }; if ($Silent eq "No") { print "Copied $dstFileAbs\n" }; }; }; }; # Now we set the permissions and owner if ( $Owner ne "" ) { @Args = ("chown", "$Owner", $dstFileAbs); system(@Args) == 0 or die "ERROR: system @Args failed: $?\n"; if ($? == -1) { print "ERROR: failed to execute @Args\n $!\n"; } elsif ($? & 127) { printf "ERROR: @Args\n child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; exit 0; } else { if ($debug eq "on") { printf "@Args\n child exited with value %d\n", $? >> 8 }; } } # Now we set the permissions and owner if ( $Group ne "" ) { @Args = ("chgrp", "$Group", $dstFileAbs); system(@Args) == 0 or die "ERROR: system @Args failed: $?\n"; if ($? == -1) { print "ERROR: failed to execute @Args\n $!\n"; } elsif ($? & 127) { printf "ERROR: @Args\n child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; exit 0; } else { if ($debug eq "on") { printf "@Args\n child exited with value %d\n", $? >> 8 }; } } @Args = ("chmod", "755", $dstFileAbs); system(@Args) == 0 or die "ERROR: system @Args failed: $?"; if ($? == -1) { print "ERROR: failed to execute @Args\n $!\n"; } elsif ($? & 127) { printf "ERROR: @Args\n child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; exit 0; } else { if ($debug eq "on") { printf "@Args\n child exited with value %d\n", $? >> 8 }; } if ($debug eq "on") { print "\n\n"; #sleep 3; }; }, $srcDirAbs;