Aquarionics

Wednesday 4th August 2004

How to convert AMR files to MP3

The Nokia 7600 (And - I'm told - some Erikson phones) record in a format called "AMR". After a little digging, I was able to convert these to a usable format (MP3) on my Linux box. This is how I did it:

You will require:

  • sox (via your package manager)
  • lame (Or some other encoder, see below)
  • The 3GPP reference converter files

The first is easy, sensible people should be able to apt-get install sox or whatever. The second may require some compiling and following instructions, the third is more complicated. The 3GPP FTP site appears to move around a bit, so links go outdated. I got it from here, but you might have more luck just searching google for "26104-520.zip".

You need to get that file, unzip it, unzip the zip inside it (It all unzips into the current directory, so you'd best put it somewhere on it's own first) and you'll get a series of c & header files. If you're using Linux (and the rest of this assumes you are) cp makefile.gcc Makefile and make it. xa.bi's site (Where I found the decoder existed) says you should do some playing with the files, but I've no idea what difference it makes. His site, btw, contains a much more advanced AMR converter than this, but I didn't need that kind of complexity. Put the resulting executables (encode and decode) somewhere useful, and remember where it is.

This is the shell script I used to make it go, you could edit it, or write your own.

#!/bin/bash

# By Aquarion - Aquarion@Aquarionics.com
# Do what you want with it, it's not rocket science.

##Change these:
#Wherever you put encode and decode when you compiled them:
CODEC=/home/aquarion/opt/mms

#Temporary directory. Chances are you've already got this set
#TEMP=/tmp/

#Where do the final MP3s go?
FINAL=mp3/

for file in *.amr; do 
	FILE=`echo $file | sed -e "s/.amr//"`; 
	echo -n "$FILE [AMR] -> [$TEMP]"
	$CODEC/decoder $file $TEMP/$FILE.$TEMP > log.std 2> log.err; 
	echo -n " -> [WAV] "
	sox -r 8000 -w -c 1 -s $TEMP/$FILE.$TEMP -r 16000 \
		-w -c 1 $TEMP/$FILE.wav > log.std 2> log.err; 
	echo -n " -> [MP3] "
	lame $TEMP/$FILE.wav $FINAL/$FILE.mp3 --silent \
		--tt $FILE --ta $USER --tl Aquarionics --ty `date +%y`
	echo  " :-) "
	rm $TEMP/$FILE.wav;
	rm $TEMP/$FILE.$TEMP;
done

That script as written takes all .amr files in the current directory, turns them into .mp3 files and puts them in a subdirectory called "mp3" (which needs to exist before you run the script.)

And there you have it. If you want to convert them to oggs, you'll need to muck around with the "lame" line of the script a bit. Oh, and you'll probably want to change the album away from "Aquarionics" too.

Those who spoke on this:

gravatar image

asjkgcc:

2005-05-14 09:52 40 wks after the Original Article

who gives a fuck about linix. you fuck head!

Comment Link Reply to asjkgcc

gravatar image

Aquarion:

2005-05-14 10:23 31 mins after asjkgcc
  1. “Linux”
  2. I do, it being my primary operating system
  3. You are a fool.

Comment Link Reply to Aquarion

gravatar image

Athos:

2007-10-02 15:50 2 yrs after Aquarion

Hi Aquarion,
I have this problem: I dumped a VoIP call with AMR codec and tried to convert the AMR payload with the script but…. : invalid magic number.
Could you please help me?

Comment Link Reply to Athos

gravatar image

regularguy:

2005-12-01 00:22 29 wks after asjkgcc

Well, apparently, 2 more adults give one about Linux than give one about your opinion.

Halfwit.

Comment Link Reply to regularguy

gravatar image

zobi:

2006-03-30 13:47 46 wks after asjkgcc

hey goddam dickhead, sounds like you can’t even spit (non talking about merely speaking) your own language) go fuck yourself on your winblows box and let us work on some real engineering, the one that requires languages abilities as well.
f*k you

Comment Link Reply to zobi

gravatar image

NL:

2007-01-05 17:22 40 wks after zobi

Must you do that? I mean, really?

Replying in kind to trolls like that makes us decent Linux folks look just as bad as them.

Comment Link Reply to NL

gravatar image

jorma:

2005-05-23 13:55 42 wks after the Original Article

> xa.bi’s site (Where I found the decoder existed)
> says you should do some playing with the files,
> but I’ve no idea what difference it makes.

On my redhat 9 box decoder only segfaulted when makefile and decoder.c where not edited as told in the other article. After editing decoder worked fine.

Comment Link Reply to jorma

gravatar image

Tor:

2005-06-10 13:50 3 wks after jorma

Yeah, I also had to edit as per xa.bi’s description.
After that it worked fine (otherwise it would try to
decode from the #!AMR magic number at the start of the file (as when transfered from e.g. a S-E phone), that’s why it would crash).

I installed ’decoder’ as /usr/local/bin/amr_decoder and then used just this simple script to convert to .wav format (which can be played directly by e.g. mplayer):

#!/bin/sh
if test $# -ne 2; then

echo “Usage: $0 file.amr file.wav” exit 1
fi

/usr/local/bin/amr_decoder $1 $1.raw
sox -r 8000 -w -c 1 -s $1.raw -r 16000 -w -c 1 $2
rm $1.raw

Comment Link Reply to Tor

gravatar image

Max Littlemoore:

2005-07-22 12:08 50 wks after the Original Article

I am interested in going the other way.

I’m using Sarge with bluetooth and I have home grown samples I want to convert to amr to play on my my phone….

I am not too experienced with to codec tools so any help would be extremely appreciated.

A link perhaps? ... or maybe its simple and I’m missing the point.

Some help would be waxed lyrical about anyway.

Best Wishes

Comment Link Reply to Max Littlemoore

gravatar image

silvano:

2005-09-20 07:09 1 yr after the Original Article

the decoder always gives me the error

unknown TX frame type 8483

I tried various AMR input files, always with the same result. I’m using version 26073-600 of the
3GPP decoder.

Comment Link Reply to silvano

gravatar image

hpn:

2005-10-09 12:13 1 yr after the Original Article

nice information. :)

The amr to mp3 conversion worked for me, alright. But how about vice versa?

I tried using the binary ‘encoder’, but I ain’t sure what the mode and the other options needed there ought to be. Help appreciated.

Thanks in advance.

Comment Link Reply to hpn

gravatar image

xurizaemon:

2006-11-02 20:41 1 yr after hpn

i used ffmpeg to convert wav => amr

should be fine for mp3 => amr too

ffmpeg -i Fog.wav -acodec amr_nb -ac 1 -ar 8000 -r 8 fog.amr

Comment Link Reply to xurizaemon

gravatar image

anonymous:

2008-11-16 11:37 3 yrs after hpn

I guess mode specifies the bitrate/channels see:
http://en.wikipedia.org/wiki/Adaptive_multi-rate_compression

the VAD options you can specify at compile time are used to select the Voice activity detection options(See GSM):
http://en.wikipedia.org/wiki/Voice_activity_detection#Specific_VAD_implementations_in_real-world_systems

Comment Link Reply to anonymous

gravatar image

CodEnemy:

2005-10-18 01:01 1 yr after the Original Article

good contribution (after your little digging) however, i think you should have had explained what does the three packages do instead of exposing your magic script.. which didn’t make it easier at all!!!

Comment Link Reply to CodEnemy

gravatar image

Pug:

2005-12-08 14:30 7 wks after CodEnemy

Thanks for the script, and I disagree with CodEnemy – your magic script made everything work out fine. :)

Comment Link Reply to Pug

gravatar image

Gps:

2006-02-20 15:55 11 wks after Pug

Anything for windows…pls advice

thanks

Comment Link Reply to Gps

gravatar image

Tigggger:

2006-06-19 12:57 17 wks after Gps

Yes, For Windows I downloaded converter tool from miksoft.net. Works perfect and you can convert both ways amr to mp3 and mp3 to amr also various other sound and videaoformats.
Hope it can work for you too, Saludos desde Guatemala!

Comment Link Reply to Tigggger

gravatar image

linad:

2006-04-23 17:16 2 yrs after the Original Article

JESUS!! aint der no software to convert it :@

Comment Link Reply to linad

gravatar image

noonard:

2006-05-18 14:27 2 yrs after the Original Article

I’m having a nightmae implementing this… The problem is the 3GPP decoder – it works fine for certain AMRs but I get segfaults for others (and yep – that’s after the modifications suggested). Truly bizarre, especially seeing as both seem to be in the same format to begin with!

Any ideas?!

Comment Link Reply to noonard

gravatar image

A Nameless One:

2006-05-31 10:06 2 wks after noonard

Have you remove the optimizer? e.g. it shouldn’t say -O4 but -O2 or no -O at all.

Also the script isn’t space safe, So if th efiles got space in them that script will die. I really hope libavcodec will include this in the compiles per default in the future.

Comment Link Reply to A Nameless One

gravatar image

biagio:

2006-07-13 08:51 2 yrs after the Original Article

On Fedora Core also I needed to apply the change suggested by xa.bi otherwise I had segmantation fault. Morever I had to modify the script substituting $TEMP varaible with “raw” otherwise sox get troubles (it seems that recognizes the file format by extention).

Comment Link Reply to biagio

gravatar image

Vincenzo:

2006-10-24 16:00 15 wks after biagio

On MacOSX I had to use the modifications suggested by xabi.

Comment Link Reply to Vincenzo

gravatar image

humble:

2007-05-03 17:40 3 yrs after the Original Article

some small changes were done to

1. create .raw file as sox is more happy with

that

2. handle spaces in original filename.

#!/bin/bash

  1. By Aquarion – Aquarion@Aquarionics.com
  2. Do what you want with it, it’s not rocket science.

##Change these:

#Wherever you put encode and decode when you compiled them:
CODEC=/home/humble/mydata/mms

#Temporary directory. Chances are you’ve already got this set
TEMP=/tmp

#Where do the final MP3s go?
FINAL=mp3/

for file in *.amr; do

FILE=`echo $file | sed -e “s/.amr//” -e “s/ /_/g”`; echo -n “$FILE [AMR] -> [$TEMP]” $CODEC/decoder “$file” $TEMP/$FILE.raw > log.std 2> log.err; echo -n “ -> [WAV] ” sox -r 8000 -w -c 1 -s $TEMP/$FILE.raw -r 16000 -w -c 1 $TEMP/$FILE.wav > log.std 2> log.err ; echo -n “ -> [MP3] ” lame $TEMP/$FILE.wav $FINAL/$FILE.mp3—silent —tt $FILE—ta $USER—tl Aquarionics—ty `date +%y` echo “ :-) ” rm $TEMP/$FILE.wav; rm $TEMP/$FILE.raw;
done

Comment Link Reply to humble

gravatar image

m4kda:

2008-01-13 12:47 3 yrs after the Original Article

or if you want a quick solution and you are not too bothered about privacy, the online http://media-convert.com/ will convert to/from AMR format.

Comment Link Reply to m4kda

gravatar image

Cybermedievalist:

2008-06-26 01:41 4 yrs after the Original Article

If you just need to play the files, VLC media player for Linux does that just fine. I’m posting that here simply because it was one of the first links that came up for me when I was researching this. I just decided to give VLC a whirl though and it played the .amr fine with no conversion needed.

Comment Link Reply to Cybermedievalist

gravatar image

weakish:

2008-09-16 08:06 4 yrs after the Original Article

I have to change Makefile and decode.c before compiling as xa.bi said. Otherwise, I will get segment fault while decoding AMR files.

Comment Link Reply to weakish

gravatar image

Nathan Zadoks:

2008-10-20 11:01 4 yrs after the Original Article

I made one that uses piping and stuff so there’s no need for temp-files.
I needed to sudo apt-get install libsox-fmt-all first tho otherwise it said ‘sox soxio: Failed reading : unknown file type’
The code:

#!/bin/bash
if [ -z “$1” ]
then
echo Usage: $0
exit 1
fi
if [ ! -f “$1” ]
then
echo $1 doesn’t exist.
exit 2
fi
file “$1” | grep ’ Adaptive Multi-Rate Codec (GSM telephony)’
if [ $? -ne 0 ]
then
file “$1”
echo $1 isn’t an AMR file.
exit 3
fi
OUTDIR “$(dirname $1)”
OUTFILE “$(basename $1 .amr)”
OUTPUT=${OUTDIR}/${OUTFILE}.wav
if [ -f “$OUTPUT” ]
then
echo ${OUTFILE}.wav already exists.
exit 4
fi
amrnb-decoder “$1” /dev/stdout|sox -t raw -r 8000 -w -c 1 -s – -r 16000 -w -c 1

Comment Link Reply to Nathan Zadoks

gravatar image

Nathan Zadoks:

2008-10-20 11:12 11 mins after Themself

Woops, little fault in it.
Any updates will be posted on my site. (www.nathan7.eu)
This is the new version:

#!/bin/bash
if [ -z “$1” ]
then
echo Usage: $0
exit 1
fi
if [ ! -f “$1” ]
then
echo $1 doesn’t exist.
exit 2
fi
file “$1” | grep ’ Adaptive Multi-Rate Codec (GSM telephony)’
if [ $? -ne 0 ]
then
file “$1”
echo $1 isn’t an AMR file.
exit 3
fi
OUTDIR “$(dirname $1)”
OUTFILE “$(basename $1 .amr)”
OUTPUT=${OUTDIR}/${OUTFILE}.wav
if [ -f “$OUTPUT” ]
then
echo ${OUTFILE}.wav already exists.
exit 4
fi
echo -n “$1”
echo -n >raw|sox>
echo “$OUTPUT”
COMMANDdec “amrnb-decoder $1 /dev/stdout”
COMMANDrec “sox -t raw -r 8000 -w -c 1 -s – -r 16000 -w -c 1 ${OUTPUT}”
echo ${COMMANDdec}|${COMMANDrec}
$COMMANDdec | $COMMANDrec

Comment Link Reply to Nathan Zadoks


Nicholas 'Aquarion' Avenell is a web developer in London, you can find out more about him or how to get in touch.

There are more Articles, Projects, Journal Entries, Photographs and things that defy description here, too.

If you're looking for something specific, there are Calendar & Category -based lists of everything.

And if you want to follow stuff that appears here, try a Syndication Feed, or the generic Feed of everything.

Aquarionics on Livejournal


Aquarion [updating]
Twitter last updated


More Articles:

[RSS Icon]
[ESF Icon]
[CDF Icon]

That which is relevant:


Explain Ads
© 2000 to 2008 inclusive Nicholas Avenell
All comments are the property of their creators, published with permission
(Unless otherwise indicated, the opinions and sentiments expressed on this site are those of the author and not of any organisation of which he is an affiliate, including his employer. Caveat Lector, E&OE. sigh)
0.346 seconds, 10 queries, 2.78Mb on Tue, 02 Dec 2008 06:49:26 +0000
Generated by Epistula Version 2.0.3