在AVPlayer中播放视频时,视频中逐帧提取所有图像。
例如,如果视频为30 fps且视频持续时间为10秒,那么我应该可以从视频中获得300张图像。
目前我正在使用以下代码提取图片:
[self.player addPeriodicTimeObserverForInterval:CMTimeMake(1,fps)queue:dispatch_get_main_queue()usingBlock:
^(CMTime time) {
[weakSelf readNextMovieFrame];
}];
(void) readNextMovieFrame {
CMTime outputItemTime = [[self playerItem] currentTime];
CMTime t = [[self playerItem] currentTime];
CMTime d = [[self playerItem] duration];
if ([_output hasNewPixelBufferForItemTime:outputItemTime]) {
CVPixelBufferRef buffer = [_output copyPixelBufferForItemTime:outputItemTime itemTimeForDisplay:nil];
CIImage *image = [CIImage imageWithCVPixelBuffer:buffer];
UIImage *imageUI = [self makeUIImageFromCIImage:image];
[self saveImage];
//CFRelease(sampleBuffer);
CVBufferRelease(buffer);
}
}
答案 0 :(得分:0)
我使用以下内容从iOS上的视频中提取图像。
- (UIImage *)getVideoImageAtTime:(CMTime)time player:(AVPlayer *)player {
NSError *error = nil;
AVAsset *asset = player.currentItem.asset;
AVAssetImageGenerator *ig = [[AVAssetImageGenerator alloc] initWithAsset:asset];
CGImageRef image = [ig copyCGImageAtTime:time actualTime:nil error:&error];
UIImage *screenShot = [UIImage imageWithCGImage:image];
CGImageRelease(image);
return error ? screenShot : nil;
}