avfoundation - 如何使用 AVFoundation 从视频流中获取原始格式的图像缓冲区?

标签 avfoundation avasset avassetreader cmsamplebufferref

在 Apple 的文档中 AVAssetReaderTrackOutput , 表示outputSettings的参数如下使用 +[AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:outputSettings:] 实例化实例时

A value of nil configures the output to vend samples in their original format as stored by the specified track.

当使用它时,例如一个 MP4 视频 Assets ,它似乎会按解码顺序逐步通过帧(即关于显示的乱序),但是所有查询都已交付 CMSampleBufferRef使用 CMSampleBufferGetImageBuffer 的对象产量 NULL CVImageBufferRef对象。

我能确保传递图像缓冲对象的唯一方法是为 outputSettings: 提供像素缓冲格式。 ,例如 kCVPixelFormatType_32ARGB对于 kCVPixelBufferPixelFormatTypeKey字典词条。

这样做的另一个有趣的副作用是,帧随后按显示顺序传送,并且帧的底层解码顺序被抽象/隐藏。

知道为什么会这样吗?

最佳答案

像您一样,我希望将 outputSettings 设置为 nil 会导致输出原生格式视频帧,但事实并非如此,您必须指定一些内容才能获取有效的 CVSampleBufferRef

一切都没有丢失,使用“勉强存在”字典似乎以其 native 格式输出帧,

AVAsset asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetTrack *videoTrack = [[asset tracksWithMediaCharacteristic:AVMediaCharacteristicVisual] objectAtIndex:0];

NSDictionary *decompressionSettings =
     @{ (id)kCVPixelBufferIOSurfacePropertiesKey : [NSDictionary dictionary] };
AVAssetReaderTrackOutput trackOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack outputSettings:decompressionSettings];
...

IOSurfaceOptions 只是默认设置 - 进一步阅读以供引用:https://developer.apple.com/documentation/corevideo/kcvpixelbufferiosurfacepropertieskey?language=objc

关于avfoundation - 如何使用 AVFoundation 从视频流中获取原始格式的图像缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50187204/

相关文章:

objective-c - CALayer _layer 没有在应用程序启动时初始化?! (OSX、 cocoa )

ios - 以与预览层匹配的大小导出 AVCaptureSession 视频

ios - 应用程序进入后台时,AVAsset Writer失败

ios - iOS 中的视频编辑问题

swift - 按下 Siri 远程菜单按钮停止视频播放

ios - 将视频和音频 Assets 合并到Swift 3 iOS中后,在视频上添加水印

ios - AVAssetWriter 和 AVAssetWriterInputPixelBufferAdaptor appendingPixelBuffer 组合失败

objective-c - 在 AVAssetReader 中设置时间范围会导致卡住

ios - 将 CMSampleBufferRef 处理到数组会导致崩溃