我已经构建了一些代码来逐帧处理 OSX 上的视频文件。以下是代码的摘录,它构建好、打开文件、找到视频轨道(仅轨道)并开始毫无问题地读取 CMSampleBuffers。但是,当我尝试提取像素缓冲区帧时,我获得的每个 CMSampleBufferRef 都返回 NULL。iOS 文档中没有说明为什么我可以期望 NULL 返回值或我可以期望如何解决该问题。无论捕获源或编解码器如何,我测试过的所有视频都会发生这种情况。
非常感谢任何帮助。
NSString *assetInPath = @"/Users/Dave/Movies/movie.mp4";
NSURL *assetInUrl = [NSURL fileURLWithPath:assetInPath];
AVAsset *assetIn = [AVAsset assetWithURL:assetInUrl];
NSError *error;
AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:assetIn error:&error];
AVAssetTrack *track = [assetIn.tracks objectAtIndex:0];
AVAssetReaderOutput *assetReaderOutput = [[AVAssetReaderTrackOutput alloc]
initWithTrack:track
outputSettings:nil];
[assetReader addOutput:assetReaderOutput];
// Start reading
[assetReader startReading];
CMSampleBufferRef sampleBuffer;
do {
sampleBuffer = [assetReaderOutput copyNextSampleBuffer];
/**
** At this point, sampleBuffer is non-null, has all appropriate attributes to indicate that
** it's a video frame, 320x240 or whatever and looks perfectly fine. But the next
** line always returns NULL without logging any obvious error message
**/
CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
if( pixelBuffer != NULL ) {
size_t width = CVPixelBufferGetWidth(pixelBuffer);
size_t height = CVPixelBufferGetHeight(pixelBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
...
other processing removed here for clarity
}
} while( ... );
需要明确的是,我已经删除了所有错误检查代码,但该代码中没有指出任何问题。即 AVAssetReader 正在读取,CMSampleBufferRef 看起来不错等。