我正在逐帧读取大型视频文件,并希望有更快的方法来获取像素缓冲区。
对于那些支持的编解码器,我使用的是AVFoundation API,然后是其余的(例如DNxHD),我正在使用Quicktime技术。
这是我基于AVFoundation的方法的简化版本。 是否有一些我没有注意到的API,或者我可以设置哪些属性来加快速度?
AVURLAsset *myAVURLAsset = [AVURLAsset URLAssetWithURL:@"blahblah" options:nil];
AVAssetTrack * videoTrack = [[myAVURLAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetReader *movieReader = [[AVAssetReader alloc] initWithAsset:myAVURLAsset error:nil];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:@(kCVPixelFormatType_32ARGB) forKey:@"kCVPixelBufferPixelFormatTypeKey"];
AVAssetReaderTrackOutput * output = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:videoTrack outputSettings:videoSettings];
[movieReader addOutput:output];
[movieReader startReading];
bool atEnd = false;
while (!atEnd) {
CMSampleBufferRef sampleBuffer = [output copyNextSampleBuffer];
if (sampleBuffer)
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
uint8_t *data = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
// ...do lots of stuff with pixel data
// ...release and tidy up sampleBuffer etc
}
else{
atEnd = true;
}
}