2012-06-20 42 views
1

我目前拍攝視頻,並使用AVCaptureSession音頻,並建立了管道寫出來使用AVAssetWriter的數據到硬盤的視頻。對於短片來說,這很好,絕對沒有問題。AVAssetWriter finishWriting失敗,生產只能觀看50%

但是,如果記錄介質的長度越過一些任意時間,通常約2分鐘,然後調用來完成寫入失敗,但仍設法制作的視頻文件只播放大約50%的內容。我在寫作過程中不斷調用isReadyForMoreMediaData並追加了SampleBuffer,並且它們始終返回true。我似乎沒有得到任何通知,直到finishWriting調用出現問題。

的AVAssetWriters錯誤是失敗的finishWriting呼叫

Capture failed to end with status 3 and error Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x210b7880 {NSLocalizedFailureReason=An unknown error occurred (268451843), NSUnderlyingError=0x210b1ee0 "The operation couldn’t be completed. (OSStatus error 268451843.)", NSLocalizedDescription=The operation could not be completed} 

不是世界上最有用的錯誤後,設定成以下...

這是通過對AVCaptureSessionRuntimeErrorNotification監聽通話隨後不久我已經建立了一個包含錯誤

Error Domain=AVFoundationErrorDomain Code=-11819 "Cannot Complete Action" UserInfo=0x21011940 {NSLocalizedRecoverySuggestion=Try again later., NSLocalizedDescription=Cannot Complete Action} 

同樣不是非常有幫助?

這是代碼我用來配置AVAssetWriter輸入音頻

//\Configure Audio Writer Input 
AudioChannelLayout acl; 
bzero(&acl, sizeof(acl)); 
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo; 

NSDictionary* audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
             [ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey, 
             [ NSNumber numberWithInt: 2 ], AVNumberOfChannelsKey, 
             [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey, 
             [ NSData dataWithBytes: &acl length: sizeof(AudioChannelLayout) ], AVChannelLayoutKey, 
             [ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey, 
             nil]; 


m_audioWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioOutputSettings] retain]; 

這是我用來配置AVAssetWriterInput視頻

 videoSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
        AVVideoCodecH264, AVVideoCodecKey, 
        [NSNumber numberWithInt:640], AVVideoWidthKey, 
        [NSNumber numberWithInt:480], AVVideoHeightKey, 
        nil]; 


m_videoWriterInput = [[AVAssetWriterInput 
         assetWriterInputWithMediaType:AVMediaTypeVideo 
         outputSettings:videoSettings] retain]; 

m_videoWriterInput.expectsMediaDataInRealTime = YES; 

代碼這是我使用的初始化代碼AVAssetWriter

m_audioAndVideoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:outputPath] fileType:AVFileTypeQuickTimeMovie error:&error]; 

m_audioAndVideoWriter.shouldOptimizeForNetworkUse = YES; 
[m_audioAndVideoWriter addInput:m_videoWriterInput]; 
[m_audioAndVideoWriter addInput:m_audioWriterInput]; 

有沒有人遇到類似的問題?或者知道可能會導致此問題的finishWriting調用內部發生了什麼?或者也許有人知道一些新穎的方法來調試這個問題?

感謝

+0

將m_audioAndVideoWriter.shouldOptimizeForNetworkUse設置爲NO似乎可以停止發生錯誤。但是,我們需要將此設置設置爲yes,因爲視頻在流式傳輸的服務器上結束。 – RyanSullivan

回答

0

奇怪的是,設置m_audioAndVideoWritershouldOptimizeForNetworkUse = NO將解決這個問題。其實,如果你抓住該文件中的編碼幀,然後它們流我會確保你設置shouldOptimizeForNetworkUseYES。我得到它的工作,所以我很確定你將能夠得到它的工作。

容器生成的最後一步包括編寫幾個重要的容器原子,如AVCC,它們爲編碼幀指定SPS/PPS,以及其他非常重要的元數據。

夫婦的想法:

  1. 多大的文件是你創建的測試iOS設備上?確保你沒有用完空間 - 這將使.mov文件創建的最後一步失敗。如果文件太快太大,我只會調整視頻的大小。此外,我會使用其中一個預設爲AVCaptureSession - 剛剛成立根據您的需要質量(captureSession.sessionPresetAVCaptureSessionPresetLowAVCaptureSessionPresetMedium)。另外兩個設置AVVideoMaxKeyFrameIntervalKeyAVVideoAverageBitRateKey可以幫助你減少文件大小爲好。我可以給你更多的信息,但我們首先確保文件大小是一個問題:)

  2. 你試過生成.mp4文件嗎?對於您的assetWriter,只需指定fileType:kUTTypeMPEG4選項。如果這有什麼不同,我會很好奇。

+0

你說什麼都不適合我... – SpaceDog