音视频采集笔记 (三)

前言

前面实现都是通过系统的框架来实现视频的实时滤镜采集(音视频采集笔记(二)),本文主要讲解通过第三方框架GPUImage2来实现视频的实时滤镜采集 (采集实现Demo)

GPUImage2使用Swift编写,是GPUImage框架的第二代,支持macOS、iOS和Swift代码的Linux或未来平台,GPUImage框架的源码全部公开,有兴趣的同学可以深入的研究一下


滤镜.gif

流程

1.创建Camera,绑定滤镜输出到renderView,开始捕捉数据
2.创建自定义视图
3.开始录制,通过MovieOutput写入数据
  • 创建Camera,绑定滤镜输出到renderView,开始捕捉数据
    var fliters = [SketchFilter(), ThresholdSobelEdgeDetection(), ThresholdSketchFilter()] as [Any]
    var camera : Camera!
    var fliter : BasicOperation!

    fileprivate lazy var renderView : RenderView = {
        let renderView = RenderView(frame: CGRect(x: 0, y: 0 , width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        return renderView
    }()

    fileprivate func setUpCamera() {

        view.addSubview(renderView)

        do {
            // 创建一个Camera的实例,Camera遵循ImageSource协议,用来从相机捕获数据
            camera = try Camera(sessionPreset: .hd1280x720)
            // 绑定处理链
            camera --> fliter --> renderView
            // 开始捕捉数据
            camera.startCapture()
        } catch {

        }
    }
-->运算符

-->是GPUImage2定义的一个中缀运算符,它将两个对象像链条一样串联起来,用起来像是这样:
camera --> basicOperation --> renderView

左边的参数遵循ImageSource协议,作为数据的输入,右边的参数遵循ImageConsumer协议,作为数据的输出。这里的basicOperation是BasicOperation的一个实例,其父类ImageProcessingOperation同时遵循ImageSource和ImageConsumer协议,所以它可以放在-->的左边或右边。-->的运算是左结合的,类似于GPUImage中的addTarget方法,但是-->有一个返回值,就是右边的参数。在上面的示例中,先计算了前半部camera --> basicOperation,然后右边的参数basicOperation作为返回值又参与了后半部basicOperation --> renderView的计算。

-->体现了链式编程的思想,让代码更加优雅,在GPUImage2有着大量运用,这得益于Swift强大的语法,关于Swift中的高级运算符

  • 创建自定义视图
    var videoCustomView = VideoCustomView()

    fileprivate func setUpUI() {
        videoCustomView.delegate = self
        view.addSubview(videoCustomView)

        videoCustomView.snp.makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
        }
    }
  • 开始录制,通过MovieOutput写入数据
    func clickPlayButton(isPlay: Bool) {
        if isPlay {
        
            do {
                // 移除之前存储的数据
                removeData()
                let videoCompressionSettings = [AVVideoCodecKey : AVVideoCodecType.h264,
                                                AVVideoScalingModeKey : AVVideoScalingModeResizeAspectFill,
                                                AVVideoWidthKey : 200,
                                                AVVideoHeightKey : SCREEN_WIDTH] as [String : Any]

                movieOutPut = try MovieOutput(URL: videoFileUrl, size: Size(width: Float(200), height: Float(SCREEN_WIDTH)), fileType: .mov, liveVideo: true, settings: videoCompressionSettings as [String : AnyObject])
                // 设置音频
                camera.audioEncodingTarget = movieOutPut
                fliter --> movieOutPut
                // 开始写入数据
                movieOutPut.startRecording()
                videoCustomView.startDisplayLink()

            } catch {

            }

        } else {
            movieOutPut.finishRecording {
                // 清空回到初始状态
                self.reset()
            }
        }
    }

滤镜切换

    func selectFliterType(fliterType: String) {
        fliter.removeAllTargets()
        camera.removeAllTargets()
        fliter = fliters[2] as? BasicOperation
        camera --> fliter --> renderView
    }

相机方向切换

    func switchCamera() {
        self.camera.location = self.camera.location == .backFacing ? .frontFacing : .backFacing
    }

GPUImage2作者提供了这个location属性,来改变相机方向切换,但是作者只留了位置,需要去源码里修改,具体实现如下

  public var location:PhysicalCameraLocation {
        didSet {
            guard let newVideoInput = try? AVCaptureDeviceInput(device: location.device()!) else { return }


            self.captureSession.beginConfiguration()
            self.captureSession.removeInput(self.videoInput)
            self.captureSession.addInput(newVideoInput)
            captureConnection = self.videoOutput.connection(with: .video)

            if captureConnection.isVideoOrientationSupported {
                captureConnection.videoOrientation = .portrait
            }

            captureConnection.isVideoMirrored = location == .frontFacing
            self.captureSession.commitConfiguration()
            self.videoInput = newVideoInput
        }
    }

扩展

GPUImage2 还有很多功能,如图片,视频增加滤镜等, 以及一百多种滤镜,详情可以参考官方Demo

你可能感兴趣的:(音视频采集笔记 (三))