ios保存GPUImage视频 [英] ios save GPUImage video

查看:305
本文介绍了ios保存GPUImage视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GPUImage在我的视频上添加叠加层。在预览中它看起来很棒,但我怎样才能将我的视频写入文件?我使用GPUMovieWriter,但我找不到这个文件,我甚至不知道它是否有效。这是我的代码:

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];
}

如何将我的电影写入AssetsLibrary?

How can I write my movie to AssetsLibrary?

推荐答案

我假设你想用UIElement作为叠加层来写视频?

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

在这种情况下,您可能希望通过blendFilter写出而不是过滤

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

删除此行

[filter addTarget:movieWriter];

并在初始化blendFilter后添加此内容

and add this after initializing the blendFilter

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

您对movieWriter的初始化似乎没问题,唯一的区别就是我使用.m4v作为视频格式。这是Apple的标准格式。

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.

视频应保存到应用程序主文件夹。使用XCode中的Orginizer(CMD + SHIFT + 2)查看应用程序文件夹,视频应位于文档下。

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".

这篇关于ios保存GPUImage视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆