我已经编写了一些代码来处理OSX上的视频文件,逐帧处理。以下是从构建OK的代码中提取的内容,它打开文件,定位视频轨道(仅轨道)并开始读取CMSampleBuffers而没有问题。但是,当我尝试提取像素缓冲区帧时,我获得的每个CMSampleBufferRef都返回NULL。在iOS文档中没有任何指示,说明为什么我可以期望返回NULL值,或者我可以如何解决这个问题。无论捕获源或CODEC如何,我测试过的所有视频都会出现此问题。
非常感谢您的任何帮助。
非常感谢您的任何帮助。
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看起来很好。