当前位置: 首页>移动开发>正文

iOS 判断 AVAsset 的编码类型是否是 H.264

去年因为视频压缩转码方面的一些需求需要判断 AVAsset 的编码类型是否为 H.264,然后就整理了这篇笔记,但是忘记发出来了


iOS 判断 AVAsset 的编码类型是否是 H.264,第1张

思路大概分三步:

1.从 AVAsset 的轨道中取出视频轨道
2.从视频轨道中获取视频格式信息
3.从视频格式信息中获取视频编码类型

AVAsset 的轨道集合:

/*!
  @property     tracks
  @abstract     Provides the array of AVAssetTracks contained by the asset
*/
@property (nonatomic, readonly) NSArray<AVAssetTrack *> *tracks;

以下代码可以建一个 AVAsset 的 Category

@implementation AVAsset (PSExtends)

/// 1.从 AVAsset 的轨道中取出视频轨道
- (AVAssetTrack *)ps_videoAssetTrack
{
    NSArray<AVAssetTrack *> *tracksArray = self.tracks;
    AVAssetTrack *videoAssetTrack = nil;
    for (AVAssetTrack *track in tracksArray) {
        if ([track.mediaType isEqualToString:AVMediaTypeVideo]) {
            videoAssetTrack = track;
            break;
        }
    }
    return videoAssetTrack;
}

- (BOOL)ps_isH264
{
    BOOL isH264 = NO;
    AVAssetTrack *videoAssetTrack = [self ps_videoAssetTrack];
    if (videoAssetTrack) {
        isH264 = videoAssetTrack.ps_isH264;
    }
    return isH264;
}

- (BOOL)ps_isHEVC
{
    BOOL isHEVC = NO;
    AVAssetTrack *videoAssetTrack = [self ps_videoAssetTrack];
    if (videoAssetTrack) {
        isHEVC = videoAssetTrack.ps_isHEVC;
    }
    return isHEVC;
}

@end

@implementation AVAssetTrack (PSExtends)

/// 2.从视频轨道中获取视频格式信息
- (CMFormatDescriptionRef)ps_CMFormatDescriptionRef
{
    NSArray *formatDescriptions = [self formatDescriptions];
    CMFormatDescriptionRef formatDescription = (__bridge CMFormatDescriptionRef)(formatDescriptions.firstObject);
    return formatDescription;
}

/// 3.从视频格式信息中获取视频编码类型
- (CMVideoCodecType)ps_CMVideoCodecType
{
    CMFormatDescriptionRef formatDescription = [self ps_CMFormatDescriptionRef];
    CMVideoCodecType codecType = CMVideoFormatDescriptionGetCodecType(formatDescription);
    return codecType;
}

- (BOOL)ps_isH264
{
    BOOL isH264 = NO;
    CMVideoCodecType codecType = [self ps_CMVideoCodecType];
    isH264 = (codecType == kCMVideoCodecType_H264);
    return isH264;
}

- (BOOL)ps_isHEVC
{
    BOOL isHEVC = NO;
    CMVideoCodecType codecType = [self ps_CMVideoCodecType];
    isHEVC = (codecType == kCMVideoCodecType_HEVC);
    return isHEVC;
}

@end

其他编码类型:

/*!
    @enum CMVideoCodecType
    @discussion Four-character codes identifying the video codec. Certain codec types are also pixel formats.
                  Note: There is no kCMVideoCodecType_Raw -- use the appropriate pixel format type as the codec type.
    @constant   kCMVideoCodecType_422YpCbCr8    Component Y'CbCr 8-bit 4:2:2 ordered Cb Y'0 Cr Y'1
    @constant   kCMVideoCodecType_Animation     Apple Animation format
    @constant   kCMVideoCodecType_Cinepak   Cinepak format
    @constant   kCMVideoCodecType_JPEG  Joint Photographic Experts Group (JPEG) format
    @constant   kCMVideoCodecType_JPEG_OpenDML  JPEG format with Open-DML extensions
    @constant   kCMVideoCodecType_SorensonVideo     Sorenson video format
    @constant   kCMVideoCodecType_SorensonVideo3    Sorenson 3 video format
    @constant   kCMVideoCodecType_H263  ITU-T H.263 format
    @constant   kCMVideoCodecType_H264  ITU-T H.264 format (AKA ISO/IEC 14496-10 - MPEG-4 Part 10, Advanced Video Coding format)
    @constant   kCMVideoCodecType_HEVC  ITU-T HEVC format
    @constant   kCMVideoCodecType_HEVCWithAlpha HEVC format with alpha support defined in Annex-F.
                  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.
    @constant   kCMVideoCodecType_MPEG4Video    ISO/IEC Moving Picture Experts Group (MPEG) MPEG-4 Part 2 video format
    @constant   kCMVideoCodecType_MPEG2Video    MPEG-2 video format
    @constant   kCMVideoCodecType_MPEG1Video    MPEG-1 video format
    @constant   kCMVideoCodecType_DVCNTSC   DV NTSC format
    @constant   kCMVideoCodecType_DVCPAL    DV PAL format
    @constant   kCMVideoCodecType_DVCProPAL     Panasonic DVCPro PAL format
    @constant   kCMVideoCodecType_DVCPro50NTSC  Panasonic DVCPro-50 NTSC format
    @constant   kCMVideoCodecType_DVCPro50PAL   Panasonic DVCPro-50 PAL format
    @constant   kCMVideoCodecType_DVCPROHD720p60    Panasonic DVCPro-HD 720p60 format
    @constant   kCMVideoCodecType_DVCPROHD720p50    Panasonic DVCPro-HD 720p50 format
    @constant   kCMVideoCodecType_DVCPROHD1080i60   Panasonic DVCPro-HD 1080i60 format
    @constant   kCMVideoCodecType_DVCPROHD1080i50   Panasonic DVCPro-HD 1080i50 format
    @constant   kCMVideoCodecType_DVCPROHD1080p30   Panasonic DVCPro-HD 1080p30 format
    @constant   kCMVideoCodecType_DVCPROHD1080p25   Panasonic DVCPro-HD 1080p25 format
    @constant   kCMVideoCodecType_AppleProRes4444XQ Apple ProRes 4444 XQ format
    @constant   kCMVideoCodecType_AppleProRes4444   Apple ProRes 4444 format
    @constant   kCMVideoCodecType_AppleProRes422HQ  Apple ProRes 422 HQ format
    @constant   kCMVideoCodecType_AppleProRes422    Apple ProRes 422 format
    @constant   kCMVideoCodecType_AppleProRes422LT  Apple ProRes 422 LT format
    @constant   kCMVideoCodecType_AppleProRes422Proxy   Apple ProRes 422 Proxy format
    @constant   kCMVideoCodecType_AppleProResRAW    Apple ProRes RAW format
    @constant   kCMVideoCodecType_AppleProResRAWHQ  Apple ProRes RAW HQ format
*/
typedef FourCharCode CMVideoCodecType API_AVAILABLE(macos(10.7), ios(4.0), tvos(9.0), watchos(6.0));
#if COREMEDIA_USE_DERIVED_ENUMS_FOR_CONSTANTS
enum : CMVideoCodecType
#else
enum
#endif
{
    kCMVideoCodecType_422YpCbCr8       = kCMPixelFormat_422YpCbCr8,
    kCMVideoCodecType_Animation        = 'rle ',
    kCMVideoCodecType_Cinepak          = 'cvid',
    kCMVideoCodecType_JPEG             = 'jpeg',
    kCMVideoCodecType_JPEG_OpenDML     = 'dmb1',
    kCMVideoCodecType_SorensonVideo    = 'SVQ1',
    kCMVideoCodecType_SorensonVideo3   = 'SVQ3',
    kCMVideoCodecType_H263             = 'h263',
    kCMVideoCodecType_H264             = 'avc1',
    kCMVideoCodecType_HEVC             = 'hvc1',
    kCMVideoCodecType_HEVCWithAlpha    = 'muxa',
    kCMVideoCodecType_MPEG4Video       = 'mp4v',
    kCMVideoCodecType_MPEG2Video       = 'mp2v',
    kCMVideoCodecType_MPEG1Video       = 'mp1v',
    kCMVideoCodecType_VP9              = 'vp09',

    kCMVideoCodecType_DVCNTSC          = 'dvc ',
    kCMVideoCodecType_DVCPAL           = 'dvcp',
    kCMVideoCodecType_DVCProPAL        = 'dvpp',
    kCMVideoCodecType_DVCPro50NTSC     = 'dv5n',
    kCMVideoCodecType_DVCPro50PAL      = 'dv5p',
    kCMVideoCodecType_DVCPROHD720p60   = 'dvhp',
    kCMVideoCodecType_DVCPROHD720p50   = 'dvhq',
    kCMVideoCodecType_DVCPROHD1080i60  = 'dvh6',
    kCMVideoCodecType_DVCPROHD1080i50  = 'dvh5',
    kCMVideoCodecType_DVCPROHD1080p30  = 'dvh3',
    kCMVideoCodecType_DVCPROHD1080p25  = 'dvh2',
    
    kCMVideoCodecType_AppleProRes4444XQ = 'ap4x',
    kCMVideoCodecType_AppleProRes4444  = 'ap4h',
    kCMVideoCodecType_AppleProRes422HQ = 'apch',
    kCMVideoCodecType_AppleProRes422   = 'apcn',
    kCMVideoCodecType_AppleProRes422LT = 'apcs',
    kCMVideoCodecType_AppleProRes422Proxy = 'apco',

    kCMVideoCodecType_AppleProResRAW   = 'aprn',
    kCMVideoCodecType_AppleProResRAWHQ = 'aprh',
} API_AVAILABLE(macos(10.7), ios(4.0), tvos(9.0), watchos(6.0));


https://www.xamrdz.com/mobile/4uu1848716.html

相关文章: