我正在使用 BlackMagic DeckLink SDK 尝试从 BM 设备捕获帧。
我正在尝试从回调中获取像素数据并将其转换为 aIDeckLinkVideoInputFrame
以便能够使用 AVFoundation 和 将其写入磁盘。我正在使用的代码似乎正在工作,除了写入磁盘的所有帧都是绿色的。(BlackMagic 在屏幕上生成预览的示例代码确实显示了图像,因此设备和设备设置应该没问题)。DeckLinkController::VideoInputFrameArrived
CVPixelBufferRef
AVAssetWriterInputPixelBufferAdaptor
AVAssetWriter
AVAssetWriter
设置如下:
writer = [[AVAssetWriter assetWriterWithURL:destinationUrl
fileType:AVFileTypeAppleM4V
error:&error] retain];
if(error)
NSLog(@"ERROR: %@", [error localizedDescription]);
NSMutableDictionary * outputSettings = [[NSMutableDictionary alloc] init];
[outputSettings setObject: AVVideoCodecH264
forKey: AVVideoCodecKey];
[outputSettings setObject: [NSNumber numberWithInt:1920]
forKey: AVVideoWidthKey];
[outputSettings setObject: [NSNumber numberWithInt:1080]
forKey: AVVideoHeightKey];
NSMutableDictionary * compressionProperties = [[NSMutableDictionary alloc] init];
[compressionProperties setObject: [NSNumber numberWithInt: 1000000]
forKey: AVVideoAverageBitRateKey];
[compressionProperties setObject: [NSNumber numberWithInt: 16]
forKey: AVVideoMaxKeyFrameIntervalKey];
[compressionProperties setObject: AVVideoProfileLevelH264Main31
forKey: AVVideoProfileLevelKey];
[outputSettings setObject: compressionProperties
forKey: AVVideoCompressionPropertiesKey];
writerVideoInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:outputSettings] retain];
NSMutableDictionary * pixBufSettings = [[NSMutableDictionary alloc] init];
[pixBufSettings setObject: [NSNumber numberWithInt: kCVPixelFormatType_422YpCbCr8_yuvs]
forKey: (NSString *) kCVPixelBufferPixelFormatTypeKey];
[pixBufSettings setObject: [NSNumber numberWithInt: 1920]
forKey: (NSString *) kCVPixelBufferWidthKey];
[pixBufSettings setObject: [NSNumber numberWithInt: 1080]
forKey: (NSString *) kCVPixelBufferHeightKey];
writerVideoInput.expectsMediaDataInRealTime = YES;
writer.shouldOptimizeForNetworkUse = NO;
adaptor = [[AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerVideoInput
sourcePixelBufferAttributes:pixBufSettings] retain];
[writer addInput:writerVideoInput];
作为参考,这些输出设置和压缩选项应该是正确的,但我尝试了几种不同的选择。
当一个框架从设备进来时,我将它转换CVPixelBufferRef
为如下:
void *videoData;
int64_t frameTime;
int64_t frameDuration;
videoFrame->GetBytes(&videoData);
videoFrame->GetStreamTime(&frameTime, &frameDuration, 3000);
CMTime presentationTime = CMTimeMake(frameDuration, 3000);
CVPixelBufferRef buffer = NULL;
CVPixelBufferPoolCreatePixelBuffer(NULL, adaptor.pixelBufferPool, &buffer);
CVPixelBufferLockBaseAddress(buffer, 0);
void *rasterData = CVPixelBufferGetBaseAddress(buffer);
memcpy(rasterData, videoData, (videoFrame->GetRowBytes()*videoFrame->GetHeight()));
CVPixelBufferUnlockBaseAddress(buffer, 0);
if (buffer)
{
if(![adaptor appendPixelBuffer:buffer withPresentationTime:presentationTime]) {
NSLog(@"ERROR appending pixelbuffer: %@", writer.error);
[writerVideoInput markAsFinished];
if(![writer finishWriting])
NSLog(@"ERROR finishing writing: %@", [writer.error localizedDescription]);
}
else {
NSLog(@"SUCCESS");
if(buffer)
CVPixelBufferRelease(buffer);
}
}
此代码将帧附加到 AVAssetWriterInputPixelBufferAdaptor,但所有帧都是绿色的。
谁能看到我在这里做错了什么,或者有人有使用 AVFoundation 使用 BlackMagic Decklink SDK 捕获和压缩帧的经验吗?