[英]AVAssetWriter / AVAssetWriterInputPixelBufferAdaptor - black frames and frame rate
我正在拍攝相機信息並將其寫入電影。 我遇到的問題是導出后電影前面有幾個黑秒(相對於實際錄制開始時間)。
我認為這與[self.assetWriter startSessionAtSourceTime:kCMTimeZero];
我有一個半工作的解決方案,它有一個frameStart變量,它在samplebuffer委托方法中向上計數。
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
frameStart++;
if (self.startRecording == YES) {
static int64_t frameNumber = 0;
if(self.assetWriterInput.readyForMoreMediaData) {
[self.pixelBufferAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:CMTimeMake(frameNumber, 25)];
}
frameNumber++;
}
}
然后在用戶按下按鈕時調用此方法:
[self.assetWriter startSessionAtSourceTime:CMTimeMake(frameStart,25)];
這很有效。 但只有一次......如果我想要錄制第二部電影,那么黑框會再次回來。
另外,當我看輸出的電影時,幀速率是25fps,就像我想要的那樣。 但視頻看起來好像加快了。 好像框架之間沒有足夠的空間。 因此電影的播放速度快了兩倍。
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:640], AVVideoWidthKey, [NSNumber numberWithInt:480], AVVideoHeightKey, AVVideoCodecH264, AVVideoCodecKey, nil];
self.assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:outputSettings];
self.assetWriterInput.expectsMediaDataInRealTime = YES;
您不需要自己計算幀時間戳。 您可以使用獲取當前樣本的時間戳
CMTime timestamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
但是,在我看來,你只是將幀的像素緩沖區傳遞給適配器而不進行任何修改。 是不是更容易將樣本緩沖區本身直接傳遞給assetWriterInput
,如下所示?
[self.assetWriterInput appendSampleBuffer:sampleBuffer];
首先,為什么每幀增加兩次frameNumber? 增加一次,刪除第一個。 這應該可以確定播放速度。
其次,你完成錄制時是否將frameNumber重置為0? 如果不是這個是你的問題。 如果不是,我需要更多解釋這里發生的事情..
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.