Main Contents

Convert all your Asterisk .wav recordings to .mp3

October 24, 2008

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 if you are using a ‘call-genie’ setup. I have also written scripts to convert your .g729/.gsm/etc => .mp3 so if you need one of those scripts leave a comment and I will email it to you (20090908 UPDATE: Check end of post for .g729 to MP3 file conversion script)

Installation Instructions:
1. Download the script to your server.   wget http://www.thiscoolsite.com/scripts/convertwav/convert_recordings.sh  or download it here.

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.
Leave a comment if you need further assistance.

#!/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

As always, leave a comment if you need help or clarification.
-Gregg

Filed under: Asterisk VoIP, Linux, Tech | Comments (22)

22 Comments

  1. Daniel Greig November 12, 2008 @ 12:50 am

    I am interested in the g729 convert to mp3 script. Please email it to me.

  2. Steve Edwards December 4, 2008 @ 8:10 pm

    A few suggestions for your script. Some are just my “style,” some make it more efficient and robust. Unfortunately, it looks like your text box is going to mung it severely. Oh well…

    #!/bin/bash

    # Let the user specify the directory on the command line or default to
    # your directory
    recorddir=”${1:-/var/spool/asterisk/monitor}”

    # The “cd” should be outside the loop. Beside efficiency, a “relative”
    # recorddir inside the loop would fail.
    cd $recorddir

    # You don’t need to invoke ls in backticks, the shell will expand the
    # wildcard by itself.
    # Also, since we have already changed our current directory, we don’t
    # need to specify it here.
    # Also, I hate single character variables. They convey no information
    # and are hell to search for.
    for file in *.wav
    do

    # Make sure the .wav is at the end of the file name so you don’t get
    # tripped up by something like monkeys.waving.wav
    # Using $() instead of backticks can make nesting commands (when
    # you need it) easier. Also, backticks can get confused with
    # single-quotes in some fonts.
    # Enclosing the variable in double-quotes will handle those pesky
    # spaces within file names.
    mp3=$(echo “$file” | sed ’s/.wav$/.mp3/g’)

    # An alternative would be to use the basename command
    mp3=$(basename “$file” .wav).mp3

    # You don’t need to extract the date from the output of ls. Beside
    # efficiency, you could get hosed by environment variables.
    touch –reference “$file” “$mp3″
    done

    # I like to end all of my code with a comment so I know I’m really at
    # the end of the file.
    # (end of convert_recordings.sh)

  3. David Herselman December 15, 2008 @ 10:02 am

    Don’t really need mlame if you are only interested in converting wav -> mp3:

    /var/spool/asterisk/monitor/convert_mp3.sh:
    #!/bin/bash
    recorddir=”${1:-/var/spool/asterisk/monitor}”
    cd $recorddir;
    for file in *.wav; do
    mp3=$(basename “$file” .wav).mp3;
    nice lame -b 16 -m m -q 9 –resample 8 “$file” “$mp3″;
    touch –reference “$file” “$mp3″;
    chown apache.apache “$mp3″;
    chmod 600 “$mp3″;
    rm -f “$file”;
    #mv “$file” /converted;
    done

    Then simply call it as part of your nightly cleanup tasks but remember to redirect output:

    /var/spool/asterisk/monitor/convert_mp3.sh &> /dev/null

  4. Thekkamalai January 8, 2009 @ 1:13 am

    I need to convert .g729 and .gsm files to .mp3.

    please email the script to me.

  5. Rodrigo Martin January 27, 2009 @ 8:04 am

    I need to convert .wav files to .gsm in Asterisk,someone knows how to do it?
    thanks

  6. Neeraj Chand February 3, 2009 @ 8:23 pm

    Hi Gregg,

    I’m currently recording in .g729 format, could you please email the .G729 => mp3 shell script?
    Cheers!

  7. MichaelD February 7, 2009 @ 1:11 pm

    Do you happen to have an script that would convert voice mail .wav to .mp3. I would appreciate your input.

    Regards,

    Michael

  8. Enzo February 12, 2009 @ 8:37 am

    Hi Gregg,

    Its a cool script!

    Maybe you can help me, I receive message when running the script:

    “lame: -e emp must be n/5/c not sample”

    And when I remove the “–resample” option this message came:
    “Unsupported data format: 0×0031″

    Any ideas,

    Manty Thanks and Regards,

    Enzo

  9. Anderson Nuernberg March 23, 2009 @ 12:52 pm

    I need to convert .gsm files to .mp3.

    please email the script to me.

    thanks.

  10. Ramy June 2, 2009 @ 6:07 pm

    Hi

    Interesting site and Thank you for the tips.
    I understand from the comments that all those files can be CONVERTED to mp3.

    Is there a way to CREATE the file in MP3 in Asterisk instead of first creating them in .wav and the converting them to .mp3?

    Issue is that i generate a lot of files. aprox 10,000 files daily. converting them, will keep the original stamp, but sorting them out afterward by the “date created” column, would show the date the file was converted, and not the original date for the original file created.

    Example:
    agent_telephone_datetimestamp.wav
    converts to
    agent_telephone_datetimestamp.mp3

    but listing them in a window environment, the
    “date created” would show the conversion date not the file original date.

    This problem would be resolved if the Asterisk original file would be recorded in mp3 i presume, as the date would be the same in both cases.

    Thank you

  11. Ash June 2, 2009 @ 9:08 pm

    Very useful info. I understand to reduce cpu load while a call is in process, it would be good to record in say g729 or gsm format (assuming both are supported by voip service provider). Once could later use your script to convert a file to say mp3 format. Is this correct?

    I need to convert g729 and gsm to mp3 format. I would be great if you could send me the script for this, thanks in advance.

    ash

  12. Wilmar June 15, 2009 @ 1:26 am

    This is great info.

    I also have the need to convert gsm to mp3 format. I would appreciate it if you can also send the script to me. Thanks in advance.

  13. Roger August 6, 2009 @ 5:33 pm

    I need to convert gsm to mp3 format. I would appreciate it if you send the script to me. Thanks in advance.

  14. Alex August 9, 2009 @ 10:40 am

    Hi Gregg

    Nice script, can you send me the g729 > mp3 script pls.

    Thanks

  15. ALEX M August 13, 2009 @ 6:11 pm

    I ve an problem when try run scrip with crontab the mp3 file is 0 bytes..

    regards¡¡

  16. throxies September 1, 2009 @ 4:21 pm

    I followed your script Greg and it worked fine when running the script manually but when I cron it, converted mp3 file will have 0kb.

    #crontab -e
    ########## here’s the cron script running every minute

    */1 * * * * root /home/convert_recordings.sh 2>&1 >> /script_output.log

    Please help me…. Badly need to have it in crontab.

  17. Jim September 8, 2009 @ 5:23 am

    Hi Gregg,

    Please can you send me the script to convert between g729 and mp3.

    Thanks

  18. networ September 9, 2009 @ 4:21 am

    Hi guys,

    look here http://www.izlin.net/how-to-convert-wav-g729-to-mp3-under-linux-for-asteriskelastixtrixbox/ – here you can find solutions for converting G729 to mp3.

  19. HunterXHunter September 16, 2009 @ 11:38 am

    Please can you send me the script to convert between g729 to mp3.

    Thank you.

  20. HunterXHunter September 16, 2009 @ 11:39 am

    Greeting!!

    Please can you send me the script to convert from g729 and mp3. I really need it .

    Thanks

  21. HunterXHunter September 16, 2009 @ 12:10 pm

    Greeting!!

    Please can you send me the script to how convert from *.g729c to *.mp3 . I really need it. I’m having great difficulty in converting on these.

    Thanks

  22. Phil January 15, 2010 @ 1:18 pm

    You site has helped me a bunch…Once question though..When im running your script im getting:

    ./convert_recordings.sh: line 19: /bin/ls: Argument list too long

    can you please help me with this?

Leave a comment