2017-01-30 62 views

回答

1

你對視頻幀有任何操縱嗎?在我的情況下,我正在調整視頻幀的大小,然後再編寫它們。在這種情況下,首先寫入音頻幀,在幾幀後寫入視頻幀。由於這個原因,前幾幀在預覽中顯示爲白色。但它在視頻播放中幾乎不明顯。

解決方案:

跳過寫音頻幀,直到你使用avasssetwriter


推薦代碼captureoutput

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

    if CMSampleBufferDataIsReady(sampleBuffer) == false 
    { 
     // Handle error 
     return; 
    } 
    startTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer) 

    if videoWriter.status == AVAssetWriterStatus.Unknown { 
       videoWriter.startWriting() 
       videoWriter.startSessionAtSourceTime(startTime) 
       return 
     } 

    if videoWriter.status == AVAssetWriterStatus.Failed { 
     // Handle error here 
     return; 
    } 

    // Here you collect each frame and process it 

    if(recordingInProgress){ 

    if let _ = captureOutput as? AVCaptureVideoDataOutput { 

     if videoWriterInput.isReadyForMoreMediaData{ 
      videoWriterInput.append(sampleBuffer) 
      video_frames_written = true 
     } 
    } 
    if let _ = captureOutput as? AVCaptureAudioDataOutput { 
      if audioWriterInput.isReadyForMoreMediaData && video_frames_written{ 
       audioWriterInput.append(sampleBuffer) 
      } 

     } 

    } 

} 
+0

我想這可能工作寫一個視頻幀,但似乎即使我只在寫入視頻幀時寫入音頻幀,仍然存在一個白色幀,其中既沒有視頻也沒有au dio被記錄。還有其他建議嗎? –

+0

你能分享錄製的文件嗎?可能是第一個samplebuffer的日誌? – manishg

+0

以下是視頻https://drive.google.com/file/d/0B2QAK-lm39WhdVlqa0hSY0tNaGM/view?usp=sharing的示例。錄音機的設置方式是每隔一秒錄製一個新的片段,但問題不在於此,因爲在錄製的第一秒內仍然存在空白幀,沒有視頻或音頻。 –