GPUImage视频录制

时间:2015-03-17 17:59:11

标签: ios swift gpuimage

我一直在尝试创建使用GPUImage库来记录来自摄像头的视频,过滤它并保存到相机胶卷。

GPUImageMovieWriter给我带来了一些问题。这是我的代码:

    //Set up the GPUImageVideoCamera
    videoCamera = GPUImageVideoCamera(sessionPreset: AVCaptureSessionPreset640x480, cameraPosition: .Back)
    videoCamera.outputImageOrientation = .Portrait;
    videoCamera.horizontallyMirrorFrontFacingCamera = true
    videoCamera.horizontallyMirrorRearFacingCamera = false

    //Create filter with view
    filter = GPUImageFilter()
    filterView = GPUImageView(frame: self.view.frame)
    filterView.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;
    self.view.addSubview(filterView)

    pathToMovie = NSHomeDirectory().stringByAppendingPathComponent("stream.mp4")
    unlink((pathToMovie as NSString).UTF8String)
    var movieURL = NSURL.fileURLWithPath(pathToMovie)
    movieWriter = GPUImageMovieWriter(movieURL: movieURL, size: CGSizeMake(640, 480))
    movieWriter.encodingLiveVideo = true

    //Attach targets
    videoCamera.addTarget(filter)
    filter.addTarget(filterView)
    filter.addTarget(movieWriter)

    //Start capture
    videoCamera.startCameraCapture()
    movieWriter.startRecording()

应用程序崩溃时出现以下调试器输出(调用startRecording()方法时):

  

[AVAssetWriter startWriting]状态为3时无法调用方法

有关为何发生这种情况的任何线索?为什么AVAssetWriter的状态为.Failed?

请注意,所有变量都是我班级的属性。

1 个答案:

答案 0 :(得分:2)

得到了同样的错误。似乎movieURL错误地构建了。修改后更改代码

movieURL = NSURL.fileURLWithPath(NSHomeDirectory())
                .URLByAppendingPathComponent("stream.mp4")!
unlink(movieURL.path!)