2016-08-16 25 views
10

Korzystam z AVCaptureSession, aby uzyskać wyjście kamery i pomyślnie dodać wejścia i wyjścia audio i wideo.Sposób rejestrowania wideo przy użyciu AVCaptureVideoDataOutput

{ 

    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 
       , preferredStyle: .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)") 
    } 
} 

Korzystanie AVCaptureMovieFileOutput jestem w stanie zapisać wyjście wideo w bibliotece zdjęć przy użyciu

movieFileOutput.startRecordingToOutputFileURL(outputFilePath, recordingDelegate: self) 

ale używam AVCaptureVideoDataOutput jako wyjście do wykonywania dodatkowej pracy na danych meta i uzyskać od deleguje i próbuje nagrać wideo, ale nie mogę uzyskać żadnych metod, aby rozpocząć i zatrzymać nagrywanie wideo.

Zaproponuj Jak nagrać wideo za pomocą AVCaptureVideoDataOutput

+0

Skorzystaj z tego na [github] (https://gist.github.com/hayago/93108 55). –

Odpowiedz

2

Potrzebujesz AVCaptureSession to zrobić:

//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]; 

Proszę przejrzeć RosyWriterCapturePipeline.m, jest to bardzo dobry przykład:

RosyWriter

+1

Szukam kodu w Swift nie w Objective C – Rajjjjjj

+0

Chodzi o to, że ludzie nie mogą znaleźć sposobu, jak zdefiniować lokalizację zapisu i jak użyć "videoOut" do zapisania, która nie zawiera odpowiedzi. Jestem przy okazji jedną z osób. – Hope