普通mp4文件
我生成的视频文件
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));