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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With