CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need help modifing a script

 



tgeo
New User

Dec 19, 2012, 12:33 AM

Post #1 of 6 (1509 views)
Need help modifing a script Can't Post

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
PBX-INTERNAL-206-120-20121212-160547-1355317520.9958.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

Code
#!/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 $FileName-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 $FileName-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


FishMonger
Veteran / Moderator

Dec 19, 2012, 7:01 AM

Post #2 of 6 (1496 views)
Re: [tgeo] Need help modifing a script [In reply to] Can't Post

What portion of your script is not doing what you expect?.

To start with, you should add the strict and warnings pragmas to every Perl script you write. They will point out lots of common mistakes.

Code
#!/usr/bin/perl 

use strict;
use warnings;


You should check the return code of the system calls to make sure they were successful before you unlink the files.

Instead of the system call to the 'mv' command, you should use the File::Copy module.
http://search.cpan.org/~dom/perl-5.12.5/lib/File/Copy.pm

==========================

I assume you're currently using the monitor() dialplan app to record the calls.

It might be better to take a step back and use mixmonitor() instead of monitor(). The mixmonitor() app will mix the 2 audio streams and save it in a single file.

http://www.voip-info.org/wiki/view/MixMonitor


tgeo
New User

Dec 19, 2012, 9:34 AM

Post #3 of 6 (1478 views)
Re: [FishMonger] Need help modifing a script [In reply to] Can't Post

Hi

Thank you for your sugestions, i already added the strict and warnings pragmas and make all the nessecery changes to the code.

I do use mixmonitor() but because i am writing custom filenames this causes to make 2 diferent files.
So i need the script to check if there is tha same unique id on two files and combine them using sox.

Lets say we have an incoming call from callerd id 2222222 that was answered by the extension 120, the filename would be PBX-IN-2222222-20121219-190652-1355936812.18342.wav
and let's say the same call is tranfered from ext 120 to ext 130, then i would have a new file PBX-INTERNAL-2222222-130-20121219-190735-1355936812.18342.wav

so what i need the script to do is to combine the above two files into one,

for eg

$File1=PBX-IN-2222222-20121219-190652-1355936812.18342.wav
$File2=PBX-INTERNAL-2222222-130-20121219-190735-1355936812.18342.wav

system("/usr/bin/sox -m $File1 $File2 $File1");

I hope you get what i mean.



FishMonger
Veteran / Moderator

Dec 19, 2012, 10:10 AM

Post #4 of 6 (1473 views)
Re: [tgeo] Need help modifing a script [In reply to] Can't Post


Quote
I do use mixmonitor() but because i am writing custom filenames this causes to make 2 diferent files.

Using custom filenames should not cause it to not mix the audio streams. It doesn't have that problem for me and the wiki doesn't give any indication that it would have that side effect.

Are you sure that you don;t have a dialplan conflict where it's actually using monitor()?

I do understand what you're needing to do, but you still haven't given any info on where your script is failing.

I have not tested your script and since you haven't explained what's wrong, I can't say what you need to change.


tgeo
New User

Dec 19, 2012, 11:58 AM

Post #5 of 6 (1467 views)
Re: [FishMonger] Need help modifing a script [In reply to] Can't Post

here is the recording script from elastix file: extensions_override_elastix.conf

Code
[macro-record-enable] 
include => macro-record-enable-custom
exten => s,1,GotoIf($["${BLINDTRANSFER}" = ""]?check)
exten => s,n,ResetCDR(w)
exten => s,n,StopMixMonitor()
exten => s,n(check),ExecIf($["${ARG1}"=""]?MacroExit())
exten => s,n,GotoIf($["${ARG2}"="Group"]?Group:OUT)
exten => s,n(Group),Set(LOOPCNT=${FIELDQTY(ARG1,-)})
exten => s,n,Set(ITER=1)
exten => s,n(begin),GotoIf($["${CUT(DB(AMPUSER/${CUT(ARG1,-,${ITER})}/recording),=,3)}" != "Always"]?continue)
exten => s,n,Set(TEXTEN=${CUT(ARG1,-,${ITER})})
exten => s,n,Noop(Recording enable for ${TEXTEN})
exten => s,n,Set(CALLFILENAME=PBX-IN-${CALLERID(number)}-${STRFTIME(${EPOCH},,%Y%m%d-%H%M%S)}-${UNIQUEID})
exten => s,n,System(echo -n -e "PBX-IN-${CALLERID(number)}-${STRFTIME(${EPOCH},,%Y%m%d-%H%M%S)}-${UNIQUEID}" | nc -u -w 1 192.168.135.36 30000)
exten => s,n,Goto(record)
exten => s,n(continue),Set(ITER=$[${ITER}+1])
exten => s,n,GotoIf($[${ITER}<=${LOOPCNT}]?begin)
exten => s,n(OUT),GotoIf($["${ARG2}"="IN"]?IN)
exten => s,n,ExecIf($["${CUT(DB(AMPUSER/${ARG1}/recording),\\\|,1):4}" != "Always"]?MacroExit())
exten => s,n,Noop(Recording enable for ${ARG1})
exten => s,n,Set(CALLFILENAME=PBX-OUT-${MACRO_EXTEN}-${STRFTIME(${EPOCH},,%Y%m%d-%H%M%S)}-${UNIQUEID})
exten => s,n,System(echo -n -e "PBX-OUT-${MACRO_EXTEN}-${STRFTIME(${EPOCH},,%Y%m%d-%H%M%S)}-${UNIQUEID}" | nc -u -w 1 192.168.135.36 30000)
exten => s,n,Goto(record)
exten => s,n(IN),ExecIf($["${CUT(DB(AMPUSER/${ARG1}/recording),\\\|,2):3}" != "Always"]?MacroExit())
exten => s,n,Noop(Recording enable for ${ARG1})
exten => s,n,Set(CALLFILENAME=PBX-INTERNAL-${CALLERID(number)}-${ARG1}-${STRFTIME(${EPOCH},,%Y%m%d-%H%M%S)}-${UNIQUEID})
exten => s,n(record),MixMonitor(${MIXMON_DIR}${CALLFILENAME}.${MIXMON_FORMAT},a,${MIXMON_POST})
exten => s,n,Set(CDR(userfield)=audio:${CALLFILENAME}.${MIXMON_FORMAT})
exten => s,n,MacroExit()


There isn't anywhere else use of monitor()

I don't get any error while running the script buy it doesn't merge files with the same id, i think i need to alter something on the code below in order to check if two files have the same id and if they do run the system command /usr/bin/sox


Code
 if(($FileName)=($File=~/(.+)\-out.wav/)) 
{
system("/usr/bin/sox -m $FileName-in.wav $FileName-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 $FileName-out.wav $FileName.wav");
unlink "$FileName-in.wav";
unlink "$FileName-out.wav";
}
else
{
($FileName) = ($File=~/(.+)\.wav/);
}


Below you can see some files of my /var/spool/asterisk/monitor directory


Code
PBX-IN-6979551616-20121219-212116-1355944876.18832.wav 
PBX-INTERNAL-6979551616-206-20121219-212151-1355944876.18832.wav
PBX-OUT-92610426000-20121219-212326-1355945006.18851.wav
PBX-IN-6975907292-20121219-212416-1355945056.18853.wav
PBX-OUT-92610622182-20121219-214602-1355946364.18878.wav
PBX-INTERNAL-2610622182-399-20121219-214635-1355946364.18878.wav

At the above directory the script should merge file 1 and 2 with the same name as file 1
it should also merge file 5 with file 6
Those 4 files are actualy 2 phonecalls, one incoming witch was tranfered to ext 206 and one outgoing witch was transfered to ext 399

I hope the problem is clear now and someone could help.

PS: Forgive my terible English


FishMonger
Veteran / Moderator

Dec 19, 2012, 12:58 PM

Post #6 of 6 (1462 views)
Re: [tgeo] Need help modifing a script [In reply to] Can't Post

Where and how are the global vars (in particular the ${CALLFILENAME} and ${MIXMON_FORMAT} vars) in the MixMonitor command being defined/assigned? That appears to be why and where the key problem of the separate audio files is located.

Did you write the macro or is it part of elastix? I've never used elastix. We always build asterisk from source.

None of the filenames you posted would match -out.wav or -in.wav so there's no reason to expect either of those sox commands to be executed, which would explain why the files are not being merged.

Your regex needs to extract the channel's unique id (which appears to be the digits just prior to the .wav ext) and key off of that when matching up the inbound/outbound streams.

Is this script the one defined in ${MIXMON_POST} which is then executed at the end of the call, or is that some other command?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives