Converting recorded wav calls to mp3

First of all a big THANK YOU dicko, georgeman and jhayes for all your work.

Just wanted to add my 2 cents. For all the cautious kind like me, i made some small changes so that all the WAV files are moved untill you’re confident you can start deleting them.

Also removed the password for novice users like me so that they can simply copy paste and start using it. most ‘standard’ freepbx installations won’t have a MySQL password.

NOTE: In order to enable “Post Call Recording Script” in freepbx goto SETTINGS > ADVANCED SETTINGS
Change both “Display Readonly Settings” & “Override Readonly Settings” to YES

#!/bin/bash
# ---
# Save script as /var/lib/asterisk/bin/wav2mp3.sh and then:
# chown asterisk:asterisk /var/lib/asterisk/bin/wav2mp3.sh
# chmod +x /var/lib/asterisk/bin/wav2mp3.sh
# ---
# Configure FreePBX postrecording script as: /var/lib/asterisk/bin/wav2mp3.sh ^{CALLFILENAME} ^{UNIQUEID}
# ---

# Wait 7 seconds before we start
sleep 7

# The default path for the recordings includes the current date
dy=$(date '+%Y')
dm=$(date '+%m')
dd=$(date '+%d')

# Path where recordings are stored, including trailing slash
dtpath=/var/spool/asterisk/monitor/$dy/$dm/$dd/

# Path where WAV are moved, including trailing slash
wavpath=/var/spool/asterisk/monitor/wav/$dy/$dm/$dd/

# If the WAV file is 44 bytes long, delete it and exit. These are empty audio files that are generated
# for example when a ring group or queue rings multiple extensions. This has been reported multiple
# times, most recent bug report is https://issues.freepbx.org/browse/FREEPBX-13370
if [ $(wc -c < "$dtpath$1.wav" | cut -d ' ' -f1) -eq 44 ]; then
	rm -f "$dtpath$1.wav"
	exit 0
fi

# Use LAME for the MP3 conversion, on a low priority process
nice lame -b 32 -m m "$dtpath$1.wav" "$dtpath$1.mp3"

# If the conversion was successful, update CDR database and delete WAV file
if [ $? -eq 0 ]; then
	mysql -u root -s -D asteriskcdrdb -e "UPDATE cdr SET recordingfile = '$1.mp3' WHERE uniqueid = '$2'"

# Move to a WAV folder instead of deleting the files 	
	mkdir -p "$wavpath"
	mv "$dtpath$1.wav" "$wavpath$1.wav"
	
# Uncomment	when you are confident and want to start deleting the files, comment the section above to stop moving the files.
# rm -f "$dtpath$1.wav"

else
	exit 1
fi