Showing posts with label convert audio. Show all posts
Showing posts with label convert audio. Show all posts

Monday, August 5, 2013

Convert all your Asterisk .wav recordings to .mp3

I have had many jobs (consulting and Salary) that have involved using Asterisk in a “Call-Center” environment. Whenever an agent would make a sale, the call would need to be recorded for verification purposes.  Now I could talk about Asterisk and call-recording for pages upon pages, but I will be focusing on large .wav to .mp3 conversion jobs on the Linux/Asterisk server itself.  First off, Asterisk can record calls using several different methods.  The first method being that you setup a feature called Automon in /etc/asterisk/features.conf and use *1 (or whatever you specify) to record a call in progress.  The second method is recording every call that comes in through a specific DID or enters a specific queue.  Another method is to setup a ‘call genie’ that you conference your phone to and it records the entire call-bridge.  There are pros and cons to every type of recording method and format.  You can use GSM, wav, g729, etc. to record calls.  Not matter what method you use, the simple script I wrote will convert your .wav files to .mp3 and also retain the timestamp of when the file was initially created (recorded). The options I use for mlame are what makes this script retain its awesome-ness over time.  The options allow the final-result mp3 to be compressed and compatable with Asterisk.  This means that you can playback your .mp3 files that you converted over Asterisk.
Installation Instructions:
1. Copy the below script to your server and save it as convert_recordings.sh. 
 ========================================
#!/bin/bash
#Make sure to install Lame and copy mlame to your .wav dir
#If you specify your own filenames, use NO spaces.

recorddir="/var/spool/asterisk/monitor"
for i in `ls $recorddir/*.wav`; do
cd $recorddir   
$recorddir/mlame -f -o "-b 16 -m m -q 9 --resample 8" $i
date=`ls -l $i | awk '{print $7,$6,2008,$8}'`
mp3=`echo $i | sed 's/.wav/.mp3/g'`
touch -d "$date" $mp3

#Uncomment the below line if you want your .wavs moved to a different directory.
#This way you can review the timestamps and mp3 files before deleting the .wav
#mv $i /wav_recordings
done
============================
2. Put the script anywhere you wish, I tend to leave it in my home directory
3. chmod +x convert_recordings.sh
4. You need to install lame.  Get the file from the lame site.
5. Extract the tarball.  For Example…    tar -zxvf lame-398-2.tar.gz
6. cd lame-398-2
7.   ./configure
8. make
9. make install
10. Now copy the mlame file from lame-398-2/misc/mlame to your .wav directory /var/spool/asterisk/monitor (or wherever your .wavs are)
11. chmod +x mlame
12. Edit the convert_recordings.sh  recorddir variable to match your .wav directory path (no trailing ‘/’). The default directory is set to /var/spool/asterisk/monitor
13. Run the script by navigating to the directory and   ./recordings_convert.sh
You should see it take a few seconds for each file and go on.  I usually test it out by letting it convert a few files then Cntrl + C and ls -l the directory to check the filesizes and timestamps.  There is a commented out section at the bottom of recordings_convert.sh that moves the .wav files to a separate directory (easier to check if it worked) and in my case, I keep the old files for a month or so for job security reasons.  If you lose a bunch of verification recordings, you are going to have major problems.
Cron it, at it, and happy converting.
__________________________________
Code for .g729 to MP3 script.  Very dirty, but works, please test on a small group of test files first.


#!/bin/bash
#Author: Gregg Hansen  20080414
#Used to convert from .g729 -in and -out files to .mp3
#Run on Recordings server

for i in `ls /ramdiskunload/*.g729`;
do
#convert all .g729 to wav first, then soxmix
DST=`echo $i | sed 's/.g729/.wav/g'`
asterisk -rx "file convert $i $DST"
rm -f $i
mv $DST /recordings
done
#all files should now be in the /recordings directory
#use soxmix and mlame to convert them to one file => mp3
for j in `ls /recordings/*-in.wav`;
do
INFILE=`echo $j | sed 's/-in/-out/g'`
MIXED=`echo $j | sed 's/-in//g'`
soxmix $j $INFILE $MIXED
rm -f $j
rm -f $INFILE
/root/test/mlame -f -o "-b 16 -m m -q 9 --resample 8" $MIXED
done


-----------------------------------------------------------------

Code for .g729 to wav script.  Very dirty, but works, please test on a small group of test files first.
 
#!/bin/bash
#Author: Gregg Hansen  20080414
#Used to convert from .g729 -in and -out files to .mp3
#Run on Recordings server
for i in `ls /root/mix/*.gsm`;
do
#convert all .g729 to wav first, then soxmix
LDST=/root/mix/recordings
DST=`echo $i | sed 's/.gsm/.wav/g'`
asterisk -rx "file convert $i $DST"
rm -f $i
mv $DST $LDST
done
#all files should now be in the /recordings directory
#use soxmix and mlame to convert them to one file => mp3
for j in `ls /root/mix/recordings/*-in.wav`;
do
INFILE=`echo $j | sed 's/-in/-out/g'`
MIXED=`echo $j | sed 's/-in//g'`
soxmix $j $INFILE $MIXED
rm -f $j
rm -f $INFILE
#/root/lame/misc/mlame -f -o "-b 16 -m m -q 9 --resample 8" $MIXED
done
 

Convert audio files for use in Asterisk

Convert files from the CLI

You just recorded a fabulous audio file to use as you main voice menu. Then you realize that Asterisk does not use WAV format audio for the Playback or Background applications. So what do you do? How can you convert your WAV files into GSM files that still have good sound quality? (This is partially false, Asterisk can play anything it has a format and codec for, including some wav files. See below.)

Note the differences!

gsm: raw gsm encoding, good for VoIP
wav: MS wav format, 16 bit linear
WAV: MS wav format, gsm encoded (wav49)


Converting to sln format

Starting from Asterisk 1.2.0, the .sln (SLINEAR) format seems to be the preferred format.
To convert wav file to sln, use the following command:

sox foo-in.wav -t raw -r 8000 -s -w -c 1 foo-out.sln

Note that sox v14.3.0 and above (installed in Ubuntu 9.10), the -w option has changed to -2

sox foo-in.wav -t raw -r 8000 -s -2 -c 1 foo-out.sln

If you have a directory full of .wav files to convert, try this command. It uses sed to automatically rename the files with the .sln extension (assuming incoming wav files at a sample rate other than 8khz.)

for a in *.wav; do sox "$a" -t raw -r 8000 -s -w -c 1 `echo $a|sed "s/.wav/.sln/"` resample -ql; done

Converting your WAV files to good GSM files is easier than you might think if you have the program Sox installed (on Debian systems the libsox-fmt-gsm package is required in addition to sox). From the shell prompt, enter this command:

sox foo.wav -r 8000 foo.gsm resample -ql

and hit the <ENTER> key. Note that the sox option '-ql' (lower case L) modifies the resample option. It is not a number one (1). In a few moments you will have a new GSM format file in the same directory as the original WAV file. In this example "foo.wav" is your main voice menu audio file in WAV format, and "foo.gsm" is the same file converted to GSM format. If you wanted to, you could use "main-voice-menu.gsm" as the name in place of "foo.gsm": what matters here is the second file name you use in this command ends in ".gsm".

If your WAV file was in stereo, add the -c1 option to convert to mono, or the output will sound very strange.

sox foo.wav -r 8000 -c1 foo.gsm resample -ql

You may get better results if you record your WAV file in 16 bit 8000 Hz mono and then run

sox foo.wav foo.gsm

If you have multiple WAV files in one directory and you want to convert them all, use this command:

for a in *.wav; do sox "$a" -r 8000 -c1 "`echo $a|sed -e s/wav//`gsm" resample -ql; done

You can also put a bash script in /usr/bin and name it wav-gsm-convert. The content can be like this

  1. !/bin/bash
s=`echo $1| sed -e's/\.wav//'|xargs -i{} echo {}.gsm`
sox -t wav $1 -r 8000 -c1 -t gsm $s resample -ql


Next, move your new foo.gsm file to the directory: /var/lib/asterisk/sounds

Now you can easily use the applications Playback and Background in your extensions.conf file to play your fabulous main voice menu. For example:
exten => s,1,Background(foo)
or
exten => s,1,Background(main-voice-menu)
or
exten => s,1,Playback(foo)
or
exten => s,1,Playback(main-voice-menu)

Playing .sln files from the command line

You can play sln files using sox from the command line (play is part of sox):

play -t raw -r 8000 -s -w -c 1 file.sln

Using WAV files

Asterisk has codecs for wav (pcm), gsm, g729, g726, and wav49, all of which can be used for Playback and Background. However, Asterisk does not understand ADPCM WAV files. To convert your WAV files to a format which Asterisk can understand, use the following command:

sox foo-in.wav -r 8000 -c 1 -s -w foo-out.wav resample -ql

Note that sox v14.3.0 and above (installed in Ubuntu 9.10), resample is no longer used, remix is used or leave it as:

sox foo-in.wav -r 8k -c 1 -s -w foo-out.wav

All so if your using sox v14.3.foo and above and you are getting check_header errors with play back try this:

sox foo-in.wav -r 8k -c 1 -e gsm foo-out.wav

This also works converting mp3, just make sure you have libsox-fmt-mp3 installed.

Normalizing volume and reducing volume fluxuations

Using the sox command's "compand" filter, you can reduce or eliminate flutters in volume level, and you can normalize the volume of your sound files. This is called dynamic range compression. The effect is desirable for a PBX system where changes in volume, or voices that are too quiet to understand would be considered unprofessional. It is important to perform the compand effect BEFORE you resample it as to preserve as much quality as possible.

An example command to perform some appropriate dynamic range compression and normalization is shown below:

sox "foo-in.wav" -r 8000 -c1 "foo-out.gsm" lowpass 4000 compand 0.02,0.05 -60,-60,-30,-10,-20,-8,-5,-8,-2,-8 -8 -7 0.05 resample -ql

In this example, foo-in.wav is a 16-bit mono 44khz uncompressed pcm WAV file.

Converting to a CD writable format

So, you've decided to do your call recording in GSM format as you don't care about quality and you don't want to stuff your disks full, but how do you write that file to an audio CD to send to somebody who wants to listen to the call?

sox infile.gsm -r 44100 -a outfile.wav