iOS将视频帧提取为图像

MB.*_*MB. 14 avfoundation ios

我正在使用UIImagePicker来允许用户创建视频然后修剪它.我需要将该视频分成多个帧,让用户选择其中一个.

为了显示帧我可能必须将它们转换为UIImage.我怎样才能做到这一点?我必须使用AVFoundation,但我找不到关于如何获取和转换帧的教程.

我是否应该使用AVFoundation进行图像捕捉?如果是这样,我必须自己实施修剪?

Rob*_*bin 12

我认为这个问题的答案就是你在寻找什么.

iPhone使用AVFoundation从视频中读取UIimage(帧).

接受的答案指定了2种方法.您可以根据您的要求使用任何一种.


Har*_*kar 8

这是从视频中获取FPS图像的代码

1)进口

#import <Photos/Photos.h>
Run Code Online (Sandbox Code Playgroud)

2)在viewDidLoad中

    videoUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"VfE_html5" ofType:@"mp4"]];
    [self createImage:5]; // 5 is frame per second (FPS) you can change FPS as per your requirement.
Run Code Online (Sandbox Code Playgroud)

3)功能

-(void)createImage:(int)withFPS {
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoUrl options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.requestedTimeToleranceAfter =  kCMTimeZero;
    generator.requestedTimeToleranceBefore =  kCMTimeZero;

    for (Float64 i = 0; i < CMTimeGetSeconds(asset.duration) *  withFPS ; i++){
        @autoreleasepool {
            CMTime time = CMTimeMake(i, withFPS);
            NSError *err;
            CMTime actualTime;
            CGImageRef image = [generator copyCGImageAtTime:time actualTime:&actualTime error:&err];
            UIImage *generatedImage = [[UIImage alloc] initWithCGImage:image];
            [self savePhotoToAlbum: generatedImage]; // Saves the image on document directory and not memory
            CGImageRelease(image);
        }
    }
}

-(void)savePhotoToAlbum:(UIImage*)imageToSave {

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:imageToSave];
    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
            NSLog(@"sucess.");
        }
        else {
            NSLog(@"fail.");
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)