iOS ObjectC GPUImage视频滤镜录制和保存

GPUImage 作为一个优秀的视频处理框架,不可谓不流行,so,我也蹭一下。一下通过代码来告诉大家怎么实现视频录制时加滤镜以及保存到本地。 看了代码有不懂的欢迎私信~~

#import "BBSRecordController.h"#import "BBSRecordHandler.h"#import#import#import#import "LZBPlayerViewController.h"

@interface BBSRecordController ()

@property (nonatomic, strong) UIButton *changeCameraButton;

@property (nonatomic, strong) BBSRecordHandler *recordHandler;

@property (nonatomic, strong) GPUImageVideoCamera *videoCamera;

@property (nonatomic, strong) GPUImageMovieWriter *movieWriter;

@property (nonatomic, strong) GPUImageView *showView;

@property (nonatomic, strong) GPUImageRGBFilter *invert;

@property (nonatomic, strong) NSString *pathToMovie;

@end

@implementation BBSRecordController

- (void)viewDidLoad {

    [super viewDidLoad];


    self.view.backgroundColor = [UIColor whiteColor];

    self.pathToMovie = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"aiMovie.mp4"];

    self.videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPresetHigh  cameraPosition:AVCaptureDevicePositionBack];

    self.videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

    self.videoCamera.horizontallyMirrorFrontFacingCamera = YES;

    [self.videoCamera addAudioInputsAndOutputs];


    self.invert = [[GPUImageRGBFilter alloc] init];

    self.invert.red = 0.5;

    self.invert.green = 0.3;

    self.invert.blue = 0.6;

    [self.videoCamera addTarget:self.invert];


    self.showView = [[GPUImageView alloc] initWithFrame:self.view.frame];

    self.showView.center = self.view.center;

    [self.view addSubview:self.showView];

    [self.invert addTarget:self.showView];

    [self.videoCamera startCameraCapture];


    UIButton *recordButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [recordButton setTitle:@"录制" forState: UIControlStateNormal];

    [recordButton setTitle:@"停止" forState: UIControlStateSelected];

    [recordButton addTarget:self action:@selector(didTapRecordButton:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:recordButton];


    [recordButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerX.equalTo(self.view);

        make.height.mas_equalTo(100);

        make.width.mas_equalTo(60);

        make.bottom.equalTo(self.view).offset(-50);

    }];

    [self.view addSubview:self.changeCameraButton];

    [self.changeCameraButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.equalTo(self.view).offset(-20);

        make.top.equalTo(self.view).offset(20);

    }];


    UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [saveButton setTitle:@"保存" forState: UIControlStateNormal];

    [saveButton addTarget:self action:@selector(saveVideo) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:saveButton];


    [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.view).offset(20);

        make.top.equalTo(self.view).offset(20);

    }];

}

#pragma mark - EventResponse

- (void)didTapChangeCameraButton {

    [self.videoCamera rotateCamera];

}

- (void)didTapRecordButton:(UIButton *)sender {

    if (!sender.selected) {

        sender.selected = YES;

        unlink([self.pathToMovie UTF8String]);


        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            [self.invert addTarget:self.movieWriter];

            self.videoCamera.audioEncodingTarget = self.movieWriter;

            [self.movieWriter startRecording];

        });

    }

    else {

        sender.selected = NO;

        [self.invert removeTarget:self.movieWriter];

        self.videoCamera.audioEncodingTarget = nil;

        [self.movieWriter finishRecordingWithCompletionHandler:^{

            [self saveVideo];

            self.movieWriter = nil;

        }];

    }

}

- (void)saveVideo {

//    NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];

    BOOL exists =  [[NSFileManager defaultManager] fileExistsAtPath:self.pathToMovie];

    if (!exists) {

        NSLog(@"不存在");

        return;

    }

    if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.pathToMovie)) {

        //保存相册核心代码

        UISaveVideoAtPathToSavedPhotosAlbum(self.pathToMovie, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);

    }

}

//保存视频完成之后的回调

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {

    if (error) {

        NSLog(@"保存视频失败%@", error.localizedDescription);

    }

    else {

        NSLog(@"保存视频成功");

    }

}

#pragma mark - Getters & Setters

- (UIButton *)changeCameraButton {

    if (_changeCameraButton) {

        return _changeCameraButton;

    }

    _changeCameraButton = [UIButton buttonWithType:UIButtonTypeCustom];

    _changeCameraButton.backgroundColor = [UIColor clearColor];

    [_changeCameraButton setTitle:@"切换" forState:UIControlStateNormal];

    [_changeCameraButton addTarget:self action:@selector(didTapChangeCameraButton) forControlEvents:UIControlEventTouchUpInside];

    return _changeCameraButton;

}

- (GPUImageMovieWriter *)movieWriter {

    if (_movieWriter) {

        return _movieWriter;

    }

    NSURL *movieURL = [NSURL fileURLWithPath:self.pathToMovie];

    NSMutableDictionary *settings = [[NSMutableDictionary alloc] init];

    [settings setObject:AVVideoCodecTypeH264 forKey:AVVideoCodecKey];

    [settings setObject:[NSNumber numberWithInteger:720] forKey:AVVideoWidthKey];

    [settings setObject:[NSNumber numberWithInteger:1280] forKey:AVVideoHeightKey];

    _movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(720 , 1280) fileType:AVFileTypeMPEG4 outputSettings:settings];

    _movieWriter.encodingLiveVideo = YES;

    _movieWriter.assetWriter.movieFragmentInterval = kCMTimeInvalid;

    return _movieWriter;

}

@end

你可能感兴趣的:(iOS ObjectC GPUImage视频滤镜录制和保存)