使用GPUImage开启的相机进行摄像,保存写入到Path

之前已经有一篇博客讲过怎么开启摄像头并完成对摄像头的图像的滤镜化了,现在就说说怎么录像,并把这个添加滤镜的录像文件写到Path

原理是GPUImage给出了GPUImageMovieWriter这么个类,专门用于记录摄像头的录像。设定了路径和声音视频参数以后,把GPUImageMovieWriter的对象委托给滤镜对象,再把滤镜对象委托给VideoCamera就可以开始录制了。

首先定义:

    GPUImageMovieWriter * movieWriter;

    GPUImageVideoCamera * videoCamera;

    NSMutableDictionary * videoSettings;

    NSDictionary * audioSettings;

    NSString * pathToMovie;

这些都是录像必备的属性

接下来是初始化的工作

首先初始化声音和视频参数:

    //init Video Setting

    videoSettings = [[NSMutableDictionaryalloc] init];;

    [videoSettingssetObject:AVVideoCodecH264forKey:AVVideoCodecKey];

    [videoSettingssetObject:[NSNumbernumberWithInteger:200] forKey:AVVideoWidthKey];

    [videoSettingssetObject:[NSNumbernumberWithInteger:200] forKey:AVVideoHeightKey];

    

    //init audio setting

    AudioChannelLayout channelLayout;

    memset(&channelLayout, 0, sizeof(AudioChannelLayout));

    channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;

    

    audioSettings = [NSDictionarydictionaryWithObjectsAndKeys:

                                   [ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,

                                   [ NSNumber numberWithInt: 2 ], AVNumberOfChannelsKey,

                                   [ NSNumber numberWithFloat: 16000.0 ], AVSampleRateKey,

                                   [ NSData dataWithBytes:&channelLayout length: sizeof( AudioChannelLayout ) ], AVChannelLayoutKey,

                                   [ NSNumber numberWithInt: 32000 ], AVEncoderBitRateKey,

                                   nil];

然后是初始化文件路径和视频写入对象

    //init Movie path

    pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:thePath];

    unlink([pathToMovieUTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie

    NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];

    

    //init movieWriter

    movieWriter = [[GPUImageMovieWriteralloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 640.0) fileType:AVFileTypeMPEG4outputSettings:videoSettings];

    

    [movieWritersetHasAudioTrack:YESaudioSettings:audioSettings];


接下来是GPUImageVideoCamera和滤镜效果的初始化,我就不写了,看我另外一篇博客

初始化的最后动作就是赋予委托:

    //把滤镜效果加给摄像头

    [videoCameraaddTarget:testFilter];

    //把摄像头上的图像给GPUImageView显示出来

    [testFilteraddTarget:imageView];

    [testFilteraddTarget:movieWriter];

这样初始化的工作就全部做完了,要开始录制只要开启以下代码:

        videoCamera.audioEncodingTarget = movieWriter;

        [movieWriterstartRecording];

就可以开始录制了,结束录制也很简单:

        //stop recording

        [testFilterremoveTarget:movieWriter];

        [movieWriterfinishRecording];

录制完的视频将会保存在路径里

 

还有两个小设置:

开启和关闭闪光灯:

        [videoCamera.inputCamerasetTorchMode:AVCaptureTorchModeOn];

        [videoCamera.inputCameraunlockForConfiguration];


        [videoCamera.inputCameralockForConfiguration:nil];

        [videoCamera.inputCamerasetTorchMode:AVCaptureTorchModeOff];

        [videoCamera.inputCameraunlockForConfiguration];


posted @ 2013-10-30 09:17  wisejoker  阅读(3043)  评论(3编辑  收藏  举报