How To Convert FusionPBX Call Recordings to Ogg

In this article you will learn how to convert FusionPBX Call Recordings from WAV to OGG (OPUS) with Linux script.

This script finds yesterday *.wav FisionPBX call recordings, convert them to opus and then delete. After updates CDR records.

Script converts files with FFmpeg converter. And should executed daily with cron job.

Convert FusionPBX Call Recordings with Linux Script

Create new file convert-to-opus in /etc/cron.daily and make the script executable.

# cd /etc/cron.daily/ 
# touch convert-to-opus
# chmod +x convert-to-opus

Edit file convert-to-opus and fill with code below, change PGPASSWORD with yours DB connection password.

#!/bin/sh
#
#  Needs ffmpeg to work.  Verify recordings directory and set database PW
#
#

#PGPASSWORD="$(grep db_password /etc/fusionpbx/config.php | cut -d "'" -f2)"
export PGPASSWORD="YOUR_DB_PASSWORD"


export PGPASSWORD

#Find wav files from yesterday and convert them to opus, then delete original files, then update xml cdr and call recording database entriesecho "started conversion" >> /root/convert.log
date >> /root/convert.log

for DIRECTORY in /var/lib/freeswitch/recordings/*/; do
  DIRYESTERDAY=/var/lib/freeswitch/recordings/`basename "$DIRECTORY"`/archive/`date -d "yesterday" +%Y/%b/%d`
    if [ -d "$DIRYESTERDAY" ]; then
      for WAVFILE in /var/lib/freeswitch/recordings/`basename "$DIRECTORY"`/archive/`date -d 'yesterday' +%Y/%b/%d`/*.wav; do
      BNAME=`basename $WAVFILE .wav`
echo $BNAME
        /etc/cron.daily/ffmpeg -i $WAVFILE -c:a libopus -b:a 8k $DIRYESTERDAY/$BNAME.ogg >> /dev/null
        psql --host=127.0.0.1 --username=fusionpbx -t -c "UPDATE v_call_recordings SET call_recording_name = '$BNAME.ogg' WHERE call_recording_name = '$BNAME.wav' and call_recording_path = '$DIRYESTERDAY'"
        psql --host=127.0.0.1 --username=fusionpbx -t -c "UPDATE v_xml_cdr set record_name = '$BNAME.ogg' WHERE record_name = '$BNAME.wav'"
      done
    rm $DIRYESTERDAY/*.wav
    fi
 chown -R www-data:www-data $DIRYESTERDAY/*
done
# run again for plain -archive- lost calls
for DIRECTORY in /var/lib/freeswitch/recordings/*/; do
  DIRYESTERDAY=/var/lib/freeswitch/recordings/`basename "$DIRECTORY"`/`date -d "yesterday" +%Y/%b/%d`
    if [ -d "$DIRYESTERDAY" ]; then
      for WAVFILE in /mnt/retain/`basename "$DIRECTORY"`/`date -d 'yesterday' +%Y/%b/%d`/*.wav; do
      BNAME=`basename $WAVFILE .wav`
echo $BNAME
        /etc/cron.daily/ffmpeg -i $WAVFILE -c:a libopus -b:a 8k $DIRYESTERDAY/$BNAME.ogg >> /dev/null
        psql --host=127.0.0.1 --username=fusionpbx -t -c "UPDATE v_call_recordings SET call_recording_name = '$BNAME.ogg' WHERE call_recording_name = '$BNAME.wav' and call_recording_path = '$DIRYESTERDAY'"
        psql --host=127.0.0.1 --username=fusionpbx -t -c "UPDATE v_xml_cdr set record_name = '$BNAME.ogg' WHERE record_name = '$BNAME.wav'"
      done
    rm $DIRYESTERDAY/*.wav
    fi
 chown -R www-data:www-data $DIRYESTERDAY/*
done


echo "conversions complete" >> /root/convert.log
echo $(date -u) "conversions ccomplete" >> /root/convert.log

If you have trouble with copy paste text from here, script can be downloaded here.

Install FFmpeg

This script requires FFmpeg to work. You can download FFmpeg Static Builds from here: https://johnvansickle.com/ffmpeg/

# cd /etc/cron.daily/
# wget https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz
# tar xf ffmpeg-release-amd64-static.tar.xz
# rm ffmpeg-release-amd64-static.tar.xz
# mv ffmpeg-4.2.2-amd64-static/ffmpeg /etc/cron.daily/
# rm -rf ffmpeg-4.2.2-amd64-static

Or simply install on install on Ubuntu/Debian by running:

$ sudo apt-get install ffmpeg

Credits Stratoswitch.com

   

If you like what you are reading, please:

Buy me a coffeeBuy me a coffee

arstech

Start the discussion at forum.arstech.net