如何使用GPUImageChromaKeyBlendFilter组合两个不同大小的视频?

时间:2016-01-04 11:40:41

标签: ios objective-c iphone avfoundation gpuimage

我有视频,我正在使用GPUImageChromaKeyBlendFilter在第一个视频上添加第二个视频,Everthing工作正常。现在我想知道有任何方法可以设置两个不同大小的视频并创建一个视频。

离。第一个视频大小是1280 * 720,第二个视频大小是640 * 640我想使用GPUImage在第一个视频上设置第二个视频是否可能?

请参阅下面的代码,这是我实施的,它在相同尺寸的视频中运行良好,但如何设置两个不同大小的视频请帮助我。

  fxURL = [[NSBundle mainBundle] URLForResource:@"FXSample" withExtension:@"mov"];
    rawVideoURL = [[NSBundle mainBundle] URLForResource:@"rrecord" withExtension:@"mov"];

    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:rawVideoURL options:nil];
    AVAssetTrack *videoAssetTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

    self.gpuMovieA = [[GPUImageMovie alloc] initWithURL:rawVideoURL];
    self.gpuMovieFX = [[GPUImageMovie alloc] initWithURL:fxURL];
    self.gpuMovieA.playAtActualSpeed = YES;
    self.filter = [[GPUImageChromaKeyBlendFilter alloc] init];
    [self.filter setupFilterForSize:CGSizeMake(640, 640)];
    [self.gpuMovieFX addTarget:self.filter];
    [self.gpuMovieA addTarget:self.filter];

    _gpuMovieFX.playAtActualSpeed = YES;
    self.gpuMovieA.playAtActualSpeed = YES;

    //setup writer
    NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/gpu_output.mov"];
    unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie
    NSURL *outputURL = [NSURL fileURLWithPath:pathToMovie];

    self.movieWriter =  [[GPUImageMovieWriter alloc] initWithMovieURL:outputURL size:videoAssetTrack.naturalSize];

    [self.filter addTarget:self.movieWriter];

    self.movieWriter.shouldPassthroughAudio = NO;
    self.gpuMovieA.audioEncodingTarget = nil;
    //add this line otherwise it will cause "Had to drop an audio frame" which cozes the saved video lose some sounds
    [self.gpuMovieA enableSynchronizedEncodingUsingMovieWriter:self.movieWriter];

    [self.movieWriter startRecording];
    [self.gpuMovieA startProcessing];
    [self.gpuMovieFX startProcessing];

    __weak typeof(self) weakSelf = self;

    [self.movieWriter setCompletionBlock:^{
        weakSelf.gpuMovieA.audioEncodingTarget = nil;
        [weakSelf.gpuMovieFX endProcessing];
        [weakSelf.gpuMovieA endProcessing];

        [weakSelf.movieWriter finishRecordingWithCompletionHandler:^{
            NSLog(@"movie writing done");
            [weakSelf.gpuMovieA cancelProcessing];
            [weakSelf.gpuMovieFX cancelProcessing];

            dispatch_async(dispatch_get_main_queue(), ^{
                // [SVProgressHUD showErrorWithStatus:@"failed"];
                [weakSelf writeToAlbum:outputURL];
            });
        }];
    }]; 

0 个答案:

没有答案