GPUImage录像的一些备忘

GPUImage在加滤镜、图像处理上面集成的很完善,现在在使用的时候碰到的一些问题做下总结备忘,这些都是弄了很长时间才找到原因,如果碰到相似的可以节省下时间。当然简单的使用也可以看这个文章《IOS使用GPUImage滤镜初级试水

1、摄像头左右相反

使用GPU录像的时候,如果前置摄像头在左右是相反的,那么需要设置下镜像设置了

//如果是调用前置摄像头就发现成的像是左右相反了,就看下是不是开启了镜像
self.videoCamera.horizontallyMirrorFrontFacingCamera = YES;
self.videoCamera.horizontallyMirrorRearFacingCamera = NO;

2、开始录像的时候会图像会高亮下

如果录像的额时候,图像高亮下,可以在初始化GPUImageVideoCamera的时候,加上

[m_videoCamera addAudioInputsAndOutputs];

这个是为camera的增加的声音输入,但是这个如果是在一个界面的时候,频繁的增加有可能会造成录制的画面卡顿,所以可以在界面初始化Viewdidload的时候,初始化camera的时候加上这个代码。

3、第一帧偶尔黑屏

如果第一帧黑屏,应该是在开始录制等时间的问题,可以把要使用的视频第一帧裁减掉,裁剪视频的方案晚点会写成一个总结。

4、暂停/重录视频

如果仅仅是暂停录制,不暂停摄像头画面

//录制暂停
[m_movieWriter setPaused:true];
//录制继续
[m_movieWriter setPaused:false];

如果是暂停录制,并且也要暂停摄像头画面

//录制暂停
[m_movieWriter setPaused:true];
//摄像头暂停
[m_videoCamera pauseCameraCapture];
//录制继续
[m_movieWriter setPaused:false];
//摄像头继续
[m_videoCamera resumeCameraCapture];

5、暂停又重录会黑屏或者最后几秒有画面没声音

可以进入GPUImageMovieWriter.m中,在

- (void)processAudioBuffer:(CMSampleBufferRef)audioBuffer;

把里面的一段函数先注销掉

if (offsetTime.value > 0) {
     CFRelease(audioBuffer);
     audioBuffer = [self adjustTime:audioBuffer by:offsetTime];
     CFRetain(audioBuffer);
}

6、视频录制的代码

6.1、设置变量

//摄像头
    GPUImageVideoCamera *m_videoCamera;
    GPUImageFilter *m_customFilter;
    GPUImageView *m_filteredVideoView;
    //录制
    GPUImageMovieWriter *m_movieWriter;
    NSMutableDictionary * videoSettings; //字面意思.视频设置
    NSDictionary * audioSettings; //声音设置

6.2、初始化摄像头

m_videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:AVCaptureDevicePositionBack];
//    [m_videoCamera setFrameRate:60];
    //前置摄像头
    m_videoCamera.horizontallyMirrorFrontFacingCamera = YES;
    m_videoCamera.horizontallyMirrorRearFacingCamera = NO;
    m_videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    [m_videoCamera addAudioInputsAndOutputs];
//    m_customFilter = [[AmomoFilter alloc] init];
    m_customFilter = [[GPUImageFilter alloc] init];
    
    m_filteredVideoView = [[GPUImageView alloc] initWithFrame:CGRectMake(0.0,0.0,JLXScreenSize.width,JLXScreenSize.height)];
    // Add the view somewhere so it's visible
    [self.view addSubview:m_filteredVideoView];
    
    //添加滤镜
    [m_videoCamera addTarget:m_customFilter];
    [m_customFilter addTarget:m_filteredVideoView];
    [m_videoCamera startCameraCapture];

6.3、初始化movieWriter

-(void)initcustomWriter
{
    oldVideoPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie4.mov"];
    unlink([oldVideoPath UTF8String]); // 如果已经存在文件,AVAssetWriter会有异常,删除旧文件
    NSURL *movieURL = [JLXNetTools conVertToURL:oldVideoPath];
    
    //视频设置
    videoSettings = [[NSMutableDictionary alloc] init];
    [videoSettings setObject:AVVideoCodecH264 forKey:AVVideoCodecKey];
    [videoSettings setObject:[NSNumber numberWithInteger:720] forKey:AVVideoWidthKey]; //视频的宽度,这里最好是定义imageCamera时候的宽度
    [videoSettings setObject:[NSNumber numberWithInteger:1280] forKey:AVVideoHeightKey]; //视频的高度.同上
    
    //音频设置
    AudioChannelLayout channelLayout;
    memset(&channelLayout, 0, sizeof(AudioChannelLayout));
    channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
    audioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                     [ 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];
    
    m_movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(720, 1280) fileType:AVFileTypeQuickTimeMovie outputSettings:videoSettings];
//    [m_movieWriter setHasAudioTrack:YES audioSettings:audioSettings];
    [m_movieWriter setHasAudioTrack:true];
    m_videoCamera.audioEncodingTarget = m_movieWriter; //设置声音
//    m_movieWriter.assetWriter.movieFragmentInterval = kCMTimeInvalid; //Couldn't write a frame
    m_movieWriter.encodingLiveVideo = YES;
    [m_customFilter addTarget:m_movieWriter];
}

6.4、开始录制

[m_movieWriter startRecording];

6.5、结束录制

 [m_movieWriter finishRecordingWithCompletionHandler:^{
    }];
 [m_customFilter removeTarget:m_movieWriter];
 m_videoCamera.audioEncodingTarget = nil;

6.6、暂停/重录视频

如果仅仅是暂停录制,不暂停摄像头画面

//录制暂停
[m_movieWriter setPaused:true];
//录制继续
[m_movieWriter setPaused:false];

如果是暂停录制,并且也要暂停摄像头画面

//录制暂停
[m_movieWriter setPaused:true];
//摄像头暂停
[m_videoCamera pauseCameraCapture];
//录制继续
[m_movieWriter setPaused:false];
//摄像头继续
[m_videoCamera resumeCameraCapture];

6.7、取消录制

[m_movieWriter cancelRecording];

七、视频滤镜

7.1、一个滤镜

    //添加滤镜
m_customFilter = [[GPUImageFilter alloc] init];
[m_videoCamera addTarget:m_customFilter];
[m_customFilter addTarget:m_filteredVideoView];
[m_customFilter addTarget:m_movieWriter];

7.2、多个滤镜

多个滤镜需要使用滤镜组

//漫画
    GPUImageSketchFilter *disFilter = [[GPUImageSketchFilter alloc] init];
    //褐色
    GPUImageSepiaFilter* sepiaFilter = [[GPUImageSepiaFilter alloc] init];
    
    filterGroup = [[GPUImageFilterGroup alloc] init];
    [filterGroup addFilter:disFilter];
    [filterGroup addFilter:sepiaFilter];
    
    //先后顺序
    [disFilter addTarget:sepiaFilter];
    
    //开始的滤镜
    [filterGroup setInitialFilters:[NSArray arrayWithObject:disFilter]];
    [filterGroup setTerminalFilter:sepiaFilter];
    
    [filterGroup addTarget:m_filteredVideoView];
    [m_videoCamera addTarget:filterGroup];
 
    [filterGroup addTarget:m_movieWriter];
最后修改:2017 年 04 月 21 日 03 : 31 PM
如果看了这个文章可以让你少加会班,可以请我喝杯可乐
已打赏名单
微信公众号

发表评论