0
回答
MediaMuxer and MediaExtractor 提取视频
注册华为云得mate10,2.9折抢先购!>>>   

我想从MP4中提取视频然后和音频进行合成,这是提取视频的部分代码:

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}
当调用 mp4Muxer.stop(), 发生异常:


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)

MPEG4Writer Missing codec specific data 发生点在MPEG4Writer.cpp的位置处:

if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime) ||
        !strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime) ||
        !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
        if (!mCodecSpecificData ||
            mCodecSpecificDataSize <= 0) {
            ALOGE("Missing codec specific data");
            return ERROR_MALFORMED;
        }
    }



我要如何才能让程序正确工作呢?



举报
叶大侠
发帖于2年前 0回/711阅
顶部