iOS中类似Android MediaCodec的编解码能力获取类咨询(仅支持AVPlayer)
iOS 获取编码器/解码器能力的替代方案
iOS 提供了无需依赖 AVCaptureSession 的 API 来获取编码器、解码器的能力,主要依赖 AVFoundation 和 VideoToolbox 框架,可满足你获取 H264、HEVC 编码格式的 profile/level、分辨率、帧率等信息的需求。
核心方法与工具
1. VideoToolbox 框架:直接获取编码器支持的全量属性
VideoToolbox 是底层多媒体处理框架,能直接查询指定编码器的所有支持参数:
- 使用
VTCopySupportedPropertyDictionary函数,传入目标编码器类型(如 H264 的kCMVideoCodecType_H264、HEVC 的kCMVideoCodecType_HEVC),即可获取包含所有支持属性的字典。 - 关键属性键包括:
kVTCompressionPropertyKey_AllowedProfiles:支持的编码 profile 列表kVTCompressionPropertyKey_AllowedLevels:对应 profile 支持的 levelkVTCompressionPropertyKey_MaxFrameRate:最大支持帧率kVTCompressionPropertyKey_MaxPixelDimensions:最大支持分辨率kVTCompressionPropertyKey_MinPixelDimensions:最小支持分辨率
示例代码片段:
#import <VideoToolbox/VideoToolbox.h> // 查询H264编码器支持的属性 CFDictionaryRef h264Props = VTCopySupportedPropertyDictionary(kCMVideoCodecType_H264, kVTVideoEncoderSpecification_EnableHardwareAcceleration); if (h264Props) { // 解析profile列表 NSArray *allowedProfiles = (__bridge NSArray *)CFDictionaryGetValue(h264Props, kVTCompressionPropertyKey_AllowedProfiles); // 解析最大帧率 NSNumber *maxFrameRate = (__bridge NSNumber *)CFDictionaryGetValue(h264Props, kVTCompressionPropertyKey_MaxFrameRate); // 解析分辨率范围 NSDictionary *maxDimensions = (__bridge NSDictionary *)CFDictionaryGetValue(h264Props, kVTCompressionPropertyKey_MaxPixelDimensions); CFRelease(h264Props); }
2. AVFoundation 框架:验证特定编码设置的兼容性
如果你的场景围绕 AVPlayer 处理媒体(如编码输出适配),可以用 AVAssetWriterInput 验证指定设置是否被支持:
- 构造包含目标 profile、level、分辨率、帧率的
AVVideoSettings字典 - 调用
canApplyOutputSettings:forMediaType:方法,判断该设置是否被当前设备支持
示例代码片段:
#import <AVFoundation/AVFoundation.h> // 构造H264编码设置 NSDictionary *h264Settings = @{ AVVideoCodecKey: AVVideoCodecTypeH264, AVVideoWidthKey: @1920, AVVideoHeightKey: @1080, AVVideoCompressionPropertiesKey: @{ AVVideoProfileLevelKey: AVVideoProfileLevelH264MainAutoLevel, AVVideoAverageBitRateKey: @5000000, AVVideoMaxKeyFrameIntervalKey: @30 } }; // 验证设置是否支持 BOOL isSupported = [AVAssetWriterInput canApplyOutputSettings:h264Settings forMediaType:AVMediaTypeVideo];
针对 H264/HEVC 的具体说明
- H264:编码器类型对应
kCMVideoCodecType_H264,支持的 profile 包括基线(Baseline)、主(Main)、高(High)等,每个 profile 对应的 level 和分辨率、帧率限制可从 VideoToolbox 返回的属性字典中解析。 - HEVC:编码器类型对应
kCMVideoCodecType_HEVC,支持的 profile 包括主(Main)、主 10(Main 10)等,同样通过属性字典获取完整的能力参数。
内容的提问来源于stack exchange,提问作者Ratul Saha
相关产品推荐
相关产品推荐

