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
Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131

2 Answers2

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

Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
Ehab Amer
  • 392
  • 1
  • 9
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.

Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
hzhou
  • 9
  • 4