如何从 iOS 上的视频获取帧

How to get frame from video on iOS

在我的应用程序中,我想从视频中提取帧以进行过滤。我尝试按时间偏移拍摄帧边缘视频。这是我的代码:

- (UIImage*)getVideoFrameForTime:(NSDate*)time {

    CGImageRef thumbnailImageRef = NULL;

    NSError *igError = nil;

    NSTimeInterval timeinterval = [time timeIntervalSinceDate:self.videoFilterStart];

    CMTime atTime = CMTimeMakeWithSeconds(timeinterval, 1000);
    thumbnailImageRef =
    [self.assetImageGenerator copyCGImageAtTime:atTime
                                     actualTime:NULL
                                          error:&igError];

    if (!thumbnailImageRef) {
        NSLog(@"thumbnailImageGenerationError %@", igError );
    }

    UIImage *image = thumbnailImageRef ? [[UIImage alloc] initWithCGImage:thumbnailImageRef] : nil;

    return image;
}

不幸的是,我只看到位于整数秒的帧:1、2、3.. 即使时间间隔不是整数(1.5 等)。

如何获取任意非整数区间的帧数?

如果我没记错的话,NSDate 的准确度只能达到秒,这就解释了为什么帧只占用整数秒。您必须使用不同类型的输入才能在 non-integer 秒时获取帧。

使用此项目获取更多帧详细信息 github上的对应项目:iFrameExtractor.git

感谢@shallowThought 我在这个问题中找到了答案

你只需要添加这两行

assetImgGenerate.requestedTimeToleranceAfter = kCMTimeZero;
assetImgGenerate.requestedTimeToleranceBefore = kCMTimeZero;