3

Currently, I am extracting EXIF metadata for PHAsset's that are images, but not having luck with videos.

For images, this works for me:

let imageOptions = PHImageRequestOptions()
imageOptions.isNetworkAccessAllowed = true
imageOptions.isSynchronous = true
imageOptions.version = .current

PHImageManager.default().requestImageDataAndOrientation(for: self.asset!, options: imageOptions) { (data, responseString, orientation, info) in

  if let imageData: Data = data {
   if let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil) {
                        
    let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)! as NSDictionary

   }
 }

})

What alteration would I need to retrieve metadata for a video asset?

self.asset!.mediaType == .video

UPDATE

After the first answer, I tried studying: https://developer.apple.com/documentation/avfoundation/media_assets_and_metadata/retrieving_media_metadata

So far, I am still having issues understanding the concept. I tried:

let formatsKey = "availableMetadataFormats"

asset.loadValuesAsynchronously(forKeys: [formatsKey]) {
    var error: NSError? = nil
    let status = asset.statusOfValue(forKey: formatsKey, error: &error)
    if status == .loaded {
        for format in asset.availableMetadataFormats {
            let metadata = asset.metadata(forFormat: format)
            print (metadata)
        }
    }
}

I wasn't able to extract anything inside PHImageManager.default().requestAVAsset, coming up empty.

What I need is video fps/codec/sound(stereo or mono)/colorspace. That is it. I was able to get somewhere with:

if let videoTrack = asset.tracks(withMediaType: .video).first {
   let videoFormatDescription = videoTrack.formatDescriptions.first as! CMVideoFormatDescription
                            
   print (videoFormatDescription)             

}

Within CMVideoFormatDescription, most of the required attributes seem to be present, yet, I am unable to extract them so far.

Gizmodo
  • 3,151
  • 7
  • 45
  • 92
  • 1
    "Currently, I am extracting EXIF metadata for PHAsset's that are images, but not having luck with images" Unclear. Should one of those words be "videos" perhaps? – matt Feb 25 '21 at 02:47
  • Indeed. Corrected. – Gizmodo Feb 25 '21 at 02:58

1 Answers1

2

Call requestAVAsset(forVideo:options:resultHandler:). Now you have an AVAsset. It has metadata and commonMetadata properties and you’re off to the races.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    Pretty good discussion in the docs: https://developer.apple.com/documentation/avfoundation/media_assets_and_metadata/retrieving_media_metadata – matt Feb 25 '21 at 03:30
  • The main reason I am trying to tap into 'Metadata' is to get EXIF tidbits such as fps, colorspace, bitrate, etc. From reading those links, all these seem to be located all over the place. – Gizmodo Feb 25 '21 at 04:41
  • 1
    Well standard exif doesn't exist for video the way it does for photos. So the API gives you the metadata that it has. There are various formats and that's why you start by learning what format(s) even exist, with https://developer.apple.com/documentation/avfoundation/avmetadataformat. As I said, the docs are quite good on this. – matt Feb 26 '21 at 06:28
  • Still having issues trying to accomplish this. OP updated. – Gizmodo Mar 05 '21 at 19:46