使用MediaMuxer创建的不可流式视频文件 [英] Non-streamable video file created with MediaMuxer

查看:871
本文介绍了使用MediaMuxer创建的不可流式视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MediaCodec编码视频.帧通过相机预览回调传递到MediaCodec实例(未使用Surface).我正在使用 JCodec 库进行混合,并且能够流式传输产生的视频(视频播放器显示正确的持续时间,我可以通过搜索栏更改视频位置.

I am using MediaCodec to encode video. Frames are coming through the camera preview callback to the MediaCodec instance (no Surface used). I am using JCodec library for muxing and I am able to stream produced video (video player is showing correct duration and I am able to change video position with seek bar).

今天,我尝试使用MediaMuxer而不是JCodec,并且视频看起来仍然不错,但是持续时间绝对不正确(几小时而不是一分钟),搜索栏不起作用完全没有.

Today I've tried to use MediaMuxer instead of JCodec and I've got video which still looks fine, but duration is absolutely incorrect (a few hours instead of one minute) and the seek bar is not working at all.

mediaMuxer = new MediaMuxer("/path/to/video.mp4", MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

当我收到MediaCodec.INFO_OUTPUT_FORMAT_CHANGED时,以下代码会被延迟调用:

The following code is lazily called when I receive MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:

videoTrackIndex = mediaMuxer.addTrack(encoder.getMediaFormat());
mediaMuxer.start();

我正在使用以下代码对帧进行编码:

I am encoding the frames with the following code:

mediaMuxer.writeSampleData(videoTrackIndex, byteBuffer, bufferInfo);

在定位之后,

byteBufferbufferInfo直接来自MediaCodec:

byteBuffer and bufferInfo are coming directly from MediaCodec after some positioning stuff:

byteBuffer.position(bufferInfo.offset);
byteBuffer.limit(bufferInfo.offset + bufferInfo.size);

演示时间设置正确:

mMediaCodec.queueInputBuffer(inputBufferIndex, 0, getWidth() * getHeight() * 1.5, System.nanoTime() / 1000, 0);

在记录的结尾,我要做:

And at the end of the record I do:

mediaMuxer.stop();
mediaMuxer.release();

日志:

I/MPEG4Writer﹕ setStartTimestampUs: 0
I/MPEG4Writer﹕ Earliest track starting time: 0
D/MPEG4Writer﹕ Stopping Video track
I/MPEG4Writer﹕ Received total/0-length (770/0) buffers and encoded 770 frames. - video
D/MPEG4Writer﹕ Stopping Video track source
D/MPEG4Writer﹕ Video track stopped
D/MPEG4Writer﹕ Stopping writer thread
D/MPEG4Writer﹕ 0 chunks are written in the last batch
D/MPEG4Writer﹕ Writer thread stopped
I/MPEG4Writer﹕ The mp4 file will not be streamable.
D/MPEG4Writer﹕ Stopping Video track

我想The mp4 file will not be streamable.有关问题的信号.

更新:

我已经在另一台设备(LG G2)上测试了我的应用程序,该设备执行的日志记录更为详细.产生相同文件的时间很长.日志位于此处,视频文件为

I've tested my app on another device (LG G2) which does more verbose logging. The same file is produced with huge duration. Logs are here and the video file is here.

推荐答案

由于@fadden,我得以找出问题所在.我实际上是在发送我的presentationTimeUs = 0的第一帧.这是因为我没有正确处理带有MediaCodec.BUFFER_FLAG_CODEC_CONFIG标志的帧.我实际上是在将它们喂给多路复用器,但是我应该做的是通过以下代码跳过它们(按照示例):

Thanks to @fadden I was able to figure out the problem. I was actually sending my first frame with presentationTimeUs = 0. It happened because I was not handling frames with MediaCodec.BUFFER_FLAG_CODEC_CONFIG flag properly. I was actually feeding them to the muxer, but what I should have done is to skip them with the following code (as per example):

if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
  mBufferInfo.size = 0;
}

这篇关于使用MediaMuxer创建的不可流式视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆