Search code examples
iosswiftgpuimage

GPUImage Video Recording


I have been trying to create use the GPUImage library to record a video from camera, filter it and save to camera roll.

The GPUImageMovieWriter is causing me some issues. Here is my code:

    //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()

The app crashes with the following debugger output (when startRecording() method is called ):

[AVAssetWriter startWriting] Cannot call method when status is 3

Any clues as to why this is occurring? Why is the AVAssetWriter's Status being .Failed?

Note that all variables are properties of my class.


Solution

  • Got the same error. It seems that the movieURL is builded wrongly. Fixed when changed code to

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