如何解决什么是Android中的MediaCodec,MediaExtractor和MediaMuxer?
MediaCodec,MediaExtractor和MediaMuxer在android中是什么意思?我不是视频人,但是我确实知道编码和解码的基本含义。我需要知道每个类的功能是什么,以及在什么情况下使用它们。
我也想知道:
- 如果我想从摄像机预览中提取帧并创建视频文件并进行一些编辑(例如速度),应该使用哪些类以及它们如何协同工作?
- 如果我要创建像Exoplayer这样的视频播放器(不是所有功能,而是一个简单的Dash自适应流播放器),我应该使用哪些类,以及它们如何协同工作?
希望你会回答。谢谢。
解决方法
首先,我要说的是,如果您不了解视频编码/解码的工作原理,则很难理解该API。我建议对编码器/解码器的工作方式进行研究。
我将为每个提供一个过于简单的解释。
MediaCodec类可用于访问低级媒体编解码器,即编码器/解码器组件。它是Android低级多媒体支持基础架构的一部分
因此MediaCodec
处理视频数据包/缓冲区的解码或编码,并负责与编解码器的交互。
以下是如何初始化MediaCodec
的示例:
// Create Mediacodec instance by passing a mime type. It will select the best codec for this mime type
MediaCodec mDecoder = MediaCodec.createDecoderByType(mimeType);
// Pass an instance on MediaFormat and the output/rendering Surface
mDecoder.configure(format,surface,null,0);
mDecoder.start();
然后您将开始将缓冲区传递到MediaCodec
,如下所示:
ByteBuffer[] inputBuffers = mDecoder.getInputBuffers();
int index = mDecoder.dequeueInputBuffer(timeout);
// Check if buffers are available
if (index >= 0) {
// Get dequeued buffer
ByteBuffer buffer = inputBuffers[index];
// Get sample data size to determine if we should keep queuing more buffers or signal end of stream
int sampleSize = mExtractor.readSampleData(buffer,0);
if (sampleSize < 0) {
// Signal EOS,this happens when you reach the end if the video,for example.
mDecoder.queueInputBuffer(inIndex,MediaCodec.BUFFER_FLAG_END_OF_STREAM);
} else {
// Queue the dequeued buffer and pass the extractors sample time
mDecoder.queueInputBuffer(index,sampleSize,mExtractor.getSampleTime(),0);
mExtractor.advance();
}
}
然后您将输出缓冲区出队并释放到表面:
BufferInfo frameInfo = new BufferInfo();
int index mDecoder.dequeueOutputBuffer(frameInfo,timeout);
switch (index) {
case
MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
break;
case
MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
MediaFormat newFormat = mDecoder.getOutputFormat();
break;
case
MediaCodec.INFO_TRY_AGAIN_LATER: break; default:
break;
}
// You can now push the frames to the surface
// This is where you can control the playback speed,you can do this by letting your thread sleep momentarily
if (index > 0) {
mDecoder.releaseOutputBuffer(bufferIndex,true);
}
MediaExtractor有助于从数据源提取已解复用的,通常经过编码的媒体数据。
文档描述不言自明。
下面看一下,我添加了一些注释,使其更易于理解:
// Initialize the extractor
MediaExtractor() mExtract = new MediaExtractor(); mExtract.setDataSource(mSource);
// Select/set the video track (if available)
int trackIndex = selectVideoTrack(mExtract);
if(trackIndex < 0)
throw new IOException("Can't find track");
mExtract.selectTrack(trackIndex);
// The extractor is now ready to be used
// Get the track format
mFormat = mExtractor.getTrackFormat(trackIndex);
// Get buffer size to check if a buffer is available
// This will be used by MediaCodec to determine if buffers are available
sampleSize = mExtractor.readSampleData(buffer,0);
MediaMuxer有助于混合基本流。当前,MediaMuxer支持MP4,Webm和3GP文件作为输出。自Android牛轧糖以来,它还支持在MP4中混合B帧。
这又是不言而喻的。它用于创建视频/音频文件。例如,将两个视频文件合并在一起。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。