[Android] MediaExtractor 与 MediaMuxer

MediaExtractor

MediaExtractor 是用来将视频文件解析成视频和音频的,Android 官方提供了以下示例代码:

 MediaExtractor extractor = new MediaExtractor();
 extractor.setDataSource(...);
 int numTracks = extractor.getTrackCount();
 for(int i = 0; i <numTracks; ++ i){
   MediaFormat format = extractor.getTrackFormat(i);
   String mime = format.getString(MediaFormat.KEY_MIME);
   if(weAreInterestedInThisTrack){
     extractor.selectTrack(ⅰ);
   }
 }
 ByteBuffer inputBuffer = ByteBuffer.allocate(...)
 while(extractor.readSampleData(inputBuffer,...)> = 0){
   int trackIndex = extractor.getSampleTrackIndex();
   long presentationTimeUs = extractor.getSampleTime();
   ...
   extractor.advance();
 }

 extractor.release();
 extractor = null;

其中重要的方法有:

setDataSource():设置文件源

getTrackCount():获取轨道数

getTrackFormat(int):获取指定轨道的音频格式信息。更多信息可见:https://developer.android.com/reference/android/media/MediaFormat

selectTrack(int):后面调用 readSampleData(ByteBuffer, int) 、 getSampleTrackIndex() 、 getSampleTime() 时只获取此选择轨道的信息

getSampleTrackIndex():获取当前轨道的索引

getSampleTime():返回当前时间戳(单位:ms)

advance():读取下一帧数据

readSampleData(ByteBuffer,  int):把指定通道中的数据按偏移量读取到 ByteBuffer 中

release():释放资源

MediaMuxer

MediaMuxer 是 Android 提供用来生成音视频的,可输出 MP4、Webm、 3GP 格式的文件。

   MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
   // SetUp Video/Audio Tracks.
   MediaFormat audioFormat = new MediaFormat(...);
   MediaFormat videoFormat = new MediaFormat(...);
   int audioTrackIndex = muxer.addTrack(audioFormat);
   int videoTrackIndex = muxer.addTrack(videoFormat);

   // Setup Metadata Track
   MediaFormat metadataFormat = new MediaFormat(...);
   metadataFormat.setString(KEY_MIME, "application/gyro");
   int metadataTrackIndex = muxer.addTrack(metadataFormat);

   muxer.start();
   while(..) {
       // Allocate bytebuffer and write gyro data(x,y,z) into it.
       ByteBuffer metaData = ByteBuffer.allocate(bufferSize);
       metaData.putFloat(x);
       metaData.putFloat(y);
       metaData.putFloat(z);
       BufferInfo metaInfo = new BufferInfo();
       // Associate this metadata with the video frame by setting
       // the same timestamp as the video frame.
       metaInfo.presentationTimeUs = currentVideoTrackTimeUs;
       metaInfo.offset = 0;
       metaInfo.flags = 0;
       metaInfo.size = bufferSize;
       muxer.writeSampleData(metadataTrackIndex, metaData, metaInfo);
   };
   muxer.stop();
   muxer.release();
 }

其中重要的方法有:

MediaMuxer(String path, int format):传入路径以及格式,构造对象

addTrack(MediaFormat format):添加具有指定格式的轨道

start():开始合成

writeSampleData(int trackIndex, ByteBuffer byteBuf, MediaCodec.BufferInfo bufferInfo):把 ByteBuffer 中的数据写入到在构造器设置的文件中

stop():结束合成

release():释放资源

参考资料

https://developer.android.com/reference/android/media/MediaMuxer

https://developer.android.com/reference/android/media/MediaExtractor

发表评论

电子邮件地址不会被公开。 必填项已用*标注