Raziza O
Raziza O

Reputation: 1646

Add second audio track to MediaMuxer

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

Upvotes: 3

Views: 3142

Answers (1)

John Hwang
John Hwang

Reputation: 85

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.

Upvotes: 1

Related Questions