简体   繁体   English

在Android上使用MediaCodec录制慢动作视频

[英]Recording slow motion video using MediaCodec on Android

I'm implementing video recording with Android camera. 我正在使用Android相机实现视频录制。 I use MediaCodec to encode frames from onPreviewFrame() callback. 我使用MediaCodec对onPreviewFrame()回调中的帧进行编码。

Now I want to record slow motion video. 现在,我想录制慢动作视频。 How can I set the camera capture rate or frame rate? 如何设置相机拍摄速率或帧速率? Where can I set these parameters, for example, if I want to record at 120fps, and play at 30fps? 例如,如果我想以120fps录制并以30fps播放,该在哪里设置这些参数?

The following is what I have researched: 以下是我研究的内容:

I recorded a slow-motion video with MOTO X (having built-in slow-motion video recording function) and used FFmpeg to check its fps. 我使用MOTO X(具有内置的慢动作视频录制功能)录制了慢动作视频,并使用FFmpeg检查了它的fps。 I found it records at 110 fps(near 120fps) and playback at 30fps. 我发现它的记录速度为110 fps(接近120 fps),播放速度为30 fps。 This proves that it provides hardware support for recording at high fps. 这证明它为以高fps录制提供硬件支持。

However, when I use getSupportedPreviewFpsRange to check its supported fps range, there are only 4 sets supported fps range with no one greater than 30fps: 但是,当我使用getSupportedPreviewFpsRange检查其支持的fps范围时,仅支持4组支持的fps范围,没有一个大于30fps:

min_fps: 15.0 , max_fps15.0 min_fps:15.0,max_fps15.0

min_fps: 15.0 , max_fps20.0 min_fps:15.0,max_fps20.0

min_fps: 15.0 , max_fps30.0 min_fps:15.0,max_fps30.0

min_fps: 24.0 , max_fps30.0 min_fps:24.0,max_fps30.0

I used setPreviewFpsRange to set the fps higher, but it remains no more than 30fps. 我使用setPreviewFpsRange来设置更高的fps,但它保持不超过30fps。 I also tried set KEY_FRAME_RATE and KEY_CAPTURE_RATE of MediaFormat . 我还尝试设置MediaFormat的 KEY_FRAME_RATE和KEY_CAPTURE_RATE。 But it still does not work. 但这仍然行不通。

I have also searched the solution by setting CamcorderProfile or setVideoFrameRate with MediaRecorder. 我还通过使用MediaRecorder设置CamcorderProfile或setVideoFrameRate来搜索解决方案 But I'm doing with MediaCodec. 但是我正在使用MediaCodec。

Can anyone help me with this problem? 谁能帮助我解决这个问题?

Capturing video frames at a higher rate doesn't address the problem. 以更高的速率捕获视频帧并不能解决问题。 If you capture at 120fps, and play back at 120fps, you have high-speed video playing at a normal rate. 如果以120fps捕获并以120fps播放,则可以正常速率播放高速视频。

What you want to do is modify the timestamps. 您要做的就是修改时间戳。 MediaCodec supports VFR (variable frame-rate) video. MediaCodec支持VFR(可变帧率)视频。 Rather than spending 1 second recording 60 frames that will be played back back at 30fps, you record 30 frames per second that will play back at 15fps. 您无需花费1秒录制60帧以30fps回放的速度,而是每秒录制30帧以15fps回放的速度。

The H.264 stream generated by MediaCodec's AVC encoder does not include timestamps at all. MediaCodec的AVC编码器生成的H.264流完全不包含时间戳。 You specify the timestamp for each frame when you feed the output to MediaMuxer, generating the .mp4 file. 在将输出提供给MediaMuxer并生成.mp4文件时,可以为每个帧指定时间戳。 Most sample code will simply take the timestamp obtained from Camera and pass it through MediaCodec to MediaMuxer unmodified, but you are allowed to tweak it. 大多数示例代码将简单地获取从Camera获得的时间戳,并通过MediaCodec将该时间戳传递给未经修改的MediaMuxer,但可以对其进行调整。 The only thing you can't do is allow the timestamp to go backward. 您唯一不能做的就是让时间戳倒退。

If you look at the way the "eight rects" movie is generated in Grafika , you can see that it plays with the output timestamp to make the movie play slower or faster. 如果查看Grafika中 “八格”电影的生成方式 ,您会发现它以输出时间戳进行播放,从而使电影播放得更快或更慢。

Another easy way to accomplish the same thing without varying the frame rate is to double the frames. 在不改变帧频的情况下完成相同任务的另一种简便方法是将帧加倍。 For the slow-motion portion, simply hand the same frame to the encoder multiple times. 对于慢动作部分,只需将同一帧多次交给编码器即可。 Again, you need to tweak the presentation timestamps for each frame, but your video will have a consistent 30fps. 同样,您需要调整每帧的演示时间戳,但是您的视频将具有一致的30fps。 (It would look better if you interpolated frames, but that's a lot harder.) (如果插值框架会更好,但这要困难得多。)

for slow recording. 进行慢速录制。

mMediaRecorder.setVideoFrameRate(QUALITY_HIGH_SPEED_LOW); mMediaRecorder.setVideoFrameRate(QUALITY_HIGH_SPEED_LOW);

for high speed recording. 用于高速记录。

mMediaRecorder.setVideoFrameRate(QUALITY_HIGH_SPEED_HIGH);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM