An updated version of our script to bulk convert wav call recordings to mp3 as mentioned here.
This version may be redistributed freely, as long as the copyright message remains.
#!/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 2 - 2016/04/15
#
# Changelog
# v2 - Skip broken files (but show an error message)
# v1 - Initial version
#
# Copyright Jaytag Computer Limited 2016 - www.jaytag.co.uk
#
# You may use or modify this script as you wish as long as this copyright
# message remains. Redistribution is permitted.
# Set the Asterisk Recording Directory
recorddir="/var/spool/asterisk/monitor/"
# Start the Loop
for wavfile in `find $recorddir -name \*.wav`; do
# Make Variables from the WAV file names
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"
# Update the CDR Database, only if conversion is sucessful
if [ -e "$mp3file" ]
then
mysql -u root -s -N -D asteriskcdrdb<<<"UPDATE cdr SET recordingfile='$mp3filenopath' WHERE recordingfile = '$wavfilenopath'"
echo "DBUPDATE -------------------------------------------------------"
echo "DBUPDATE - $wavfilenopath changed to $mp3filenopath in CDR DB"
echo "DBUPDATE -------------------------------------------------------"
fi
# 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
8 comments
Join the conversationLes - October 20, 2016
I’m trying to run your script but always get an error.
The error is:
line 35: syntax error near unexpected token `fi’
line 35: `fi’
It seems to be related to:
mysql -u root -s -N -D asteriskcdrdb<<<"UPDATE cdr SET recordingfile='$mp3filenopath' WHERE recordingfile = ‘$wavfilenopath'"
I think it has to do with not being able to specify a password for the db.
Any ideas what could be causing it?
Les - October 21, 2016
Solved above issue: -ppassword in the sql line to specify password of database. But the error was being caused by a missing ; in the line: if [ -e “$mp3file” ]; then
Now, it produces the following error…..
Could not find “find $recorddir -name \*.wav”.
find $recorddir -name \*.wav encoding failed
So there seems to be something else wrong with the script.
Les - October 21, 2016
I finally got it working 🙂 Just in case anyone else is having difficulties, here is the working version:
#!/bin/bash
# A Script to Convert FreePBX call recordings from WAV to MP3……
# 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 fa$
nice lame -b 16 -m m -q 9-resample “$wavfile” “$mp3file” && rm -frv $wavfile ||$
if [ -e “$mp3file” ] ; then
# Update the CDR Database
mysql -u root -pPASSWORD -s -N -D asteriskcdrdb<<<"UPDATE cdr SET recordingfil$
echo "DBUPDATE – $wavfilenopath changed to $mp3filenopath in CDR DB"
fi
# 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
Umair Ahmed Malik - June 29, 2017
how do you suggest this should be run? through a cron or through mixmon?
Marc - May 30, 2018
Does this script act on all subfolders? It appears the recorded calls are being saved in
/var/spool/asterisk/monitor/year/mm/dd (eg: /var/spool/asterisk/monitor/2018/05/29)
Where mm is month and dd is day.
Thank you.
Scott Cover - August 10, 2018
Line 30 should be:
if [ -e “$mp3file” ]; then
Thanks for the super useful script though!
Andrew van Niekerk - November 13, 2018
Great work on the bulk conversion script! Saved me an hour or two
Radu - December 27, 2019
replace if [ -e “$mp3file” ] then with if [ -e “$mp3file” ]; then for the script to work