Search code examples
androidaudioandroid-mediacodecpcm

Mux video with my own audio PCM track


Using Android MediaMuxer, what would be a decent way to add my own PCM track as the audio track in the final movie?

In a movie, at a certain time, I'm slowing down, stop, then accelerate and restart a video. For the video part, it's easy to directly affect the presentation time, but for audio, there is a chunk-by-chunk process that makes less intuitive to handle a slow down, a stop and a start in the audio track.

Currently, when iterating through the buffer I've received from the source, to slow down the whole track I do:

// Multiply by 3 the presentation time.

audioEncoderOutputBufferInfo.PresentationTimeUs = 
              audioEncoderOutputBufferInfo.PresentationTimeUs * ratio);

// I expand the sample by 3. Damn, just realized I haven't
// respected the sample alignment but anyway, the problem is not about white noise...

encoderOutputBuffer = Slowdown(encoderOutputBuffer, 3); 

// I then write it in the muxer 
muxer.WriteSampleData(outputAudioTrack, encoderOutputBuffer, audioEncoderOutputBufferInfo);

But this just doesn't play. Of course, if the MediaFormat from the source was copied to the destination, then it will have a 3 times shorter duration than the actual audio data.

Could I just take the whole PCM from an input, edit the byte[] array, and add it as a track to the MediaMuxer?


Solution

  • If you want to slow down your audio samples you need to do this before you encode them, so before you queue the input buffer of your audio codec. From my experience, the audio presentation timestamps are ignored by most of the players out there (I tried it with VLC and ffplay). If you want to make sure that audio and video stay in sync, you must make sure that you actually have enough audio samples to fill in the gap between to pts, otherwise the player will just start to play the following samples regardless of their pts. Furthermore you cannot just mux PCM samples using the MediaMuxer, you need to encode them first.