4

I was looking for a way to record in mpeg-4 with the ability to pause and resume but it seemed like there isn't one. So I decided to record in raw wave format and convert to .m4a. Is there a way I can convert .wav file to .m4a in android. I've looked around for mencoder port for android but found none, there were some post's about porting ffmpeg to android on linux but its not quite clear how i can use it in android to merge two .m4a files or convert .wav file to .m4a.

Pannu
  • 2,091
  • 2
  • 20
  • 24

2 Answers2

4

There are two possible things you could mean by "merging" the audio files, and two different ways to go about it using ffmpeg:

Putting two separate audio tracks into the same file

Like on a DVD where there's a normal audio track, and another audio track like commentary or a dubbed language. To do this with ffmpeg and two m4a files, use the command ffmpeg -i in-1.m4a -acodec copy out.m4a -i in-2.m4a -acodec copy -newaudio. The order of options is important, so don't mess with it.

Concatenating two audio files

Making the second file play immediately after the first. The easiest way to do this is to use a program designed for audio manipulation like SoX. It appears that there is a recent but unstable Android port of SoX.

If you want to do the concatenation strictly in ffmpeg, then you can do the following:

  1. ffmpeg -i in-1.m4a -f s16le in-1.raw
    This outputs the audio file to raw PCM. You need to pay attention to the sampling rate and the number of channels, because this information will not be stored in the raw PCM file and you will need it later. ffmpeg will show you the audio stream information as it is re-encoding. It will look something like Audio: aac, 44100 Hz, stereo, s16, 63 kb/s, which means that your sampling rate is 44100 and your number of channels is 2 (stereo). Most often, you will be dealing with a sampling rate of 22050, 44100 or 48000; and 1 ("mono"), 2 ("stereo"), or 6 ("5.1") channels.

  2. ffmpeg -i in-2.m4a -f s16le in-1.raw
    Now decode the second file to raw PCM.

  3. ffmpeg -f s16le -ar 44100 -ac 2 -i 'concat:in-1.raw|in-2.raw' -acodec libfaac out.m4a
    Again, order of options is very important. Change the sampling rate (-ar) and channels (-ac) options as appropriate for your input.

Community
  • 1
  • 1
stharward
  • 347
  • 1
  • 6
  • Aren't those commands suppose to be run in command line. I ported `ffmpeg` to android now I need to know how i can do the same using `JNI` or something – Pannu Nov 09 '11 at 10:23
  • @Pannu: well, you could just execute ffmpeg as an external program from Java. If you want to call libavcodec using JNI, I can't help you with example code because I don't know Java and I barely know the libavcodec API. But from the little I've done with audio processing, concatenating two audio files is really easy because it's just appending one array to another, and then feeding the result to the encoder and muxer. – stharward Nov 09 '11 at 13:41
1

Assuming that you build ffmpeg with the relevant formats and codecs, converting wav to m4a is as simple as:

ffmpeg -i in.wav out.m4a

If you're doing audio work, you might have better luck with SoX.

blahdiblah
  • 30,909
  • 18
  • 92
  • 149