iOS Video Filter对于AVVideoComposition,CIFilter,CIImage而言非常慢

时间:2018-11-22 07:41:41

标签: ios objective-c cifilter avvideocomposition

我想从“照片”中过滤选定的视频,因此请使用CIFilter和AVVideoComposition,但这会花费太多时间。

30秒的视频需要3分钟。

如何减少过滤时间。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

    [self dismissViewControllerAnimated:YES completion:nil];

    NSURL *videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];

    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    AVAsset *asset = [AVAsset assetWithURL:videoUrl];

    AVVideoComposition *composition = [AVVideoComposition videoCompositionWithAsset:asset applyingCIFiltersWithHandler:^(AVAsynchronousCIImageFilteringRequest * _Nonnull request) {

        CIImage *ciImage = request.sourceImage.imageByClampingToExtent;

        [filter setValue:ciImage forKey:kCIInputImageKey];

        Float64 seconds = CMTimeGetSeconds(request.compositionTime);
        [filter setValue: @(seconds * 10.0) forKey:kCIInputRadiusKey];

        CIImage *outputImage = [filter.outputImage imageByCroppingToRect:request.sourceImage.extent];

        [request finishWithImage:outputImage context:nil];
    }];


    NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.mp4"];
    unlink([pathToMovie UTF8String]);
    NSURL *outputUrl = [NSURL fileURLWithPath:pathToMovie];


    AVAssetExportSession *export = [AVAssetExportSession exportSessionWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
    export.outputFileType = AVFileTypeQuickTimeMovie;
    export.outputURL = outputUrl;
    export.videoComposition = composition;
    [export exportAsynchronouslyWithCompletionHandler:^{
        NSLog(@"Video Output url : %@", outputUrl.absoluteString);
    }];

}

0 个答案:

没有答案