Transcode Recorded Calls to Opus, Reducing Storage Needs

Call recordings can be quite useful, though their size can be onerous when stored as a wave file (eg: .wav, as Freeswitch does by default).

The script below reduced our stored recordings from the past year to 3.3% of their original size and updated the database so they continued to be accessible in FusionPBX's Call Detail Records & Call Recordings applications.

Also, make sure to install opus-tools! On Debian, apt install opus-tools

PGPASSWORD="$(grep db_password /etc/fusionpbx/config.php | cut -d "'" -f2)"
for i in $(seq 0 365); do
        day="-$i day"
        for DIRECTORY in /var/lib/freeswitch/recordings/*/; do
                DIRCONVERT=/var/lib/freeswitch/recordings/"$(basename "$DIRECTORY")"/archive/"$(date -d "$day" +%Y/%b/%d)"
                if [ -d "$DIRCONVERT" ]; then
                        for WAVFILE in /var/lib/freeswitch/recordings/"$(basename "$DIRECTORY")"/archive/"$(date -d "$day" +%Y/%b/%d)"/*.wav; do
                                bname=$(basename "$WAVFILE" .wav)
                                nice opusenc --quiet --bitrate 16 "$WAVFILE" "$DIRCONVERT/$bname.ogg"
                                psql --quiet --host= --username=fusionpbx -t -c "UPDATE v_call_recordings SET call_recording_name = '$bname.ogg' WHERE call_recording_name = '$bname.wav' and call_recording_path = '$DIRCONVERT'"
                                psql --quiet --host= --username=fusionpbx -t -c "UPDATE v_xml_cdr SET record_name = '$bname.ogg' WHERE record_name = '$bname.wav'"
                                rm "$WAVFILE"
chown -R www-data:www-data /var/lib/freeswitch/recordings
Thanks to DigitalDaz for the initial script & KonradSC for highlighting that the v_call_recordings table needs to be updated!

- Call quality sounds fine at 16Kbps (as the script defaults to), lower bitrates can be used if space is very limited
- Re-encoding progressed at 4.8MB/s on a single core of an E5-2650L v3 CPU, nice deprioritizes transcoding to keep the server responsive
- Script transcodes every .wav in each archive folder from the last 365 days. Update the bounds 0 and 365 on the third line of the script when running this script on a daily cronjob
- The application log in v_xml_cdr will still show a file ending in .wav inside a bit of JSON, as that was what the application wrote to disk when the CDR was created. We do not edit this as the application log doesn't seem to be interpreted by other FusionPBX apps.

If you see the following lines, then your bounds on line 3 contain a day that has already had its files migrated. I don't think this has any ill effects.
/var/lib/freeswitch/recordings/*.wav: No such file or directory
m: cannot remove '/var/lib/freeswitch/recordings/*.wav': No such file or directory
Edit: mention the opus-tools package
Last edited: