Monday, September 10, 2012

AsteriskNOW 2.0.2 64-bit WAV to MP3

I recently revamped our PBX and installed AsteriskNOW 2.0.2 64-bit (http://www.asterisk.org/downloads).  There were lots of tutorials online dealing with setup and i was able to get the server and 15 Polycom 501 handsets setup in about 3 days.  Because i am often traveling or away from my desk for long periods of time I rely heavily on the voicemail to email feature. The problem was that the .wav files were not opening on my Android Phone. After doing some research i found out that the file was in an odd format (wav|gsm).  I had two options at this point:  I could either pay a couple bucks for an android app that would play this format or i would reconfigure my server to use another format like mp3 for free.

Being a self proclaimed saver and always looking for a challenge, I decided to search the internet for tutorials.  I found a great tutorial outlining the process found here:   http://pcaddicts.ca/rc/2011/06/26/asterisknow-freepbx-mp3-voicemail/.

As i started through the process i quickly realized that this tutorial was written for an x86 setup and not an x64 setup.  Below is the steps i used on my x64 CentOS 5.8 final AsteriskNOW 2.0.2 64-bit setup.

1) Login into your box

2) Goto your home directory
cd /home/

3) Download the lastest version of lame from here http://pkgs.repoforge.org/lame/
wget http://pkgs.repoforge.org/lame/lame-3.97-1.el5.rf.x86_64.rpm

4) install rpm
rpm -Uvh lame-3.97-1.el5.rf.x86_64.rpm

5) Create the following script as /usr/sbin/sendmp3voicemail.pl
nano /usr/sbin/sendmp3voicemail.pl
#!/usr/bin/perl
open(VOICEMAIL,"|/usr/sbin/sendmail -t");
open(LAMEDEC,"|/usr/bin/dos2unix|/usr/bin/base64 -di|/usr/bin/lame --quiet --preset voice - /var/spool/asterisk/tmp/vmout.$$.mp3");
open(VM,">/var/spool/asterisk/tmp/vmout.debug.txt"); 
my $inaudio = 0;
loop: while(<>){
  if(/^\.$/){
    last loop;
  }
  if(/^Content-Type: audio\/x-wav/i){
    $inaudio = 1;
  }
  if($inaudio){
    while(s/^(Content-.*)wav(.*)$/$1mp3$2/gi){}
    if(/^\n$/){
      iloop: while(<>){
        print LAMEDEC $_;
        if(/^\n$/){
          last iloop;
        }
      }
      close(LAMEDEC);
      print VOICEMAIL "\n";
      print VM "\n";
      open(B64,"/usr/bin/base64 /var/spool/asterisk/tmp/vmout.$$.mp3|");
      while(){
        print VOICEMAIL $_; 
 print VM $_; 
      }
      close(B64);
      print VOICEMAIL "\n";
      print VM "\n";
      $inaudio = 0;
    }
  }
  print VOICEMAIL $_;
  print VM $_;
}
print VOICEMAIL "\.";
print VM "\.";
close(VOICEMAIL);
close(VM);

#CLEAN UP THE TEMP FILES CREATED
#This has to be done in a separate cron type job
#because unlinking at the end of this script is too fast,
#the message has not even gotten piped to send mail yet


6) Make the script executable
chmod 775 /usr/sbin/sendmp3voicemail.pl

7)Change default voicemail format to uncompressed WAV (which will be converted to MP3) and set mailcmd script.
FreePBX -> Voicemail Admin -> Settings

Changes:

format= wav
mailcmd= /usr/sbin/sendmp3voicemail.pl


8) Restart Asterisk and test
service asterisk restart

No comments:

Post a Comment

Phoenix

I am resurrecting this tech blog for notes related to Azure Logic Apps with SAP.