2011-02-09 16 views
29

Voglio registrare video e catturare fotogrammi allo stesso tempo con il mio codice.È possibile utilizzare AVCaptureVideoDataOutput e AVCaptureMovieFileOutput contemporaneamente?

Sto usando AVCaptureVideoDataOutput per le cornici di sostegno e AVCaptureMovieFileOutput per la registrazione video. Ma non può funzionare e ottenere il codice di errore -12780 mentre si lavora allo stesso tempo ma individualmente.

Ho cercato questo problema ma non ho ricevuto risposta. Qualcuno ha la stessa esperienza o spiega? Mi dà molto fastidio per un po 'di tempo.

grazie.

+0

_ "Il video può essere catturato direttamente su file con AVCaptureMovieFileOutput. Tuttavia, questa classe non ha dati visualizzabili e ** non può ** essere utilizzata contemporaneamente con AVCaptureVideoDataOutput." _ Trovato qui: [collegamento] (https: // developer.xamarin.com/api/type/MonoTouch.AVFoundation.AVCaptureSession/) .. solo per chiarire la vera causa del problema – Csharpest

risposta

48

Non posso rispondere alla specifica domanda put, ma ho la registrazione con successo il video e afferrando fotogrammi al tempo stesso utilizzando:

  • AVCaptureSession e AVCaptureVideoDataOutput a telai di rotta nel mio proprio codice
  • AVAssetWriter, AVAssetWriterInput e AVAssetWriterInputPixelBufferAdaptor a scrivere fotogrammi fuori ad un file di film codificati H.264

Questo è senza indagare audio. Alla fine ottengo CMSampleBuffers dalla sessione di acquisizione e poi li spingo nell'adattatore del buffer dei pixel.

EDIT: quindi il mio codice è più o meno simile, con i bit che stai avendo problemi con scremato più e ignorando i problemi di portata:

/* to ensure I'm given incoming CMSampleBuffers */ 
AVCaptureSession *captureSession = alloc and init, set your preferred preset/etc; 
AVCaptureDevice *captureDevice = default for video, probably; 

AVCaptureDeviceInput *deviceInput = input with device as above, 
            and attach it to the session; 

AVCaptureVideoDataOutput *output = output for 32BGRA pixel format, with me as the 
            delegate and a suitable dispatch queue affixed. 

/* to prepare for output; I'll output 640x480 in H.264, via an asset writer */ 
NSDictionary *outputSettings = 
    [NSDictionary dictionaryWithObjectsAndKeys: 

      [NSNumber numberWithInt:640], AVVideoWidthKey, 
      [NSNumber numberWithInt:480], AVVideoHeightKey, 
      AVVideoCodecH264, AVVideoCodecKey, 

      nil]; 

AVAssetWriterInput *assetWriterInput = [AVAssetWriterInput 
            assetWriterInputWithMediaType:AVMediaTypeVideo 
                outputSettings:outputSettings]; 

/* I'm going to push pixel buffers to it, so will need a 
    AVAssetWriterPixelBufferAdaptor, to expect the same 32BGRA input as I've 
    asked the AVCaptureVideDataOutput to supply */ 
AVAssetWriterInputPixelBufferAdaptor *pixelBufferAdaptor = 
      [[AVAssetWriterInputPixelBufferAdaptor alloc] 
       initWithAssetWriterInput:assetWriterInput 
       sourcePixelBufferAttributes: 
        [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithInt:kCVPixelFormatType_32BGRA], 
          kCVPixelBufferPixelFormatTypeKey, 
        nil]]; 

/* that's going to go somewhere, I imagine you've got the URL for that sorted, 
    so create a suitable asset writer; we'll put our H.264 within the normal 
    MPEG4 container */ 
AVAssetWriter *assetWriter = [[AVAssetWriter alloc] 
           initWithURL:URLFromSomwhere 
           fileType:AVFileTypeMPEG4 
           error:you need to check error conditions, 
             this example is too lazy]; 
[assetWriter addInput:assetWriterInput]; 

/* we need to warn the input to expect real time data incoming, so that it tries 
    to avoid being unavailable at inopportune moments */ 
assetWriterInput.expectsMediaDataInRealTime = YES; 

... eventually ... 

[assetWriter startWriting]; 
[assetWriter startSessionAtSourceTime:kCMTimeZero]; 
[captureSession startRunning]; 

... elsewhere ... 

- (void)  captureOutput:(AVCaptureOutput *)captureOutput 
    didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
      fromConnection:(AVCaptureConnection *)connection 
{ 
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 

    // a very dense way to keep track of the time at which this frame 
    // occurs relative to the output stream, but it's just an example! 
    static int64_t frameNumber = 0; 
    if(assetWriterInput.readyForMoreMediaData) 
     [pixelBufferAdaptor appendPixelBuffer:imageBuffer 
         withPresentationTime:CMTimeMake(frameNumber, 25)]; 
    frameNumber++; 
} 

... and, to stop, ensuring the output file is finished properly ... 

[captureSession stopRunning]; 
[assetWriter finishWriting]; 
+3

Ti dispiace inviare un codice di esempio su come farlo? Il tuo karma della vita reale aumenterebbe di 10 volte !!! : D – SpaceDog

+8

Oh, c'è il karma coinvolto? Quindi è stato aggiunto un codice di esempio molto semplice! – Tommy

+2

Grazie per il codice, ho capito che funziona con le immagini. Che ne pensi di aggiungere suoni al video, qualche indizio? –

1

Questa è una versione veloce di risposta di Tommy .

// Set up the Capture Session 
// Add the Inputs 
// Add the Outputs 


var outputSettings = [ 
    AVVideoWidthKey : Int(640), 
    AVVideoHeightKey : Int(480), 
    AVVideoCodecKey : .h264 
] 

    var assetWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo,outputSettings: outputSettings) 

    var pixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput, sourcePixelBufferAttributes: 
     [ kCVPixelBufferPixelFormatTypeKey : Int(kCVPixelFormatType_32BGRA)]) 


    var assetWriter = AVAssetWriter(url: URLFromSomwhere, fileType: AVFileTypeMPEG4 , error : Error) 
     assetWriter.addInput(assetWriterInput) 
     assetWriterInput.expectsMediaDataInRealTime = true 
     assetWriter.startWriting() 
     assetWriter.startSession(atSourceTime: kCMTimeZero) 

    captureSession.startRunning() 


    func captureOutput(_ captureOutput: AVCaptureOutput, didOutputSampleBuffer sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { 

    var imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) 
    // a very dense way to keep track of the time at which this frame 
    // occurs relative to the output stream, but it's just an example! 
    var frameNumber: Int64 = 0 

      if assetWriterInput.readyForMoreMediaData { 
    pixelBufferAdaptor.appendPixelBuffer(imageBuffer, withPresentationTime: CMTimeMake(frameNumber, 25)) 
       } 
       frameNumber += 1 } 

     captureSession.stopRunning() 
     assetWriter.finishWriting() 

Non garantisco tuttavia un'accuratezza del 100%, perché sono nuovo per lo swift.

Problemi correlati