Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVCaptureMovieFileOutput - recording sequential video clips

I want to record video/audio in a series of fluid, variable length clips. That is, vid1.mp4 followed by vid2.mp4 should connect together seamlessly, or mostly seamlessly.

My current problem is that I cannot seem to switch files immediately without getting errors.

The crux of the issue is this:

func recordNewClip() {
    let file = self.outputUrl()
    let filePath = file!.path!
    try? NSFileManager.defaultManager().removeItemAtPath(filePath)
    movieOutput!.stopRecording()
    movieOutput!.startRecordingToOutputFileURL(file, recordingDelegate: self)
}

If I don't call stopRecording, I get errors, and if I do call stopRecording, only random video clips ever record; most recordings fail.

How can I capture sequential video clips with AVFoundation?

like image 640
Stefan Kendall Avatar asked Oct 29 '25 15:10

Stefan Kendall


1 Answers

Here We Go!!! So, This is a topic that not a lot of people deal with so let me give you some help.

When you Record you have a Session, and an Output

If you read apple's documentation to the delegate methods you will see that the

    startRecordingToOutputFileURL

That will still fire the

    didFinishRecordingToOutputFileAtURL

The problem is that you need to renew the output and add / remove it from the session.

So for swift ->

    ....
    // Remove old output
    session.removeOutput(output);
    // Create new output
    output = AVCaptureMovieFileOutput()
    if session.canAddOutput(output) {
        session.addOutput(output)
        output.startRecordingToOutputFileURL(fileDest, recordingDelegate: self.delegate)
    }  
    .... 

and that will fire the delegate method (DELEGATE METHOD !!!)

    func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
    let outputPath = outputFileURL.path!
    do {
        print(outputPath);
        try NSFileManager.defaultManager().removeItemAtPath(outputPath)
    } catch _ {
        print("error removing file");

    }
    do {
        try NSFileManager.defaultManager().moveItemAtPath(outputFileURL.path!, toPath: outputPath)
    } catch _ {
    print(error);
    }
}

The idea is that you have to do it this way because if you stop recording you need to create a new session and then a new output. This way you only need to create a new output.

Please let me know if this needs any edits or tweaking but it works for me.

like image 181
Andrew Riznyk Avatar answered Oct 31 '25 05:10

Andrew Riznyk