ios save GPUImage video

强颜欢笑 提交于 2019-12-05 07:47:15

问题


I'm add overlay over my video using GPUImage. On preview it all looks great but how can I write my video to file? I use GPUMovieWriter but I cant find this file and I dont even know if it works. Here is my code:

-(void)setUpCameraWithPosition:(bool)switchToFrontCamera
{
    if(videoCamera != nil)
    {
        [videoCamera stopCameraCapture];
    }

    if(switchToFrontCamera) {
        videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionFront];
    }
    else {
        videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
    }

    videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    videoCamera.horizontallyMirrorFrontFacingCamera = NO;
    videoCamera.horizontallyMirrorRearFacingCamera = NO;

    filter = [GPUImageiOSBlurFilter new];
    filter.blurRadiusInPixels = 3.0;

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

    NSDate *startTime = [NSDate date];

    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 240.0f, 320.0f)];
    timeLabel.font = [UIFont systemFontOfSize:17.0f];
    timeLabel.text = @"Time: 0.0 s";
    timeLabel.textAlignment = UITextAlignmentCenter;
    timeLabel.backgroundColor = [UIColor clearColor];
    timeLabel.textColor = [UIColor whiteColor];

    [videoCamera addTarget:filter];
    GPUImageView *filterView = (GPUImageView *)self.preview;
    filterView.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;

    NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"file.mov"];
    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];
    movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 640.0)];
    movieWriter.encodingLiveVideo = YES;

    [filter addTarget:movieWriter];

    uiElementInput = [[GPUImageUIElement alloc] initWithView:timeLabel];
    [filter addTarget:blendFilter];
    [uiElementInput addTarget:blendFilter];

    [blendFilter addTarget:filterView];

    __unsafe_unretained GPUImageUIElement *weakUIElementInput = uiElementInput;

    [filter setFrameProcessingCompletionBlock:^(GPUImageOutput * filter, CMTime frameTime){
        timeLabel.text = [NSString stringWithFormat:@"Time: %f s", -[startTime timeIntervalSinceNow]];
        [weakUIElementInput update];
    }];

    [videoCamera startCameraCapture];
}

How can I write my movie to AssetsLibrary?


回答1:


I assume by overlay you mean you want to write the video with the UIElement as the overlay?

In this case you would want to be writing out the via your "blendFilter" and not "filter"

Remove this line

[filter addTarget:movieWriter];

and add this after initializing the blendFilter

[blendFilter addTarget:movieWriter];
[blendFilter addTarget:filterView];

Your initialization of the movieWriter seems ok, the only difference I have is that I use ".m4v" as the video format. That is Apple's standard format.

The video should save to the applications home folder. Use the "Orginizer" ("CMD + SHIFT + 2") in XCode to look at the applications folder, the video should be under "Documents".



来源:https://stackoverflow.com/questions/23679688/ios-save-gpuimage-video

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!