3

I try to extract video from mp4 and mix with others audios,here is the video part:

MediaFormat videoFormat = null;
MediaMuxer mp4Muxer = createMediaMuxer();
MediaExtractor mp4Extractor = createMediaExtractor();

for (int i = 0; i < mp4Extractor.getTrackCount(); i++) {
    MediaFormat format = mp4Extractor.getTrackFormat(i);
    String mime = format.getString(MediaFormat.KEY_MIME);
    if (mime.startsWith("video/")) {
        mp4Extractor.selectTrack(i);
        videoFormat = format;
        break;
    }
}

int videoTrackIndex = mp4Muxer.addTrack(videoFormat);

mp4Muxer.start();

boolean videoMuxDone = false;

ByteBuffer videoSampleBuffer = ByteBuffer.allocateDirect(176 * 144); 
BufferInfo videoBufferInfo = new BufferInfo();
int sampleSize;
while (!videoMuxDone) {
    videoSampleBuffer.clear();
    sampleSize = mp4Extractor.readSampleData(videoSampleBuffer, 0);
    if (sampleSize < 0) {
        videoMuxDone = true;
    } else {
        videoBufferInfo.presentationTimeUs = mp4Extractor.getSampleTime();
        videoBufferInfo.flags = mp4Extractor.getSampleFlags();
        videoBufferInfo.size = sampleSize;
        mp4Muxer.writeSampleData(videoTrackIndex, videoSampleBuffer,videoBufferInfo);
        mp4Extractor.advance();
        }
}

mp4Extractor.release();
mp4Muxer.stop();
mp4Muxer.release();

creations:

private MediaExtractor createMediaExtractor() {
        MediaExtractor extractor = new MediaExtractor();
        try {
            extractor.setDataSource(mp4VideoFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return extractor;
}
private MediaMuxer createMediaMuxer() {
        MediaMuxer midiaMuxer = null;
        try {
            midiaMuxer = new MediaMuxer(OUTPUT_MP4,
                    MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return midiaMuxer;
}

the videoformat:

{max-input-size=6561, durationUs=3331377, height=144, mime=video/mp4v-es, csd-0=java.nio.ByteArrayBuffer[position=0,limit=30,capacity=30], width=176}

when it runs to line : mp4Muxer.stop(), exceptions happens:

MPEG4Writer Missing codec specific data
java.lang.IllegalStateException: Failed to stop the muxer
at android.media.MediaMuxer.nativeStop(Native Method)
at android.media.MediaMuxer.stop(MediaMuxer.java:226)
at com.darcye.media.Mp4Muxer.startMix(Mp4Muxer.java:155)

I have read the Extract Decode Encode Mux Audio,but still cant find the reason,please help me.

Community
  • 1
  • 1
Darcy
  • 61
  • 4
  • When you debug, does `videoFormat` in line `int videoTrackIndex = mp4Muxer.addTrack(videoFormat);` contain valid video format? – Zamrony P. Juhara Feb 29 '16 at 05:19
  • @Zamrony P. Juharayes yes, I think I got the problem. I change another mp4 file. and its videoformat is: **{max-input-size=22075, durationUs=5533333, csd-1=java.nio.ByteArrayBuffer[position=0,limit=9,capacity=9], height=320, mime=video/avc, csd-0=java.nio.ByteArrayBuffer[position=0,limit=31,capacity=31], width=560}**,it works! I think its the same question as [MediaMuxer fails to stop if csd-1 not exist](http://stackoverflow.com/questions/21341169/mediamuxer-fails-to-stop-if-csd-1-not-exist). – Darcy Feb 29 '16 at 05:45

0 Answers0