Truncate Video Using MediaCodec

I used the Android MediaCodec library to transcode video files (basically, change )

Another thing I want to achieve is to truncate the video - only start 15 seconds. The logic is to check videoExtractor.getSampleTime() , if it is more than 15 seconds, I just write EOS to the decoder buffer.

But I get an exception Caused by: android.media.MediaCodec$CodecException: Error 0xfffffff3

Here is my code:

  while ((!videoEncoderDone) || (!audioEncoderDone)) { while (!videoExtractorDone && (encoderOutputVideoFormat == null || muxing)) { int decoderInputBufferIndex = videoDecoder.dequeueInputBuffer(TIMEOUT_USEC); if (decoderInputBufferIndex == MediaCodec.INFO_TRY_AGAIN_LATER) break; ByteBuffer decoderInputBuffer = videoDecoderInputBuffers[decoderInputBufferIndex]; int size = videoExtractor.readSampleData(decoderInputBuffer, 0); long presentationTime = videoExtractor.getSampleTime(); if (size >= 0) { videoDecoder.queueInputBuffer( decoderInputBufferIndex, 0, size, presentationTime, videoExtractor.getSampleFlags()); } videoExtractorDone = !videoExtractor.advance(); if (!videoExtractorDone && videoExtractor.getSampleTime() > mVideoDurationLimit * 1000000) { videoExtractorDone = true; } if (videoExtractorDone) videoDecoder.queueInputBuffer(decoderInputBufferIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM); break; } 

Full source code can be found here .

+7
android mediacodec
source share
1 answer

I'm not sure if this is the source of the error or not, but I think it is safe to write EOS to the decoder buffer at an arbitrary point.

The reason is that the input video uses the main profile of H264 or higher, pts may not be in ascending order (because there is a B-frame), so you can skip a few frames at the end of the video. In addition, when the last frame you send to the decoder is a B-frame, the decoder may expect the next packet, but you send the EOS flag and throw an error (not quite sure).

What can you do, you can send the EOS flag to the encoder using videoEncoder.signalEndOfInputStream() after you reach the desired frame (pts decoder output is guaranteed in ascending order, at least after version android> = 4.3?)

0
source share

All Articles