Reputation: 1065
I am using AVCaptureFileOutputRecordingDelegate
to record video. I would like to save one second of video, then change the file path and save the next second somewhere else.
Is this possible? Is there some sort of timer I would have to set up? Below is the code in my record function:
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let outputPath = "\(documentsPath)/output.mov"
outputURL = NSURL(fileURLWithPath: outputPath)
output.startRecording(toOutputFileURL: outputURL as URL!, recordingDelegate: self)
Here is the code in which I set up the output record controller and everything:
override func viewWillAppear(_ animated: Bool) {
let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDuoCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.back)
for device in (deviceDiscoverySession?.devices)!{
if(device.position == AVCaptureDevicePosition.back){
do{
let input = try AVCaptureDeviceInput(device: device )
if captureSession.canAddInput(input){
captureSession.addInput(input)
}
// sessionOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString) : NSNumber(value: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange as UInt32)]
sessionOutput.alwaysDiscardsLateVideoFrames = true
if(captureSession.canAddOutput(sessionOutput) == true){
captureSession.addOutput(sessionOutput)
let previewLayer: AVCaptureVideoPreviewLayer = {
let preview = AVCaptureVideoPreviewLayer(session: self.captureSession)
preview?.bounds = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
preview?.position = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY)
preview?.videoGravity = AVLayerVideoGravityResize
return preview!
}()
view.layer.insertSublayer(previewLayer, at: 0)
output = AVCaptureMovieFileOutput()
let maxDuration = CMTimeMakeWithSeconds(180, 30)
output.maxRecordedDuration = maxDuration
captureSession.addOutput(output)
}
captureSession.commitConfiguration()
}
catch{
print("Error")
}
}
}
let deviceDiscoverySession2 = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInMicrophone], mediaType: AVMediaTypeAudio, position: AVCaptureDevicePosition.unspecified)
for device in (deviceDiscoverySession2?.devices)!{
do{
let input = try AVCaptureDeviceInput(device: device )
if captureSession.canAddInput(input){
captureSession.addInput(input)
}
captureSession.commitConfiguration()
}
catch{
print("Error")
}
}
}
Upvotes: 1
Views: 357
Reputation: 36084
On Mac OS X AVCaptureMovieFileOutput
can do what you want. From a comment in the superclass AVCaptureFileOutput
:
File outputs can start recording to a new file using the startRecordingToOutputFileURL:recordingDelegate: method. On successive invocations of this method on Mac OS X, the output file can by changed dynamically without losing media samples.
On iOS you'll have to drop back to a combination of AVCaptureVideoDataOutput
, AVCaptureAudioDataOutput
and AVAssetWriter
s that you create, start and stop for each segment.
It's not the easiest task. What are you trying to achieve? Could you instead record to a single file then split it into segments in a post-processing phase?
Upvotes: 1