zoukankan      html  css  js  c++  java
  • 使用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];


  • 相关阅读:
    基础语法;
    layabox里面的ui组件之RadioGroup
    github删除带有文件的文件夹
    【转】NHibernate:no persister for 异常
    MVC乱码可能的原因
    【转】局域网内访问VS2012 调试的IIS Express web服务器
    Hightchart y轴不允许显示小数
    WCF 断点不会命中
    Web Form 取消手机端自动转换
    Sharepoint的javascript客户端对象模型获取其他站点的list
  • 原文地址:https://www.cnblogs.com/wisejoker/p/3399837.html
Copyright © 2011-2022 走看看