如何在Swift中使用GPUImage将图像添加到视频中

时间:2016-09-13 02:03:36

标签: ios swift swift2 gpuimage

我有一些代码可以使用GPUImage将图像添加到视频中,但不知怎的,它根本无法正常工作。我已经搜索了一些解决方案,但仍然存在这个问题 我正在尝试在重新编码完成时合并图像。现在输出只是一个没有图像的视频 这是我的代码。

override func viewDidLoad(){
   super.viewDidLoad

   setupCamera()
}

func setupCamera(){
    let myBoundSize: CGSize = UIScreen.mainScreen().bounds.size

    cameraSubPreview = GPUImageView(frame: CGRectMake(0, 0, myBoundSize.width, myBoundSize.height))
    cameraInput = GPUImageVideoCamera(sessionPreset: AVCaptureSessionPreset1280x720, cameraPosition: .Front)
    cameraInput.horizontallyMirrorFrontFacingCamera = true
    cameraInput.outputImageOrientation = .Portrait

    cameraInput.addTarget(mainVideoFilter)

    mainVideoFilter.addTarget(cameraSubPreview)
    cameraPreview.addSubview(cameraSubPreview)
    cameraInput.startCameraCapture()
}

func setupFilter(){
     let logoImageForGPU = GPUImagePicture(image: logoImageView.image)

     logoImageForGPU.addTarget(transformFilter)
     logoImageForGPU.processImage()

     // apply transform to filter
     let tx: CGFloat = logoImageView.frame.size.width
     let ty: CGFloat = logoImageView.frame.size.height
     print("tx and ty: \(tx), \(ty)")
     let t: CGAffineTransform = CGAffineTransformMakeScale(tx, ty);
     transformFilter.affineTransform = t

    transformFilter.addTarget(mainVideoFilter, atTextureLocation: 1)
}

func startRecordingVideo(sender: AnyObject) {
        if(sender.state == UIGestureRecognizerState.Began) {
            if !self.isRecording {

            setupFilter()

            let ud = NSUserDefaults.standardUserDefaults()
            var IntForFilePath : Int? = ud.objectForKey("IntForFilePath") as? Int
            if IntForFilePath == nil{
                IntForFilePath = 10000
            }

            let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
            let documentsDirectory = paths[0]
            IntForFilePath = IntForFilePath! + 1
            let filePath : String? = "\(documentsDirectory)/temp\(IntForFilePath!).mp4"
            ud.setObject(IntForFilePath, forKey: "IntForFilePath")
            // URL.
            let fileURL : NSURL = NSURL(fileURLWithPath: filePath!)

            //print(filePath)
            self.delegate.filePathForVideo = filePath!

            movieWriter = GPUImageMovieWriter(movieURL: fileURL, size: CGSize(width: 480, height: 800))
            movieWriter.shouldPassthroughAudio = true
            //cameraInput.addTarget(stillImageFilter)
            //print("sif: \()")
            stillImageFilter.addTarget(movieWriter)
            cameraInput.audioEncodingTarget = movieWriter

            // start recording
            movieWriter.startRecording()

            circle.hidden = false
            drawCircleAnimation("strokeEnd", animeName: "updateGageAnimation", fromValue: 0.0, toValue: 1.0, duration: 10.0, repeat: 1, flag: false)

            self.isRecording = true
        }
    } else if (sender.state == UIGestureRecognizerState.Ended) {
        if self.isRecording {
            //stop recording
            movieWriter.finishRecording()

            stopCircleAnimation()
            self.isRecording = false

            self.moveToVideoSaveView()
        }
    }
}

如果您有任何想法可以解决这个问题,请告诉我。
提前谢谢。

0 个答案:

没有答案