GITHUB: https://github.com/chezi008/mp4muxer
如果数据由本机摄像头直接采集,建议使用MediaMuxer类去实现mp4的合成。如果是服务端传输过来的视频流可以使用mp4v2的方法实现mp4的合成。我在项目里面也简单的利用MediaMuxer编写了一个Demo。可能写的不是很详细,功能也不是很完善。所以有什么问题还是多多希望指出,一起改进。
封装了如下几个方法:
// open or creat a mp4 file. MP4FileHandle CreateMP4File(const char *fileName,int width,int height,int timeScale = 90000,int frameRate = 25); // wirte 264 metadata in mp4 file. bool Write264Metadata(MP4FileHandle hMp4File,LPMP4ENC_Metadata lpMetadata); // wirte 264 data, data can contain multiple frame. int WriteH264Data(MP4FileHandle hMp4File,const unsigned char* pData,int size); // close mp4 file. void CloseMP4File(MP4FileHandle hMp4File); // convert H264 file to mp4 file. // no need to call CreateMP4File and CloseMP4File,it will create/close mp4 file automaticly. bool WriteH264File(const char* pFile264,const char* pFileMp4);方法说明:
CreateMP4File:创建需要写入的mp4文件,可以设置视频的分辨率。默认的时间刻度是90000,帧率是25帧WriteH264Data:写入h264数据,可以写入byte[]类型的数据流,也可是LPMP4ENC_Metadata(sps pps)类型数据。CloseMP4File:转换完成后记得要释放内存,调用次方法WriteH264File:直接将本地h264文件转换成mp4。实现方法是一样的,只是在C++代码里面实现了,将h264数据分割成一帧一帧,再写入至输入文件中。这个库太久没有维护了,还是建议使用官方API,即第二种方法。 同样,我在代码中封装了三个本地方法:
public static native void init(String mp4FilePath, int widht, int height); public static native int writeH264Data(byte[] data, int size); public static native void close();从方法名,可以看得出怎么使用,所以在这里就不多赘述了。项目里面提供了标准的h264测试文件mtv.h264。
####官方文档介绍:http://www.loverobots.cn/android-api/reference/android/media/MediaMuxer.html
MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4); // More often, the MediaFormat will be retrieved from MediaCodec.getOutputFormat() // or MediaExtractor.getTrackFormat(). MediaFormat audioFormat = new MediaFormat(...); MediaFormat videoFormat = new MediaFormat(...); int audioTrackIndex = muxer.addTrack(audioFormat); int videoTrackIndex = muxer.addTrack(videoFormat); ByteBuffer inputBuffer = ByteBuffer.allocate(bufferSize); boolean finished = false; BufferInfo bufferInfo = new BufferInfo(); muxer.start(); while(!finished) { // getInputBuffer() will fill the inputBuffer with one frame of encoded // sample from either MediaCodec or MediaExtractor, set isAudioSample to // true when the sample is audio data, set up all the fields of bufferInfo, // and return true if there are no more samples. finished = getInputBuffer(inputBuffer, isAudioSample, bufferInfo); if (!finished) { int currentTrackIndex = isAudioSample ? audioTrackIndex : videoTrackIndex; muxer.writeSampleData(currentTrackIndex, inputBuffer, bufferInfo); } }; muxer.stop(); muxer.release();在结束的时候你可能会遇到几种异常:
E/MPEG4Writer: Missing codec specific data: 这是因为在写入数据的时候没有写入编码参数,h264的编码参数包含SPS和PPS。所以当你视频流遇到这些参数帧的时候,请设置好对应的参数。 //设置sps和pps 如果设置不正确会导致合成的mp4视频作为文件预览的时候,预览图片是黑色的 //视频进度条拖拽画面会出现绿色,以及块状现象 mediaformat.setByteBuffer("csd-0", mCSD0); mediaformat.setByteBuffer("csd-1", mCSD1);AAC参数:PCM在用编码器编码成AAC格式音频的时候,编码器会在自动设置参数。
当outIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED 我们可以获取到,MediaFormat format = mEnc.getOutputFormat(), format就包含了CODEC_CONFIG。此时的format可直接作为addTrack()的参数使用。 There are no sync frames for video track 这是因为没有设置关键帧的flag。 switch (nalType & 0x1f) { case H264Decoder.NAL_TYPE_I: bufferInfo.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME; break; } 音视频同步 方法里面传入的时间参数PTS的单位时:microseconds,微妙。 long pts = System.nanoTime() / 1000;直接使用当前时间戳会有问题,录制成的MP4总时间会很大。我使用一个相对时间,当前时间相对于开始的时间差。
bufferInfo.presentationTimeUs = System.nanoTime()/1000-startPts;Android在MediaMuxer和MediaCodec用例:https://www.cnblogs.com/hrhguanli/p/5043610.html Grafika: https://github.com/google/grafika HWEncoderExperiments:https://github.com/OnlyInAmerica/HWEncoderExperiments/tree/audioonly/HWEncoderExperiments/src/main/java/net/openwatch/hwencoderexperiments