Extracting AMR Audio from Android 3GP Files
03/18/2009
The Android MediaRecorder currently records audio in AMR format and stores that within a 3GP container which provides metadata for the recording. Thanks to Sebastian Annies at Core Media it is possible to extract the AMR audio from the 3GP file using isobox4j. If you can’t play .amr files on your Windows machine, then I suggest K-Lite Codec Pack.
package com.benmccann.audio;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import com.coremedia.iso.FileRandomAccessDataSource;
import com.coremedia.iso.IsoFile;
import com.coremedia.iso.IsoOutputStream;
import com.coremedia.iso.mdta.Sample;
public class ThreegpReader {
private final static int ANDROID_AUDIO_TRACK_NUM = 3;
private final File inputFile;
public ThreegpReader(File file) throws FileNotFoundException {
this.inputFile = file;
}
public void extractAmr(OutputStream outputStream) throws IOException {
IsoFile iso = new IsoFile(new FileRandomAccessDataSource(inputFile));
iso.parse();
iso.parseMdats();
IsoOutputStream isoOutput = new IsoOutputStream(outputStream, false);
// write an AMR header
isoOutput.write(new byte[] {0x23, 0x21, 0x41, 0x4d, 0x52, 0x0a});
// write the audio content
for (Sample sample : iso.getTrack(ANDROID_AUDIO_TRACK_NUM)) {
sample.getContent(isoOutput);
}
isoOutput.flush();
isoOutput.close();
}
}
What data ends up in the outputStream? Is it wav data or still in the AMR format. I am having trouble finding details about the AMR format. My goal is to convert AMR to wave for sound processing.
It is not wav data. It’s still in AMR format, but no longer wrapped in a 3gp container. You can find details on the AMR format in the RFC: http://www.ietf.org/rfc/rfc3267.txt
Hi Ben,
I obtain some errors in these code’s row…
import com.coremedia.iso.FileRandomAccessDataSource;
import com.coremedia.iso.IsoFile;
import com.coremedia.iso.IsoOutputStream;
import com.coremedia.iso.mdta.Sample;
It says me that it cannot resolve the import…Do you have any idea of what it could be?
Sorry if it’s a really dumb question but I’m very very new to Android….
Thanks in advance for your time and your disponibility!!
Andrea.
Hi Andrea,
You do not have the isobox4j .jar file on your classpath.
Hi Ben,
Thanks for your replying….Do you have any idea of where I can find the isobox4j .jar ?
I’ve tried all your links but I cannot find it….
Could you please tell where I can get it ?
Thanks!
Hi Ben,
Your code now works!!!
I have the .amr file. Now my problem is how can I access to the sample audio in file .amr ?
I mean, I need to access to the audio samples to implemet an algorithm which discriminate if in my file there’s or not silence and I don’t know how to get the audio data.
Do you know if there is some specific function or library or .jar that could help me ?
Thanks!!!
Andrea
Andrea,
You should be able to build the isobox4j project using Maven. I’m not sure if they have a binary that they distribute.
Yes we have binaries and a maven repository: https://contributions.coremedia.com/svn/isobox4j/maven/repo/
HI Ben,
Thanks for such a nice article to extract AMR from Android 3gp file. Just one query from my side. Can I mix two Andriod 3gp file using the isoParser. Any code sample will be a great help
Thanks
Pramod
Phoenytunes.com
(Mobisoft Telesolutions Pvt Ltd)
Sebestian said there were binaries, but I can’t find any that include isobox4j.jar Could anyone post a link to just that?
I guess the only solution at the moment is to try to understand how maven works … Any reason the isobox.jar couldn’t be put up by itself. I’m not familiar with Maven and really want to devote my time on working out how to convert AMR to MP3 …
Many thanks in advance
CHEERS> SAM
Well I cracked and installed Maven. Turns out it wasn’t so hard for OSX:
http://maven2.darwinports.com/
In fact all I typed was:
sudo port install maven2
svn checkout https://contributions.coremedia.com/svn/isobox4j/trunk isobox4j
cd isobox4j/isoparser
mvn install
of course – then maven downloaded like a 100 different libraries …., but everything worked after that.
However I also subsequently found it seems like you can record audio on the android directly into the AMR format, so maybe I didn’t need isobox after all 🙂
I’m trying to create a 30 second sample mp4 sample from an mp4 file. Has anyone been able to use IsoParser to do this?
You can use simple way to extract AMR from 3GP container on Android.
I just describe it in our android-blog: http://android.amberfog.com/?p=181
just a quick note – the IsoParser code has been moved to GoogleCode here
http://code.google.com/p/mp4parser/source/checkout
Thanx mike for providing google code link .Its was quite useful for me
This put up has been of excellent aid for me! A lot of many thanks for posting it! Could you include me to your mailing checklist so I will be acknowledged of the most recent information? My e-mail is Holshouser104@live.com !
I know this post is ancient, but I’m hoping you’re still monitoring it. I’m having a problem running your code. The iso.getTrack call is failing. No matter what track I put in there, nothing works. The 3gp file was recorded on an Android phone, so I figured your code would work, but no joy. Have you any ideas? Thanks.