<---- template headericclude ----->
A perl audio conversion script
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2005
    Posts
    590
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A perl audio conversion script

    I wrote this script at the request of a friend at work to convert audio files. It works recursively through directories and uses ffmpeg to convert the audio files. It's written in perl so have a look and let me know if this is useful.

    I use perl off and on so I welcome any tips on making this more effecient from you perl guru's

    Code:
    #!/usr/bin/perl
    
    # audio_convert.pl
    #
    # This script will convert all audio files in a directory to a new type of audio
    # file.  Will only convert files that do not already have a converted 
    #	counterpart.  This script is recursive.
    #
    # input arg 1 = a path (with or without trailing slash) to a directory to 
    #               convert files in.
    # input arg 2 = the source file extension to look for (.wma, .mp3, etc)
    # input arg 3 = the extension of the file type to convert to (mp3, aac, etc)
    
    # Created: 2006.11.04
    # Author: Sentry
    # Modified: 2006.11.05: added support for relative addressing and flac support
    # 											by Sentry
    # Modified: 2006.11.22: added support for spaces in input directories/files by 
    #												Sentry
    # Modified: 2006.12.24: Added support for converting from flac (not sure is FC6
    #												ffmpeg properly converts flac, it does convert wav) by
    #												Sentry
    
    use strict;
    
    sub recurse {
      my ($path, $convertFrom, $convertTo) = @_;
    
      ## append a trailing / if it's not there
      $path .= '/' if($path !~ /\/$/);
    	
    	$path =~ s/ /\\ /g;
    	
      ## loop through the files contained in the directory
      for my $eachFile (glob($path.'*')) {
        ## if the file is a directory
        if( -d $eachFile) {
          ## pass the directory to the routine ( recursion )
          recurse($eachFile, $convertFrom, $convertTo);
        } else {
    			my $isRelativeAddress = substr($path, 0, 2) eq ".." ? 0 : 1;			
    			my @fileParts = split(/\./, $eachFile);
    
          # Check to see that the current file is in an audio type you want to 
    			# convert
          if (@fileParts[(@fileParts - 1)] eq $convertFrom) {
            my $newFile = $isRelativeAddress == 0 ? ".." : "";
    				my $filePartSize = (@fileParts - 2);
    				
    				my $i = 0;				
    				# loop through the file parts in case there is more than one "." in the 
    				# name
    				for ($i = 0; $i <= $filePartSize; $i++) {
    					$newFile = $newFile . $fileParts[$i];
    				}        
            
    				$newFile = $newFile . "." . $convertTo;           
     
            # convert only if the intended version of this audio file does not 
    				# already exist
            if ( -e $newFile) {
              print $newFile . " already exists, skiping conversion...\n";
            } else {
              # escape spaces in file names
    					$newFile =~ s/ /\\ /g;
    					$eachFile =~ s/ /\\ /g;
    					my $options = $convertTo eq "flac" ? " -ac 3 " : " -ab 128 -ac 3 ";
    					
    					# If this is a flac file decode first, then convert
    					if ($convertFrom eq "flac") {
    						system("/usr/bin/flac -d " . $eachFile);
    						
    						$eachFile =~ s/\.flac/\.wav/;
    
    						system("/usr/bin/ffmpeg -i " . $eachFile . $options . $newFile);
    						system("/bin/chmod 764 " . $newFile);
    						system("/bin/rm -f " . $eachFile);
    					} else {
    						system("/usr/bin/ffmpeg -i " . $eachFile . $options . $newFile);
    						system("/bin/chmod 764 " . $newFile);  
    					}    
            } 
          } 
        }
      }
    }
    
    ## initial call ... $ARGV[0] is the first command line argument
    recurse($ARGV[0], $ARGV[1], $ARGV[2]);
    [Edit] Removed note about SELinux issue, it's been fixed.
    Last edited by sentry; 23rd January 2007 at 07:30 PM.

  2. #2
    ilbh Guest
    nice
    good job

Similar Threads

  1. Audible Audio Conversion
    By charactermatter in forum Using Fedora
    Replies: 8
    Last Post: 21st September 2008, 05:11 AM
  2. Audio file conversion???
    By fromans4 in forum Using Fedora
    Replies: 6
    Last Post: 28th February 2006, 04:44 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
[[template footer(Guest)]]