Skip to content

Instantly share code, notes, and snippets.

@krotesk
Created February 19, 2025 05:27
Show Gist options
  • Save krotesk/8a489aa543dd46b508a98e0a8003f79a to your computer and use it in GitHub Desktop.
Save krotesk/8a489aa543dd46b508a98e0a8003f79a to your computer and use it in GitHub Desktop.
wav2mp3
#!/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}
# ---
# ^{YEAR} ^{MONTH} ^{DAY} ^{CALLFILENAME} ^{MIXMON_FORMAT} ^{MIXMON_DIR}
# Wait 7 seconds before we start
sleep 7
CALLFILENAME=$1
YEAR=`date +%Y`
MONTH=`date +%m`
DAY=`date +%d`
UNIQUEID=$2
#MIXMON_FORMAT=$5
#MIXMON_DIR=$6
#FFILENAME=${SPOOLDIR}${YEAR}/${MONTH}/${DAY}/${CALLFILENAME}.${MIXMON_FORMAT}
#CALLFILENAME=`echo $CALLFILENAME | grep -oP '.*(?=....)'`
# The default path for the recordings includes the current date
# Path where recordings are stored, including trailing slash
dtpath=/var/spool/asterisk/monitor/$YEAR/$MONTH/$DAY/
# 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$4" | cut -d ' ' -f1) -eq 44 ]; then
rm -f "$dtpath$CALLFILENAME.wav"
exit 0
fi
# Use LAME for the MP3 conversion, on a low priority process
nice lame -b 32 -m m "$dtpath$CALLFILENAME.wav" "$dtpath$CALLFILENAME.mp3"
# If the conversion was successful, update CDR database and delete WAV file
if [ $? -eq 0 ]; then
# mysql -uroot -p2128506 -s -D asteriskcdrdb -e "UPDATE cdr SET recordingfile = '$CALLFILENAME.mp3' WHERE uniqueid = '$UNIQUEID'"
mysql -uroot -p2128506 -s -D asteriskcdrdb -e "UPDATE cdr SET recordingfile = '$CALLFILENAME.mp3' where recordingfile='$CALLFILENAME.wav'"
# Move to a WAV folder instead of deleting the files
# mkdir -p "$wavpath"
# mv "$dtpath$4.wav" "$wavpath$4.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$CALLFILENAME.wav"
else
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment