2016-07-15 64 views
0

我已经通过了新的GPUImageUIElementFilter的几个例子了,并试图将其应用到视频我从磁盘有,但我得到一个“白屏”视频代替。我试着用其他过滤器(棕褐色,像素)运行代码,它似乎工作正常,但与UI元素混合失败。GPUImageUIElementFilter与白色画面返回视频

这里是我的代码:

//Declared in interface 

@property(nonatomic, strong) GPUImageMovieWriter *movieWriter; 
@property(nonatomic, strong) GPUImageMovie *movieFile; 
@property(nonatomic, strong) GPUImageFilter *blendFilter; 
@property(nonatomic, strong) GPUImageUIElement *uiElementFilter; 



- (void)videoOutputOverlay { 

    self.movieFile = [[GPUImageMovie alloc] initWithURL:self.post.url]; 
    self.movieFile.runBenchmark = YES; 
    self.movieFile.playAtActualSpeed = YES; 
    self.blendFilter = [[GPUImageAlphaBlendFilter alloc] init]; 
    ((GPUImageAlphaBlendFilter *)self.blendFilter).mix = 1.0; 
    self.drawingView.backgroundColor = [UIColor clearColor]; 

    self.uiElementFilter = [[GPUImageUIElement alloc] initWithView:self.drawingView]; 
    [self.movieFile addTarget:self.blendFilter]; 
    [self.uiElementFilter addTarget:self.blendFilter]; 

    // In addition to displaying to the screen, write out a processed version of the movie to disk 
    NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Output.m4v"]; 
    unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie 
    NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie]; 

    self.movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(640.0, 480.0)]; 
    [self.blendFilter addTarget:self.movieWriter]; 

    // Configure this for video from the movie file, where we want to preserve all video frames and audio samples 
    self.movieWriter.shouldPassthroughAudio = YES; 
    self.movieFile.audioEncodingTarget = self.movieWriter; 
    [self.movieFile enableSynchronizedEncodingUsingMovieWriter:self.movieWriter]; 

    [self.movieWriter startRecording]; 
    [self.movieFile startProcessing]; 

    __weak PostPreviewViewController *weakSelf = self; 
    [self.movieWriter setCompletionBlock:^{ 
    [weakSelf.blendFilter removeTarget:weakSelf.movieWriter]; 
    [weakSelf.movieWriter finishRecording]; 

    UISaveVideoAtPathToSavedPhotosAlbum(pathToMovie, nil, NULL, NULL); 

    NSLog(@"completed saving movie"); 

    }]; 
} 

这一切几乎从一些例子在那里网上复制相同,主要区别是,我使用的是文件没有视频直播。

在我的“相机胶卷”导出的视频,我可以听到音频+一切都与其它过滤器的工作,所以我相信视频是正确加载(加上没有得到对出口的任何错误)

我假设我设置过滤器/目标的方式有问题。

将不胜感激任何帮助!由于

回答

0

一旦有人运行到这个问题,我能够在一定程度上解决这个问题(只是缺少声音在我的情况现在)。这里是我的代码,介意它有一些项目的具体的东西相反却是通用的,否则

- (void)videoOutputOverlay { 

    movieFile = [[GPUImageMovie alloc] initWithURL:self.post.videoUrl]; 
    movieFile.runBenchmark = YES; 
    movieFile.playAtActualSpeed = NO; 

    filter = [[GPUImageBrightnessFilter alloc] init]; 
    // [filter useNextFrameForImageCapture]; 
    [(GPUImageBrightnessFilter *)filter setBrightness:0.0]; 
    [filter setInputRotation:kGPUImageRotateRight atIndex:0]; 

    GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init]; 
    // [blendFilter useNextFrameForImageCapture]; 
    blendFilter.mix = 1.0; 

    UIView *contentView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
    [contentView addSubview:self.drawingView]; 
    [contentView addSubview:self.textOverlayView]; 
    uiElementInput = [[GPUImageUIElement alloc] initWithView:contentView]; 

    [movieFile addTarget:filter]; 
    [filter addTarget:blendFilter]; 
    [uiElementInput addTarget:blendFilter]; 

    // In addition to displaying to the screen, write out a processed version of the movie to disk 
    NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"]; 
    unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie 

    //load asset to grab size 
    AVAsset *asset = [AVAsset assetWithURL:self.post.videoUrl]; 

    AVAssetTrack *track = asset.tracks.firstObject; 
    CGSize exportSize = CGSizeMake(track.naturalSize.height, track.naturalSize.width); 

    movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:[NSURL fileURLWithPath:pathToMovie] size:exportSize]; 
    [blendFilter addTarget:movieWriter]; 

    __weak GPUImageUIElement *weakUIElement = uiElementInput; 
    [filter setFrameProcessingCompletionBlock:^(GPUImageOutput *filter, CMTime frameTime) { 
    [weakUIElement update]; 
    }]; 

    // Configure this for video from the movie file, where we want to preserve all video frames and audio samples 
    movieWriter.shouldPassthroughAudio = YES; 
    movieFile.audioEncodingTarget = movieWriter; 
    [movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter]; 

    [movieWriter startRecording]; 
    [movieFile startProcessing]; 

    __weak PostPreviewViewController *weakSelf = self; 
    [movieWriter setCompletionBlock:^{ 
    UISaveVideoAtPathToSavedPhotosAlbum(pathToMovie, weakSelf, @selector(image:completedSavingLocallyWithError:usingContextInfo:), NULL); 
    }]; 
}