Converting Asterisk Music On Hold files using ffmpeg
Letting asterisk play native file seems to be the best practice for MOH (MusicOnHold).
Here we’ll convert all the mp3 files in one directory into two files. First one will be a WAV and the other a u-Law PCM all one step using ffmpeg.
Asterisk will choose the best file to play determined by the codec used on the channel.
This information is useful if you don’t want to use a third party application such as mpg123 or you find using other applications to play MOH causes problems because you run asterisk on FreeBSD or something other then Linux.
Requirements
ffmpeg can be found at http://ffmpeg.mplayerhq.hu/
It’s also in the FreeBSD ports
Configuration file
To make asterisk natively play files in /var/lib/asterisk/moh
This configuration will also play the files in random order.
Add the following to musiconhold.conf configuration file.
On FreeBSD /usr/local/etc/asterisk/musiconhold.conf
On Linux /etc/asterisk/musiconhold.conf
; Music on Hold
[default]
mode=files
directory=/var/lib/asterisk/moh
random=yes
Converting your MP3s
Command line for encoding all your mp3’s in some directory using ffmpeg to a mono WAV and PCM u-Law file of 64kbits/s at 8000Hz sample rate.
These files will sound fine down a phone line. But not at a disco!
The resulting WAV and PCM u-Law files will be larger then the original mp3, but that’s no big deal.
for f in `ls *.mp3` ; do FILE=$(basename $f .mp3) ; ffmpeg -i $FILE.mp3 -ar 8000 -ac 1 -ab 64 $FILE.wav -ar 8000 -ac 1 -ab 64 -f mulaw $FILE.pcm -map 0:0 -map 0:0 ; done
NOTE: If your filenames have spaces in them, you’ll need to modify the command to something like this:
IFS=$’\n’ ; for f in `ls -1 *.mp3` ; do FILE=$(basename “$f” .mp3) ; ffmpeg -i “$FILE.mp3” -ar 8000 -ac 1 -ab 64 “$FILE.wav” -ar 8000 -ac 1 -ab 64 -f mulaw “$FILE.pcm” -map 0:0 -map 0:0 ; done
With ffmpeg you should be able to convert almost any type of sound file. Just change the .mp3 extension to whatever sound file extension you have.
Moving the new files and restarting asterisk
move all the .wav and .pcm files to your moh directory and restart asterisk:
mv *.wav /var/lib/asterisk/moh/ ;mv *.pcm /var/lib/asterisk/moh/ ; asterisk -rx ‘restart now’
Michael Hodges