Fedora Linux Support Community & Resources Center
  #1  
Old 4th November 2006, 09:32 PM
sentry Offline
Registered User
 
Join Date: Jul 2005
Posts: 591
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 06:30 PM.
Reply With Quote
  #2  
Old 20th December 2006, 09:22 PM
ilbh Offline
Registered User
 
Join Date: May 2006
Posts: 302
nice
good job
Reply With Quote
Reply

Tags
audio, conversion, perl, script

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Audible Audio Conversion charactermatter Using Fedora 8 21st September 2008 05:11 AM
Audio file conversion??? fromans4 Using Fedora 6 28th February 2006 03:44 PM


Current GMT-time: 09:25 (Tuesday, 16-09-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat