2016-07-15 26 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); 
    }]; 
}