![](/img/trans.png)
[英]Android - Using MediaMuxer with MediaExtractor and PCM stream leads to corrupted video frames
[英]What is MediaCodec, MediaExtractor and MediaMuxer in android?
MediaCodec、MediaExtractor 和 MediaMuxer 在安卓中是什么意思? 我不是視頻愛好者,但我知道編碼和解碼的基本含義。 我需要知道每個類的功能是什么以及它們在哪些用例中使用。
我也想知道:
首先讓我說,如果您不了解視頻編碼/解碼的工作原理,就很難理解這個 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, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
} else {
// Queue the dequeued buffer and pass the extractors sample time
mDecoder.queueInputBuffer(index, 0, 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 Nougat 以來,它還支持在 MP4 中混合 B 幀。
這又是不言自明的。 它用於創建視頻/音頻文件。 例如,將兩個視頻文件合並在一起。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.