通常苹果的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 /* @"avc1" */
@available(iOS 11.0, *)
public static let jpeg: AVVideoCodecType /* @"jpeg" */
@available(iOS 11.0, *)
public static let proRes4444: AVVideoCodecType /* @"ap4h" */
@available(iOS 11.0, *)
public static let proRes422: AVVideoCodecType /* @"apcn" */
@available(iOS 13.0, *)
public static let proRes422HQ: AVVideoCodecType /* @"apch" */
@available(iOS 13.0, *)
public static let proRes422LT: AVVideoCodecType /* @"apcs" */
@available(iOS 13.0, *)
public static let proRes422Proxy: AVVideoCodecType /* @"apco" */
/* IMPORTANT NOTE: this constant is used to select the appropriate encoder, but is NOT used on the encoded content, which is backwards compatible and hence uses 'hvc1' as its codec type. */
@available(iOS 13.0, *)
public static let hevcWithAlpha: AVVideoCodecType /* @"muxa" */
}
直接选择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,
//这里要和上面配套设置为hevc,不然会报错,并且需要在代码中引入import VideoToolbox
AVVideoProfileLevelKey: kVTProfileLevel_HEVC_Main10_AutoLevel,
],
]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)