Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Need help modifing a script

by tgeo (Initiate)
on Dec 19, 2012 at 08:29 UTC ( #1009527=perlquestion: print w/replies, xml ) Need Help??
tgeo has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, i am totaly new to this and i need a little help with a script i found over the net.

I have setup an Asterisk PBX that records calls and stores the files in a directory with the folowing names:

Outgoing calls: PBX-OUT-called_number-date-time-unique id.wav

eg: PBX-OUT-92610999999-20121212-145901-1355317141.98146.wav

The above outgoing call when tranfered to int 119: PBX-INTERNAL-called_number-int-date-time-unique id.wav

eg: PBX-INTERNAL-92610999999-119-20121212-145930-1355317141.98146.wav

Notice that both files have the same unique id

Incoming calls: PBX-IN-caller_id-date-time-unique id.wav

eg: PBX-IN-2610999999-20121212-150012-1355317212.9848.wav

The above incomming call when tranfered to int 119: PBX-INTERNAL-caller_id-int-date-time-unique id.wav

eg: PBX-INTERNAL-2610999999-119-20121212-150134-1355317212.9848.wav

Notice that both files have the same unique id

Internal calls: PBX-INTERNAL-from int-to int-date-time-unique id.wav


I need the script to do the folowing:

1. Delete all internal files with filename PBX-INTERNAL-XXX-XXX.*

2. Mix incoming or outgoing call file with internal (tranfered) recording based on the unique id using sox.

eg: PBX-IN-2610999999-20121212-150012-1355317212.9848.wav + PBX-INTERNAL-2610999999-119-20121212-150134-1355317212.9848.wav should make a new file with name PBX-IN-2610999999-20121212-150012-1355317212.9848.wav

3. convert .wav to .mp3 (it is works already)

4. delete converted .wav files (it is works already)

5. move .mp3 files to another server (it is works already)

here is the script

#!/usr/bin/perl $path='/var/spool/asterisk/monitor'; $mount='/mnt/nas/'; @Files = glob($path.'/*.wav'); foreach $File (@Files) { if((-M $File)>0.0002) { if(($FileName)=($File=~/(.+)\-out.wav/)) { system("/usr/bin/sox -m $FileName-in.wav $File +Name-out.wav $FileName.wav"); unlink "$FileName-in.wav"; unlink "$FileName-out.wav"; } elsif(($FileName)=($File=~/(.+)\-in.wav/)) { system("/usr/bin/sox -m $FileName-in.wav $File +Name-out.wav $FileName.wav"); unlink "$FileName-in.wav"; unlink "$FileName-out.wav"; } else { ($FileName) = ($File=~/(.+)\.wav/); } print "Creating $FileName.mp3\n"; system("/usr/bin/lame --cbr -b 32 --noreplaygain -q 2 \"$FileName.wav\ +" \"$FileName.mp3\" --quiet"); if(-e "$FileName.mp3") { print "Moving $FileName.mp3 to $mount\n"; system("mv ".$FileName.".mp3 ".$mount); print "Deleting $FileName.wav\n"; unlink "$FileName.wav"; } } }

Can someone please help

Replies are listed 'Best First'.
Re: Need help modifing a script
by roboticus (Chancellor) on Dec 19, 2012 at 13:01 UTC


    For step 1, look at line 6 to see how to get a list of filenames based on a pattern, and look at the code for step 4 (ca line 37) to see how to delete the files listed.

    For step 2, read up on sox to see how to do the mixing (I've never used it), and look at lines 14 and 20 to see how to call it.


    When your only tool is a hammer, all problems look like your thumb.

Re: Need help modifing a script
by bart (Canon) on Dec 19, 2012 at 12:51 UTC
    Your main problem appears to be that the original script depended on "-in" and "-out" as a suffix for the files to work, and now you have to deal with a file name prefix instead.

    So I'd say you have to strip the path from the filename (it's always the same anyway), or, perhaps easier, avoid adding it in the first place, by chdirring to the directory before calling glob, and then you're free to go.

    chdir $path or die "Cannot chdir to $path: $!"; @Files = glob('*.wav');
    Then you can safely do:
    foreach my $File (@Files) { next unless -e $File; # already deleted my($type, $basename) = $File =~ /^PBX-(INTERNAL|IN|OUT)-(.*)\.wav$ +/; if(!$type) { # ??? unlink $File; } elsif($type ne 'INTERNAL') { # IN, OUT system("/usr/bin/sox -m $File PBX-INTERNAL-$basename.wav $base +name.wav"); unlink $File, "PBX-INTERNAL-$basename.wav"; # delete original +s system("/usr/bin/lame --cbr -b 32 --noreplaygain -q 2 \"$basen +ame.wav\" \"$basename.mp3\" --quiet"); if(!$? && -e "$basename.mp3") { system("mv \"$basename.mp3\" $mount"); unlink "$basename.wav"; } } }
    Untested, but at least it passes syntax check.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009527]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2018-06-22 02:34 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (121 votes). Check out past polls.