iOS AVFoundation 如何使用 CMTime 转换每秒相机帧数以创建延时摄影?

Posted

技术标签:

【中文标题】iOS AVFoundation 如何使用 CMTime 转换每秒相机帧数以创建延时摄影?【英文标题】:iOS AVFoundation how to convert camera frames per second to create a timelapse using CMTime? 【发布时间】:2016-01-27 16:29:53 【问题描述】:

我有一个 iPhone 摄像头附件,它以 9FPS 的速度捕获视频,并将其作为单独的 UIImages 提供。我正在尝试将这些图像拼接在一起,以使用 AVFoundation 创建相机所看到的延时视频。

我不确定如何正确转换帧和时间以实现我想要的时间压缩。

例如 - 我希望将 1 小时的真实生活片段转换为 1 分钟的时间间隔。这告诉我,我需要每 60 帧捕获一次并将其附加到延时摄影中。

下面的代码是否完成了 60 秒到 1 秒的延时转换? 还是我需要通过kRecordingFPS 再添加一些乘法/除法?

#define kRecordingFPS 9
#define kTimelapseCaptureFrameWithMod 60
//frameCount is the number of frames that the camera has output so far
if(frameCount % kTimelapseCaptureFrameWithMod == 0)

    //...
    //convert image and prepare it for recording
    [self appendImage:image 
               atTime:CMTimeMake(currentRecordingFrameNumber++, kRecordingFPS)];

【问题讨论】:

【参考方案1】:

您的代码将每 1/9 秒在您的电影中制作 60 帧中的 1 帧,并且每次将 frameIndex 增加一。

结果应该是 [max value for frameCount] / (60 * 9) 的影片。如果您有 32400 帧(9fps 的 1 小时影片),则影片的长度为 540:9 = 60s。

尝试在每次调用 appendImage:atTime: 时使用 CMTimeShow() 打印 CMTime 以进行检查。

//at 5 fps, 300 frames recorded per 60 seconds
//to compress 300 frames into 1 second at 5 fps, we need to take every 300/5 = 60th frame
//to compress 300 frames into 1 second at 15 fps, take every 300/15 = 20th frame
//to compress 300 frames into 1 sec at 30 fps, take every 300/30 = 10th frame

#define kReceivedFPS 9
#define kStoreFPS 9
#define kSecondsPerSecondCompression 60

#define kTimelapseCaptureFrameWithMod (kSecondsPerSecondCompression * kReceivedFPS) / kStoreFPS

【讨论】:

以上是关于iOS AVFoundation 如何使用 CMTime 转换每秒相机帧数以创建延时摄影?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 captureStillImageAsynchronouslyFromConnection 实现多镜头(iOS AVFoundation)

iOS AVFoundation 如何使用 CMTime 转换每秒相机帧数以创建延时摄影?

IOS开发中AVFoundation中AVAudioPlayer的使用

iOS 8 AVFoundation - 如何在有能力的设备上启用视频稳定功能

iOS AVFoundation 导出会话缺少音频

iOS使用AVFoundation实现二维码扫描