JTK – Notes and Ramblings Things we have found …

8/7/2011

AsteriskNOW and mp3 voicemail

Filed under: General,Router/PC Config — taing @ 2:57 pm

AsteriskNOW is very easy to setup for voicemail to email. The problem is the formats Asterisk likes for voicemail are not ideal choice to be received by todays mobile devices. wav49 and gsm are not support by many of the mobile devices that might receive the email. wav is uncompressed and therefore huge and unwieldy for email. I found an nearly ideal solution at http://bernaerts.dyndns.org/linux/179-asterisk-voicemail-mp3.

I’ll repeat the core of the article here.

You will need to be sure you have lame and dos2unix installed on your system. For my AsteriskNOW distro dos2unix was already present and a yum install lame solved the rest.

The main script is copied to /usr/sbin/sendmailmp3. Be sure to make the script executable.

You will need to add/modify the mailcmd line in /etc/asterisk/voicemail.conf.

mailcmd=/usr/sbin/sendmailmp3

I found it necessary to also change the format line in /etc/asterisk/voicemail.conf

format = wav|gsm|wav49

Asterisk will save the voicemail in all three formats but the first one listed is the format emailed. By putting wav first you send the uncompressed wav file to our new script and make the re-encoding process simpler.

The main script:

#! /bin/sh
# save the current directory
CURDIR=`pwd`

# create a temporary directory and cd to it
TMPDIR=/tmp/mail`head -1 /dev/urandom | od -N 1 | awk '{ print $2 }'`
if [ ! -d $TMPDIR ] ; then mkdir $TMPDIR ; fi
cd $TMPDIR

# dump the stream to a temporary file
cat >> stream.org

# get the boundary
BOUNDARY=`grep "boundary=" stream.org | cut -d'"' -f 2`

# cut the file into parts
# stream.part - header before the boundary
# stream.part1 - header after the bounday
# stream.part2 - body of the message
# stream.part3 - attachment in base64 (WAV file)
# stream.part4 - footer of the message
awk '/'$BOUNDARY'/{i++}{print > "stream.part"i}' stream.org

# cut the attachment into parts
# stream.part3.head - header of attachment
# stream.part3.wav.base64 - wav file of attachment (encoded base64)
sed '7,$d' stream.part3 > stream.part3.wav.head
sed '1,6d' stream.part3 > stream.part3.wav.base64

# convert the base64 file to a wav file
dos2unix -o stream.part3.wav.base64
base64 -di stream.part3.wav.base64 > stream.part3.wav

# convert wav file to mp3 file
# -V 2 is a good compromise between quality and size for voice audio files
lame -V 2 stream.part3.wav stream.part3.mp3

# convert back mp3 to base64 file
base64 stream.part3.mp3 > stream.part3.mp3.base64

# generate the new mp3 attachment header
# change Type: audio/x-wav to Type: audio/mpeg
# change name="msg----.wav" to name="msg----.mp3"
sed 's/x-wav/mpeg/g' stream.part3.wav.head | sed 's/.wav/.mp3/g' > stream.part3.mp3.head

# generate first part of mail body, converting it to LF only
mv stream.part stream.new
cat stream.part1 >> stream.new
cat stream.part2 >> stream.new
cat stream.part3.mp3.head >> stream.new
dos2unix -o stream.new

# append base64 mp3 to mail body, keeping CRLF
unix2dos -o stream.part3.mp3.base64
cat stream.part3.mp3.base64 >> stream.new

# append end of mail body, converting it to LF only
echo "" >> stream.tmp
echo "" >> stream.tmp
cat stream.part4 >> stream.tmp
dos2unix -o stream.tmp
cat stream.tmp >> stream.new

# send the mail thru sendmail
cat stream.new | sendmail -t

# go back to original directory
cd $CURDIR

# remove all temporary files and temporary directory
rm -Rf $TMPDIR

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress