Converting recorded wav calls to mp3

First off I would like to state that I know freepbx does not support the mp3 conversion of recorded calls.

With that being said I am hoping someone can help me with my dilemma. I have a bash script running as a cronjob that converts recorded wav files to mp3 files and updates the cdr database. The problem I am having is: If there is an in-progress call when the cronjob executes the portion of the call that has yet to be recorded is “lost”. The cdr record is also not changed to the new file. I am hoping that someone knows how to modify the existing bash I am using to only run against calls that are no long in progress or to only run it as a post call script on the call.

Below is the code:

#!/bin/bash

# A Script to Convert FreePBX call recordings from WAV to MP3

# Also updates the CDR database, for correct downloads through the web UI

# Version 1 - 2015/11/15

#

# Copyright Jaytag Computer Limited 2015 - www.jaytag.co.uk

#

# You may use or modify this script as you wish as long as this copyright

# message remains. Redistribution prohibited.

# Set the Asterisk Recording Directory

recorddir="/var/spool/asterisk/monitor"

# Start the Loop, store the path of each WAV call recording as variable $wavfile

for wavfile in `find $recorddir -name \*.wav`; do

# Make Variables from the WAV file names, stripping the file path with sed

wavfilenopath="$(echo $wavfile | sed 's/.*\///')"

mp3file="$(echo $wavfile | sed s/".wav"/".mp3"/)"

mp3filenopath="$(echo $mp3file | sed 's/.*\///')"

# Convert the WAV files to MP3, exit with an error message if the conversion fails

nice lame -b 16 -m m -q 9-resample "$wavfile" "$mp3file" && rm 
-frv $wavfile || { echo "$wavfile encoding failed" ; exit 1; }

# Update the CDR Database

mysql -u root -s -N -D asteriskcdrdb<<<"UPDATE cdr SET 
recordingfile='$mp3filenopath' WHERE recordingfile = '$wavfilenopath'"

# On-Screen display of variables for debugging/logging

# echo ""

# echo "File -------------------------------------------------------"

# echo "Wav File : " $wavfile

# echo "Wav No Path : " $wavfilenopath

# echo "MP3 File : " $mp3file

# echo "MP3 No Path : " $mp3filenopath

# echo "End File ---------------------------------------------------"

# echo ""

# End the Loop

done
2 Likes

If you allow display and override “readonly settings” in advanced settings there is a setting for “Post Call Recording Script”, you can take it from there after reading the “hover over help” for how to use asterisk variables.

1 Like

I apologize but I should have noted that I am fairly new to the freepbx world and if you could elaborate on your post I would appreciate it. I do not do much code writing, so some of this still doesn’t make any sense to me. Sorry for needing to be “spoon feed”

1 Like

Should be enough.

2 Likes

That was not the solution I needed but it pointed me in the direction. With a executable post script of: /var/lib/asterisk/bin/convert_recordings.sh ^{TIMESTR} ^{FROMEXTEN} ^{CALLFILENAME} ^{UNIQUEID} ^{ARG3} and code of:

/bin/nice /bin/sleep 3

dy=$(date '+%Y')
dm=$(date '+%m')
dd=$(date '+%d')
file_age=35
wavfilenopath="$(echo $dtpath$3.wav | sed 's/.*\///')"
mp3filenopath="$(echo $dtpath$3.mp3 | sed 's/.*\///')"
dtpath=/var/spool/asterisk/monitor/$dy/$dm/$dd/

/bin/nice /usr/local/bin/lame -b 16 -m m -q 9-resample "$dtpath$3.wav" "$dtpath$3.mp3" && rm -frv "$dtpath$3.wav"
/bin/nice /bin/chown  asterisk:asterisk $dtpath/$3.mp3

# Update the CDR Database
mysql -u root -s -N -D asteriskcdrdb<<<"UPDATE cdr SET
recordingfile='$mp3filenopath' WHERE recordingfile = '$wavfilenopath'"

It runs the conversion on only the intended call and then updates the cdr and removes the wav file.

Thank you dicko for your assistance.

5 Likes

Dicko - to circle back around to this. Is there a cleaner way to execute this functionality. The problem is we (by law) record every call retained for at least 365 days and with the post recording script enabled the lag in outbound connection time on calls turns from 5 seconds to 90 seconds. We would prefer to continue to use the mp3 format as it is more universally playable.

Any thoughts would be appreciated.

1 Like

It shiuld be about as clean as you can get apart from the spurious sleep command ( It runs on every recorded call on completion), and that you have a file_age variable set to 35 , i would assume you wanted 366 so why did you put it there? BUT it is unused and not appropriate.

That purgeing if you want it, belongs in an external but synchronous cron job.

There is nothing intrinsic in FreePBX that will delete anything.

Nothing there in that script will change the connection time, . that would be a different problem.

1 Like

The file_age variable was in one of the two files I used to compile the code. I was not sure what it did, and since I do not fully understand the code, I left it. I have since commented it out.

If I am to understand your second comment. The purging is the changing of the cdr log. Correct?

As for the script not affecting the call connection time. I understand that it should not affect this but as soon as I disabled the post recording script the issue of call connection lag was no longer present. When I re-enable this conversion calls start to take nearly 90 seconds to connect.

1 Like

I have no idea why as that script should have no impact on the dialplan until “hangup”. Perhaps a copy of a call from the “full” log would helpfully provide timestamps to possibly show any delays,

I will comment though that you call the script with ^{ARG3}, again why? , your post is not actually a complete working script as there is no initial hash/bang line apparent, $dtpath is neither apparently defined and I would not run any command through nice yet.

Perhaps you could post the script in it’s entirety.

The purging could be good for deleting recordings over 365 day to conserve disk space and remain compliant, deleting the cdr records really won’t save any space though.

1 Like

I also have a script that i would like to run after a call is recorded. If I call the script manually it works fine, /usr/sbin/./convert_wav2mp3 but if I put that in Advanced settings “Post Call Recording Script” not only does it not run but it also stops the call from being recorded, what would be the proper entry in “Post Call Recording Script” to call that script.

gary.

1 Like

your script needs to runnable by the asterisk user, I suggest you put it in /var/lib/asterisk/bin and then

chmod -R asterisk:asterisk /var/lib/asterisk/bin

2 Likes

Thanks dicko for your reply, I moved the script to the location you suggested, and made asterisk:asterisk and executable I can still run the script from cammand line ./convert_wav2mp3 and it works, but can not get it to run from “Post Call Recording Script” what command do I need to place into that field.

Gary.

1 Like

/var/lib/asterisk/bin/convert_wav2mp3 argument1 . . etc.

2 Likes

dicko,

I did what you said in this post and converting the wav files to mp3 works like a dream. even verified it actually runs

== MixMonitor close filestream (mixed)
== Executing [/var/lib/asterisk/bin/convert_recordings.sh]
== End MixMonitor Recording DAHDI/25-1

had 573MB of recordings and after converted it was 57MB

2 Likes

Excellent, gotta love an OS that does exactly what you want, (if you understand how it does it :slight_smile: ) .

1 Like

I agree with you 100%

1 Like

Shame people are not the same…

Very nice little script,
gonna have to use that I think, Cheers.

Hv.

1 Like

It works flawless. I put the script into /var/lib/asterisk/bin and then change the ownership and then added
/var/lib/asterisk/bin/convert_wav2mp3.sh to the Post Call Recordings section of Advanced Settings.

What are all all the variables used for in Post Call Recordings.

1 Like

You will likely only need the ones mentioned in the “hover over help”, filename, path and format.

1 Like

I just put on the Line for Post Call Recordings /var/lib/asterisk/bin/convert_wav2mp3.sh

and everything works. The Help does not explain it very well lol

1 Like