通常苹果的hdr视频都是使用hevc编码的,且支持hevc的播放器一般都支持hdr播放,所以压缩视频的时候选择hevc编码也很合理。
1.压缩的时候需要设置两个属性
AVVideoCodecKey
因为压缩的时候AVVideoCodecKey属性是必须要设置的,点进AVVideoCodecKey的头文件中,描述为:
extension AVVideoCodecType {
@available(iOS 11.0, *)
public static let hevc: AVVideoCodecType
@available(iOS 11.0, *)
public static let h264: AVVideoCodecType
@available(iOS 11.0, *)
public static let jpeg: AVVideoCodecType
@available(iOS 11.0, *)
public static let proRes4444: AVVideoCodecType
@available(iOS 11.0, *)
public static let proRes422: AVVideoCodecType
@available(iOS 13.0, *)
public static let proRes422HQ: AVVideoCodecType
@available(iOS 13.0, *)
public static let proRes422LT: AVVideoCodecType
@available(iOS 13.0, *)
public static let proRes422Proxy: AVVideoCodecType
@available(iOS 13.0, *)
public static let hevcWithAlpha: AVVideoCodecType
}
直接选择hevc即可
AVVideoProfileLevelKey
并且要同时设置AVVideoProfileLevelKey,点进去只看到H264的Value值,而我们需要的hevc不在里面,但是有段注释
/* HEVC profiles/levels are defined in
VideoToolbox/VTCompressionProperties.h, e.g.
kVTProfileLevel_HEVC_Main_AutoLevel. The constants
defined there can be used as the value for the key
AVVideoProfileLevelKey. */
意思就是hevc在“VideoToolbox/VTCompressionProperties.h”里面定义的 所以需要在使用hevc编码的时候导入头文件
import VideoToolbox
总结
exportSession.videoSettings = [
AVVideoCodecKey: AVVideoCodecType.hevc,
AVVideoWidthKey: targetSize.width,
AVVideoHeightKey: targetSize.height,
AVVideoCompressionPropertiesKey: [
AVVideoAverageBitRateKey: targetSize.width * targetSize.height * 3,
AVVideoProfileLevelKey: kVTProfileLevel_HEVC_Main10_AutoLevel,
],
]
|