📜 ⬆️ ⬇️

Keeping recordings of conversations in mp3 in FreePBX / Asterisk

Now FreePBX is an extremely popular wrapper for Asterisk, which is no less popular as a telephone digital server. A separate plus of such systems is the ability to deploy on low-cost vds-servers (I deploy clients to vds worth 299r / month, 2GB of RAM, 2.8 GHZ processor, 20GB of disk space). Such a system easily serves 10-20 simultaneous calls, writes audio, allows you to embed the telephone part of a business process into the rest of the logic (interaction with crm, calls from the site / browser, autoinformers based on data in the sub, finding out the search data by caller number for shares seconds, even speech recognition and synthesis!).

Everything would be fine, but at “abundant” telephone calls in half a month the disk space ends. Those same 20GB fly away from some customers in a week! And the hoster, unfortunately, does not provide fuse on OpenVZ tariffs, which is essential for the operation of “realtime - network file systems,” like ftpfs.

Below is my set of measures to combat the problem. We will work with the / var / spool / asterisk / monitor directory, where the .wav files lie, carefully arranged asterisk into directories: year, month and day.

Initially, I went from the wrong end - I began to transfer some data to webdav (ya.disk) and ftp. This was done by a script that was launched once a month according to the crown. The script left the specified number of months on the server, transferred the rest to the remote resource.
')
For yandex.disk, he additionally packed the data “monthly”, which required free space to create an archive. But, nevertheless, in some places he caught on:

#settings #for disk.yandex.ru YAUSER=login@yandex.ru YAPASSWORD=verystrongpassword MONPATH=/var/spool/asterisk/monitor LASTMONTH=1 #      DIR=astsounds #  ,     #start MONTH=$(date +"%m") for YEAR in $( ls $MONPATH ); do for MONTH in $( ls $MONPATH/$YEAR/ ); do if [ $MONTH -le $(($(date +"%m")-$LASTMONTH)) ] || [ $YEAR -ne $(date +"%Y") ]; then tar -zcvf $MONPATH/$YEAR/$MONTH/$YEAR-$MONTH.tar.gz $MONPATH/$YEAR/$MONTH/ >/dev/null curl -T $MONPATH/$YEAR/$MONTH/$YEAR-$MONTH.tar.gz --user $YAUSER:$YAPASSWORD https://webdav.yandex.ru/$DIR/$YEAR-$MONTH.tar.gz && rm -f -r $MONPATH/$YEAR/$MONTH >/dev/null fi done #remove old YEAR dir if [ $YEAR -ne $(date +"%Y") ]; then rm -f -r $MONPATH/$YEAR fi done 

When I wrote a similar script for ftp, I decided not to archive audio. And it's easier to get, and links can be formed to a third-party resource, for online listening from a CDR report. Requires lftp utility.

 #move audio records to XXX.ru free FTP server #settings FTPSRV=node0.XXXX.ru FTPUSER=username_ftpserver FTPPWD=passwd_ftpserver MONPATH=/var/spool/asterisk/monitor LASTMONTH=1 DIR=recordings #log and cd ftpcommand() { lftp -u $FTPUSER,$FTPPWD -e "$1" $FTPSRV } ftpcommand "mkdir public_http; mkdir public_http/$DIR; quit;" MONTH=$(date +"%m") for YEAR in $( ls $MONPATH ); do for MONTH in $( ls $MONPATH/$YEAR/ ); do if [ $MONTH -le $(($(date +"%m")-$LASTMONTH)) ] || [ $YEAR -ne $(date +"%Y") ]; then echo "saving $YEAR/$MONTH" ftpcommand "mkdir public_http/$DIR/$YEAR; mkdir public_http/$DIR/$YEAR/$MONTH; quit;" for DAY in $( ls $MONPATH/$YEAR/$MONTH/ ); do ftpcommand "mkdir public_http/$DIR/$YEAR/$MONTH/$DAY; quit;" for FILE in $( ls $MONPATH/$YEAR/$MONTH/$DAY ); do ftpcommand "cd /public_http/$DIR/$YEAR/$MONTH/$DAY/; put $MONPATH/$YEAR/$MONTH/$DAY/$FILE; quit;" done done rm -f -r $MONPATH/$YEAR/$MONTH fi done #remove old YEAR dir if [ $YEAR -ne $(date +"%Y") ]; then rm -f -r $MONPATH/$YEAR fi done 

This script was also cast in the monthly crowns.

But for some people, as I already wrote, this was not enough. Data scored disk for the week. Then the thought came to store everything in mp3. In fact - the winnings were 4 times. It will require lame .

To begin with, put in / etc / asterisk such a mixmon_mp3.sh script and give it one with the asterisk host: chown asterisk. mixmon_mp3.sh . Also give the right to run: chmod + x mixmon_mp3.sh .

 #!/bin/sh #convert wav to mp3 asterisk recordings cdrdb="asteriskcdrdb" cdrtable="cdr" astdbuser="asteriskuser" #    astdbuserpass="asteriskuserpassword" #   for i in `find /var/spool/asterisk/monitor -type f -name "$1"` do if [ -e "$i" ]; then file=`basename "$i" .wav`; dir=`dirname "$i"`; lame -h -b 192 "$i" "$dir/$file.mp3"; rm -f "$dir/$file.wav"; sleep 3 mysql --user="$astdbuser" --password="$astdbuserpass" --database="$cdrdb" --execute='UPDATE '$cdrtable' SET recordingfile="'$file'.mp3" WHERE recordingfile="'$file'.wav";'; fi done 

Then, in the FreePBX webmord, in the Settings - Advanced Settings section, we enable Display Readonly Settings and Override Readonly Settings , after which the Post Call Recording Script setting is available in the Developer and Customization section. There and write the script call line with the parameter:

 /etc/asterisk/mixmon_mp3.sh ^{CALLFILENAME}.^{MIXMON_FORMAT} 

Unfortunately, the variable MIXMON_DIR was empty, so you have to be content with the file name and search it in the monitor directory. The script finds the file, converts it to mp3, deletes the wav and corrects the entry in the cdr table where the reports are stored. This is necessary for normal listening and downloading files in a webmord with reports.

We will also need to finish the webmord cdr module itself:
/ var / www / html / admin / modules / cdr /
cdr_audio.php
 <?php if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); } /** * @file * plays recording file */ if (isset($_REQUEST['cdr_file'])) { include_once("crypt.php"); $REC_CRYPT_PASSWORD = (isset($amp_conf['AMPPLAYKEY']) && trim($amp_conf['AMPPLAYKEY']) != "")?trim($amp_conf['AMPPLAYKEY']):'TheWindCriesMary'; $crypt = new Crypt(); $opath = $_REQUEST['cdr_file']; $path = $crypt->decrypt($opath,$REC_CRYPT_PASSWORD); // Gather relevent info about file $size = filesize($path); $name = basename($path); $extension = strtolower(substr(strrchr($name,"."),1)); // This will set the Content-Type to the appropriate setting for the file $ctype =''; switch( $extension ) { case "WAV": $ctype="audio/x-wav"; break; case "wav": $ctype="audio/x-wav"; break; #--------------------- case "mp3": $ctype="audio/mpeg"; break; #--------------------- case "ulaw": $ctype="audio/basic"; break; case "alaw": $ctype="audio/x-alaw-basic"; break; case "sln": $ctype="audio/x-wav"; break; case "gsm": $ctype="audio/x-gsm"; break; case "g729": $ctype="audio/x-g729"; break; default: //not downloadable // echo ("<b>404 File not found! foo</b>"); // TODO: what to do if none of the above work? break ; } $fp=fopen($path, "rb"); if ($size && $ctype && $fp) { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: audio file"); header("Content-Type: " . $ctype); header("Content-Disposition: attachment; filename=" . $name); header("Content-Transfer-Encoding: binary"); #--------------------- header("Accept-Ranges: bytes"); header("Connection: close"); header("Content-Length: $size"); header("Content-Range:bytes 0-$size/$size"); header("Content-length: " . $size); #--------------------- $chunksize = 1*(1024*1024); while (!feof($fp)) { $buffer = fread($fp, $chunksize); echo $buffer; ob_flush(); flush(); } fclose($fp); } } ?> 

cdr_play.php

 <?php if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); } /** * @file * popup window for playing recording */ include_once("crypt.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <TITLE>CDR Viewer</TITLE> <style type="text/css"> .popup_download { color: #105D90; margin: 5px; font-size: 12px; text-align: left; } </style> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <?php $crypt = new Crypt(); $REC_CRYPT_PASSWORD = (isset($amp_conf['AMPPLAYKEY']) && trim($amp_conf['AMPPLAYKEY']) != "")?trim($amp_conf['AMPPLAYKEY']):'TheWindCriesMary'; $path = $crypt->decrypt($_REQUEST['recordingpath'],$REC_CRYPT_PASSWORD); $file = urlencode($crypt->encrypt($path,$REC_CRYPT_PASSWORD)); if (isset($file)) { #--------------------- echo("<audio controls>\n<source src=\"config.php?skip_astman=1&quietmode=1&handler=file&module=cdr&file=cdr_audio.php&cdr_file=$file\" type=\"audio/mpeg\">\n</audio>"); #--------------------- } ?> </body> </html> 

Now you can convert existing files to mp3 and put this script in the daytime cron just in case:

 #!/bin/sh #convert wav to mp3 asterisk recordings cdrdb="asteriskcdrdb" cdrtable="cdr" astdbuser="asteriskuser" astdbuserpass="asteriskuserpassword" for i in `find /var/spool/asterisk/monitor -type f -name "*.wav"` do if [ -e "$i" ]; then file=`basename "$i" .wav`; dir=`dirname "$i"`; lame -h -b 192 "$i" "$dir/$file.mp3"; rm -f "$dir/$file.wav"; mysql --user="$astdbuser" --password="$astdbuserpass" --database="$cdrdb" --execute='UPDATE '$cdrtable' SET recordingfile="'$file'.mp3" WHERE recordingfile="'$file'.wav";'; fi done 


The process is slow, it is better to run at night. Now our place is much more economical, listening to reports does not require QuickTime and the file is played to us by the browser through the HTML5 tag.

Source: https://habr.com/ru/post/258445/


All Articles