Cómo grabar video usando AVCaptureVideoDataOutput

Estoy utilizando AVCaptureSession para get la salida de la camera y he agregado con éxito las inputs y salidas de audio y video.

{ var captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) as AVCaptureDevice var error: NSError? = nil do { //remove the previous inputs let inputs = cameraSession.inputs as! [AVCaptureDeviceInput] for oldInput:AVCaptureDeviceInput in inputs { cameraSession.removeInput(oldInput) } cameraSession.beginConfiguration() if cameraPosition.isEqualToString("Front") { captureDevice = cameraWithPosition(.Front)! } else { captureDevice = cameraWithPosition(.Back)! } let deviceInput = try AVCaptureDeviceInput(device: captureDevice) if (cameraSession.canAddInput(deviceInput) == true) { cameraSession.addInput(deviceInput) } let dataOutput = AVCaptureVideoDataOutput() dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString) : NSNumber(unsignedInt: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)] dataOutput.alwaysDiscardsLateVideoFrames = true if (cameraSession.canAddOutput(dataOutput) == true) { cameraSession.addOutput(dataOutput) } let audioCheck = AVCaptureDevice.devicesWithMediaType(AVMediaTypeAudio) if audioCheck.isEmpty { print("no audio device") return } let audioDevice: AVCaptureDevice! = audioCheck.first as! AVCaptureDevice var audioDeviceInput: AVCaptureDeviceInput? do { audioDeviceInput = try AVCaptureDeviceInput(device: audioDevice) } catch let error2 as NSError { error = error2 audioDeviceInput = nil } catch { fatalError() } if error != nil{ print(error) let alert = UIAlertController(title: "Error", message: error!.localizedDescription , prefernetworkingStyle: .Alert) alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) } if cameraSession.canAddInput(audioDeviceInput){ cameraSession.addInput(audioDeviceInput) } cameraSession.commitConfiguration() let queue = dispatch_queue_create("com.invasivecode.videoQueue", DISPATCH_QUEUE_SERIAL) dataOutput.setSampleBufferDelegate(self, queue: queue) } catch let error as NSError { NSLog("\(error), \(error.localizedDescription)") } } 

Usar AVCaptureMovieFileOutput es capaz de save el video de salida en la biblioteca de fotos usando

 movieFileOutput.startRecordingToOutputFileURL( outputFilePath, recordingDelegate: self) 

pero uso AVCaptureVideoDataOutput como salida para hacer trabajo extra en los metadatos que obtengo de los delegates e bash grabar el video, pero no puedo get ningún método para iniciar y detener la grabación del video.

Sugiera cómo grabar el video usando AVCaptureVideoDataOutput

Necesita una AVCaptureSession para hacerlo:

 //First add AVCaptureVideoDataOutput to AVCaptureSession AVCaptureSession *_captureSession; _captureSession = [[AVCaptureSession alloc] init]; ......Configuration...... AVCaptureVideoDataOutput *videoOut = [[AVCaptureVideoDataOutput alloc] init]; ......Configuration...... if ( [_captureSession canAddOutput:videoOut] ) { [_captureSession addOutput:videoOut]; } //Then use captureSession to start and stop recording [_captureSession startRunning]; [_captureSession stopRunning]; 

Mire a través de RosyWriterCapturePipeline.m, este es un muy buen ejemplo:

RosyWriter