4

I a trying to transcode an H264 video to HEVC using AVAssetWriter and it fails on iPhone 6s. Supposedly, the iPhone 6s supports HEVC for transcoding, not real-time video encoding. The same code works on iPhone 7 and above. If the iPhone 6s doesn't support the HEVC codec, how do we programmatically determine supported codecs at runtime?

let bitrate = trackBitrate / 5 
let trackDimensions = trackSize
let compressionSettings: [String: Any] = [
    AVVideoAverageBitRateKey: bitrate,
    AVVideoMaxKeyFrameIntervalKey: 30,
    AVVideoProfileLevelKey: kVTProfileLevel_HEVC_Main_AutoLevel
]
var videoSettings: [String : Any] = [
    AVVideoWidthKey: trackDimensions.width,
    AVVideoHeightKey: trackDimensions.height,
    AVVideoCompressionPropertiesKey: compressionSettings
]

videoSettings[AVVideoCodecKey] =  AVVideoCodecType.hevc

2 Answers 2

2

I ended up doing it this way

if #available(iOS 11.0, *),  AVCaptureVideoDataOutput().availableVideoCodecTypes.contains(.hevc) {
    // use .hevc settings here
} else {
    // use .h264 settings here
}

The #available check is needed to make the compiler happy if your app is targeting < iOS 11

2
  • It would still fail on iPhone6
    – bojan
    Nov 19, 2021 at 16:54
  • This would give an empty array in some cases and so always fail
    – Glaphi
    Feb 22, 2022 at 13:58
0

You can get the iPhone model by the following code:

+ (NSString *) deviceModel {
    struct utsname systemInfo;
    uname(&systemInfo);

    return [NSString stringWithCString: systemInfo.machine encoding: NSUTF8StringEncoding];
}

and determine if iPhone 6S disable H265 encode and iPhone7 above enable H265 encode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.