将H.264视频流和AAC音频流与mediamuxer结合使用,但无法播放合并的mp4视频

时间:2018-11-15 10:55:57

标签: android h.264

普通mp4文件

enter image description here

我生成的视频文件

enter image description here

mediaformat

        MediaFormat videoMediaFormat = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC,
            mVideoSize.getWidth(), mVideoSize.getHeight());
    videoMediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, Constant.VIDEO_BIT_RATE);
    videoMediaFormat.setByteBuffer(Constant.CSD_0, ByteBuffer.wrap(sps));
    videoMediaFormat.setByteBuffer(Constant.CSD_1, ByteBuffer.wrap(pps));

视频写入方法

private void writeVideoData(byte[] videoData) {
    Log.d(TAG, "writeVideoData: length = " + videoData.length);
    MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
    Log.d(TAG, "writeVideoData: FLAG_KEY_FRAME = " + Tool.byteToHexString(videoData[Constant.FLAG_KEY_FRAME]));
    if (videoData[Constant.FLAG_KEY_FRAME] == Constant.KEY_FRAME
            | videoData[Constant.FLAG_KEY_FRAME] == Constant.KEY_FRAME_1) {
        bufferInfo.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME;
    } else {
        bufferInfo.flags = 0;
    }
    bufferInfo.offset = 0;
    if (startTime == 0L) {
        bufferInfo.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME;
        bufferInfo.presentationTimeUs = startTime;
        startTime = System.nanoTime();
        Log.d(TAG, "writeVideoData: time = " + 0);
    } else {
        long time = (System.nanoTime() - startTime) / 1000;
        Log.d(TAG, "writeVideoData: time = " + time);
        bufferInfo.presentationTimeUs = time;
    }
    bufferInfo.size = videoData.length;
    boolean isInit = videoTrack != Constant.DEFAULT_TRACK && mediaMuxer != null;
    if (isInit) {
        writeSampleData(videoTrack, ByteBuffer.wrap(videoData), bufferInfo);
    }
}

流是通过mediarecorder获得的

DataInputStream dis = new DataInputStream(new ParcelFileDescriptor.AutoCloseInputStream(videoRead));

0 个答案:

没有答案