我正在努力将我们的应用程序从一些专有的编解码器移动到iOS本机h264编码器(VideoToolbox.framework)并且有疑问:
是否存在为压缩数据设置比特率或数据速率的方法?
以下是我创建编码器会话的方法:
CFMutableDictionaryRef sessionAttributes = CFDictionaryCreateMutable(
NULL,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
//** bitrate
int fixedBitrate = bitrate; // 2000 * 1024 -> assume 2 Mbits/s
CFNumberRef bitrateNum = CFNumberCreate(NULL, kCFNumberSInt32Type, &fixedBitrate);
CFDictionarySetValue(sessionAttributes, kVTCompressionPropertyKey_AverageBitRate, bitrateNum);
CFRelease(bitrateNum);
CFDictionarySetValue(sessionAttributes, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_High_AutoLevel);
CFDictionarySetValue(sessionAttributes, kVTCompressionPropertyKey_RealTime, kCFBooleanTrue);
OSStatus error = VTCompressionSessionCreate(kCFAllocatorDefault,
width,
height,
kCMVideoCodecType_H264,
sessionAttributes,
NULL,
kCFAllocatorDefault,
&EncoderCallback,
this, *outputCallbackRefCon,
&m_EncoderSession);
Run Code Online (Sandbox Code Playgroud)
我玩了很多不同的价值观,kVTCompressionPropertyKey_AverageBitRate但这对我没什么用,我也试过 kVTCompressionPropertyKey_DataRateLimits不同的价值但也没有任何运气.
任何想法,建议都是受欢迎的
简短的故事是你需要VTSessionSetProperty在创建会话后使用.
您作为第五个参数传入的字典实际上用于指定要使用的编码器而不是编码器设置.这有点令人困惑,但Apple文档指出:
要在创建压缩会话时指定特定视频编码器,请传递包含此密钥和EncoderID作为其值的encoderSpecification CFDictionary.EncoderID CFString可以从VTCopyVideoEncoderList返回的数组中的kVTVideoEncoderList_EncoderID条目获得.
使用该函数创建会话后,需要设置kVTCompressionPropertyKey_AverageBitRate和kVTCompressionPropertyKey_DataRateLimits属性VTSessionSetProperty.
例如:
status = VTSessionSetProperty(session, kVTCompressionPropertyKey_AverageBitRate, (__bridge CFTypeRef)@(600 * 1024));
status = VTSessionSetProperty(session, kVTCompressionPropertyKey_DataRateLimits, (__bridge CFArrayRef)@[800 * 1024 / 8, 1]);
Run Code Online (Sandbox Code Playgroud)
只记得kVTCompressionPropertyKey_AverageBitRate带位和 kVTCompressionPropertyKey_DataRateLimits占用字节和秒.