AVFoundation - Adding blur background to video

后端 未结 4 1523
花落未央
花落未央 2020-12-28 10:50

I am working on a video editing app in Swift. In my case my output video looks like as following

I am trying to fill the black portion with blur effect e

相关标签:
4条回答
  • 2020-12-28 11:20

    Swift 4 - Adding blur background to video

    1. Single video support ❤️
    1. Multiple videos merging support ❤️
    2. Support any canvas in any ratio ❤️
    3. Save final video to camera roll ❤️
    5. Manage all video orientations ❤️

    May be I'm late for this answer but still I didn't find any solution for this requirement. So sharing my work:

    0 讨论(0)
  • 2020-12-28 11:21

    Starting from iOS 9.0 You can use AVAsynchronousCIImageFilteringRequest

    see docs for more info

    Or You can use AVVideoCompositing see example of usage

    0 讨论(0)
  • 2020-12-28 11:26

    You can add blur on video using AVVideoComposition, it's Tested.

    -(void)applyBlurOnAsset:(AVAsset *)asset Completion:(void(^)(BOOL success, NSError* error, NSURL* videoUrl))completion{
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    AVVideoComposition *composition = [AVVideoComposition videoCompositionWithAsset: asset
                                                       applyingCIFiltersWithHandler:^(AVAsynchronousCIImageFilteringRequest *request){
                                                           // Clamp to avoid blurring transparent pixels at the image edges
                                                           CIImage *source = [request.sourceImage imageByClampingToExtent];
                                                           [filter setValue:source forKey:kCIInputImageKey];
    
                                                           [filter setValue:[NSNumber numberWithDouble:10.0] forKey:kCIInputRadiusKey];
    
                                                           // Crop the blurred output to the bounds of the original image
                                                           CIImage *output = [filter.outputImage imageByCroppingToRect:request.sourceImage.extent];
    
                                                           // Provide the filter output to the composition
                                                           [request finishWithImage:output context:nil];
                                                       }];
    
    
    NSURL *outputUrl = [[NSURL alloc] initWithString:@"Your Output path"];
    
    //Remove any prevouis videos at that path
    [[NSFileManager defaultManager]  removeItemAtURL:outputUrl error:nil];
    
    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPreset960x540] ;
    
    // assign all instruction for the video processing (in this case the transformation for cropping the video
    exporter.videoComposition = composition;
    exporter.outputFileType = AVFileTypeMPEG4;
    
    if (outputUrl){
    
        exporter.outputURL = outputUrl;
        [exporter exportAsynchronouslyWithCompletionHandler:^{
    
            switch ([exporter status]) {
                case AVAssetExportSessionStatusFailed:
                    NSLog(@"crop Export failed: %@", [[exporter error] localizedDescription]);
                    if (completion){
                        dispatch_async(dispatch_get_main_queue(), ^{
                            completion(NO,[exporter error],nil);
                        });
                        return;
                    }
                    break;
                case AVAssetExportSessionStatusCancelled:
                    NSLog(@"crop Export canceled");
                    if (completion){
                        dispatch_async(dispatch_get_main_queue(), ^{
                            completion(NO,nil,nil);
                        });
                        return;
                    }
                    break;
                default:
                    break;
            }
    
            if (completion){
                dispatch_async(dispatch_get_main_queue(), ^{
                    completion(YES,nil,outputUrl);
                });
            }
    
        }];
    }
    

    }

    0 讨论(0)
  • 2020-12-28 11:31

    Have you tryied this?

    let blurEffect = UIBlurEffect(style: .light)
    var blurredView = UIVisualEffectView(effect: blurEffect)
    blurredView.frame = view.bounds // Set the frame around 
    // add the blurredView on top of what you want blurred
    view.addSubview(blurredView)
    

    Here's a link to documentation

    0 讨论(0)
提交回复
热议问题