2

我尝试从 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();

创作:

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;
}

视频格式:

{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)

我已经阅读了Extract Decode Encode Mux Audio,但仍然找不到原因,请帮助我。

4

0 回答 0