Search code examples
iosswiftavfoundationavassetwriter

AVVideoCompressionPropertiesKey crash


I am doing real time video recording using AVAssetWriter and AVCaptureVideoDataOutput. I see many crash reports on user devices but can not reproduce it on any of my devices. I need info on what could be going wrong in code below. It happens probably because typecast of NSDictionary to [String:AnyObject] fails.

In the class I have:

private var videoOut: AVCaptureVideoDataOutput?

And then in a function, I do this:

  var recommendedSettings:[String:AnyObject]?

  recommendedSettings = videoOut.recommendedVideoSettings(forVideoCodecType: AVVideoCodecType.hevc, assetWriterOutputFileType: AVFileType.mov) as? [String : AnyObject]

  if let recommendedSettings = recommendedSettings {

  /* crashes here */
  var compressionSettings = recommendedSettings[AVVideoCompressionPropertiesKey] as! [String:AnyObject]


  }

What is the assumed return type for dictionary returned by AVVideoCompressionPropertiesKey in Swift?


Solution

  • The returned type is the dictionary, i.e

    let videoCompressionProps: Dictionary<String, Any> = [
        AVVideoAverageBitRateKey : videoBitrate,
        AVVideoMaxKeyFrameIntervalKey : videomaxKeyFrameInterval,
        AVVideoProfileLevelKey : avVideoProfileLevel
    ]