convert-wav-to-mp3

One of our customers recently reported an interesting issue. They were running FreePBX on a machine with a very small amount of hard drive space. Usually, this would not be an issue, as FreePBX can be installed easily onto a small drive, even less than 10GB (I have a moment of reflection now, thinking that actually 10GB is massive!!)

In any case, when you start recording calls on a system, the free space is quickly eaten up.

Within FreePBX, there is no way to set the call recording to MP3, and as a result, large WAV files are created.

The solution? Convert them to MP3, and update the CDR database to reflect the changed filenames - so that if you wish to download the call recordings from the web UI, the links are correct.

Prerequisites:

  • LAME

The script looks through /var/spool/asterisk/monitor where the call files are stored, and then converts the WAV files to MP3. The WAV files are then deleted.

This version is designed for a one-time-run from the command line. You will of course need to modify the mysql -u portion if you wish to make the update changes as another user.

Exercise caution! If you do not want to delete the WAV files, delete "&& rm -frv $wavfile" from the below script.

#!/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

Edit 15/06/2016: There is a new version of the script here.

Previous Post Next Post