3

I have a video file .mp4 - video track only.

I'm using MediaExtractor and MediaMuxer to add audio file. this Works good.

On the processed file i want to add another audio track.

So i'm using again MediaExtractor and MediaMuxer to kind of copy the file, (Creating video and audio tracks, reading [extractor] and writing [muxer]). In addition i'm trying to add the second audio track to the muxer. but this throws the error Failed to add the track to the muxer.

in this link we can see that muxer does not support multiple tracks.

Code From the link:

// Throws exception b/c 2 audio tracks were added.
muxer = new MediaMuxer(outputFile, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
muxer.addTrack(MediaFormat.createAudioFormat("audio/mp4a-latm", 48000, 1));
try {
    muxer.addTrack(MediaFormat.createAudioFormat("audio/mp4a-latm", 48000, 1));
    fail("should throw IllegalStateException.");
} catch (IllegalStateException e) {
   // expected
}

Is there other way to do it ? Elegant way ?

BTW, i'm trying to avoid using 3rd parties - like ffmpeg or so.. But if would be my only solution...

--EDIT--

Relevant piece of my code

MediaMuxer muxer = new MediaMuxer(outputFile, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource(videoAndAudioFile);

for (int currTrackIdx = 0; currTrackIdx < extractor.getTrackCount(); currTrackIdx++) {
   MediaFormat trackFormat = extractor.getTrackFormat(currTrackIdx);
   tracksIdx.add(muxer.addTrack(trackFormat));
}

MediaExtractor extractor2 = new MediaExtractor();
extractor2.setDataSource(secondAudioFile);
MediaFormat trackFormat = extractor2.getTrackFormat(0);
tracksIdx.add(muxer.addTrack(trackFormat)); // Crashes here
Raziza O
  • 1,560
  • 1
  • 17
  • 37
  • Please paste relevant code. – Shadab Ansari Mar 23 '16 at 23:30
  • Copied the relevant code from the link – Raziza O Mar 23 '16 at 23:32
  • I was asking about your code. – Shadab Ansari Mar 23 '16 at 23:33
  • It is the same.. but i've added it now – Raziza O Mar 23 '16 at 23:40
  • Did you checked extractor2.getTrackCount() value? What is that ? – Shadab Ansari Mar 23 '16 at 23:44
  • 1 track. This is not the problem.. I'm getting the `trackFormat` properly, and adding it properly.. According to the CTS tests link i sent, it is not allowed to add more than one audio track. Do you say it is not true? and there is a bug somewhere in my code? – Raziza O Mar 23 '16 at 23:49
  • This might help - http://stackoverflow.com/questions/23361005/concatenate-multiple-mp4-audio-files-using-android%C2%B4s-mediamuxer – Shadab Ansari Mar 23 '16 at 23:56
  • I saw this post and it is not my case. I am trying to add sound parallel and not one after each other. And all my audio tracks are the same format. So no problem there. – Raziza O Mar 24 '16 at 00:00
  • 2
    If it's helping anyone. What i've done in the end is converted the audio files to PCMs using `MediaExtractor` and `mediaCodec`. Converted them to the same sample rates, bit per sample, and channels when they are not in the same formats. Mixed the samples by myself.. And in the end encoded the PCM raw samples back to my wished format. – Raziza O Jul 12 '16 at 15:12
  • can you post comple code – Uma Achanta Jun 05 '17 at 08:24

1 Answers1

1

For someone who reaches here, I found this official doc at link. Muxing Multiple Video/Audio Tracks seems not supported in old API versions and even restricted in the latest version.

John Hwang
  • 85
  • 6