標(biāo)題有點(diǎn)長(zhǎng), 因?yàn)橐恢睕](méi)想好怎樣簡(jiǎn)潔的表述這個(gè)問(wèn)題
問(wèn)題現(xiàn)象: iPhone12拍攝的視頻上傳到服務(wù)器后, 在chrome, ie等瀏覽器播放會(huì)泛白
問(wèn)題原因: 因?yàn)閕Phone12默認(rèn)拍攝杜比視界HDR高動(dòng)態(tài)范圍視頻, chrome, ie的色彩管理有點(diǎn)問(wèn)題, 在非HDR顯示器顯示時(shí)候, 還是按照HDR范圍顯示, 導(dǎo)致色彩出現(xiàn)"過(guò)曝"現(xiàn)象
解決方案: 對(duì)視頻進(jìn)行轉(zhuǎn)碼. 指定色彩管理配置
核心代碼:
inline static NSDictionary *lf_assetExportVideoConfig(CGSize size, LFAssetExportSessionPreset preset)
{
float ratio = 1;
CGSize presetSize = lf_assetExportSessionPresetSize(preset);
CGSize videoSize = size;
if (videoSize.width > videoSize.height) {
ratio = videoSize.width / presetSize.height;
} else {
ratio = videoSize.width / presetSize.width;
}
if (ratio > 1) {
videoSize = CGSizeMake(videoSize.width / ratio, videoSize.height / ratio);
}
if (@available(iOS 10.0, *)) {
return @{
AVVideoCodecKey: AVVideoCodecH264,
AVVideoWidthKey:[NSNumber numberWithInteger:videoSize.width],
AVVideoHeightKey:[NSNumber numberWithInteger:videoSize.height],
AVVideoScalingModeKey:AVVideoScalingModeResizeAspectFill,
AVVideoCompressionPropertiesKey: @
{
///選擇視頻顏色編碼格式(默認(rèn)格式會(huì)支持HDR, 導(dǎo)致視頻在chrome, ie泛白)
///默認(rèn)為: AVVideoYCbCrMatrix_ITU_R_2020, 為支持HDR的格式, 應(yīng)當(dāng)指定為ITU_R_709_2通用配置
AVVideoColorPrimariesKey : AVVideoColorPrimaries_ITU_R_709_2,
AVVideoAverageBitRateKey: [NSNumber numberWithUnsignedLong:lf_assetExportSessionPresetBitrate()],
AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel,
AVVideoAllowFrameReorderingKey:@NO,
AVVideoExpectedSourceFrameRateKey:@24
},
};
} else {
return @{
AVVideoCodecKey: AVVideoCodecH264,
AVVideoWidthKey:[NSNumber numberWithInteger:videoSize.width],
AVVideoHeightKey:[NSNumber numberWithInteger:videoSize.height],
AVVideoScalingModeKey:AVVideoScalingModeResizeAspectFill,
AVVideoCompressionPropertiesKey: @
{
AVVideoAverageBitRateKey: [NSNumber numberWithUnsignedLong:lf_assetExportSessionPresetBitrate()],
AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel,
AVVideoAllowFrameReorderingKey:@NO,
AVVideoExpectedSourceFrameRateKey:@24
},
};
}
}
簡(jiǎn)單總結(jié)下:
因?yàn)槲覀冞@邊業(yè)務(wù)需求是用戶(hù)上傳的視頻需要在本地做一次壓縮, 我這邊采用的方案就是AVAssetExportSession逐幀壓縮. 在輸出配置里面指定色彩配置即可(即強(qiáng)制把HDR視頻轉(zhuǎn)換成SDR視頻).
效果如下: