2011-11-09 27 views
12

我目前正在開發一個iOS應用程序,將CoreImage應用於相機反饋,以便拍攝照片和視頻,而且我遇到了一些問題。具有多個輸出的AVCaptureSession?

到現在爲止我一直在使用AVCaptureVideoDataOutput與CoreImage獲得樣本緩衝器和操縱它們,然後顯示一個簡單的預覽,以及使用它來拍攝照片,並將其保存。

當我試圖通過寫SampleBuffers的視頻,因爲我從AVCaptureVideoDataOutput收到它們來實現視頻錄製,它有一個非常緩慢的幀速率(可能是因爲這是將在其它圖像與處理的) 。

所以我想知道,是否有可能有一個AVCaptureVideoDataOutput和一個AVCaptureMoveFileOutput同時進入相同的AVCaptureSession?

我給它一個快速去,發現當我添加額外的輸出時,我的AVCaptureVideoDataOutput停止接收信息。

如果我能得到它的工作,我希望這意味着我可以簡單地使用第二個輸出以高幀率錄製視頻,並在用戶停止錄製後對視頻進行後期處理。

任何幫助將不勝感激。

+0

是你使用AVAssetWriter將圖像寫入MOV/MP4?我確實使用了自定義的OpenGL圖像處理引擎,並且仍然可以以30fps的速度記錄。爲了提高效率,我認爲CoreImage會是OpenGL的支持。我懷疑是什麼阻止你回來的是圖像的顯示。您是使用OpenGL渲染圖像,還是使用其他API(可能是基於CPU)? –

+0

您是否找到可行的解決方案?請將 – user454083

回答

3

它比您想象的要容易。

請參閱:使用AVCaptureVideoDataOutput AVCamDemo

  1. 捕獲數據。
  2. 在記錄之前創建一個新的調度隊列, recordingQueue:recordingQueue = dispatch_queue_create("Movie Recording Queue", DISPATCH_QUEUE_SERIAL);
  3. 在captureOutput:didOutputSampleBuffer:fromConnection:委託 方法,捕捉samplebuffer,留住它,並在記錄 隊列中,將其寫入文件:

    -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {  
    
        CFRetain(sampleBuffer); 
    
        dispatch_async(recordingQueue, ^{ 
    
         if (assetWriter) { 
    
          if (connection == videoConnection) { 
           [self writeSampleBuffer:sampleBuffer ofType:AVMediaTypeVideo]; 
          } else if (connection == audioConnection) { 
           [self writeSampleBuffer:sampleBuffer ofType:AVMediaTypeAudio]; 
          } 
    
         } 
    
         CFRelease(sampleBuffer);   
        }); 
    } 
    
        - (void) writeSampleBuffer:(CMSampleBufferRef)sampleBuffer ofType:(NSString *)mediaType 
        { 
         CMTime presentationTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer); 
    
         if (assetWriter.status == AVAssetWriterStatusUnknown) { 
    
          if ([assetWriter startWriting]) { 
           [assetWriter startSessionAtSourceTime:presentationTime]; 
          } else { 
           NSLog(@"Error writing initial buffer"); 
          } 
         } 
    
         if (assetWriter.status == AVAssetWriterStatusWriting) { 
    
          if (mediaType == AVMediaTypeVideo) { 
           if (assetWriterVideoIn.readyForMoreMediaData) { 
    
            if (![assetWriterVideoIn appendSampleBuffer:sampleBuffer]) { 
             NSLog(@"Error writing video buffer"); 
            } 
           } 
          } 
          else if (mediaType == AVMediaTypeAudio) { 
           if (assetWriterAudioIn.readyForMoreMediaData) { 
    
            if (![assetWriterAudioIn appendSampleBuffer:sampleBuffer]) { 
             NSLog(@"Error writing audio buffer"); 
            } 
           } 
          } 
         } 
        } 
    
+0

轉換爲Swift 4 – user924

相關問題