Gizmodo
Gizmodo

Reputation: 3222

PHAsset video EXIF metadata retrieval

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.

Upvotes: 3

Views: 998

Answers (1)

matt
matt

Reputation: 535232

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

Upvotes: 2

Related Questions