Selecting videos less than 10 seconds long

I am integrating a custom image picker and I need to show videos from a camera that is less than 10 seconds long. Below code fetches all videos from gallery, but I want to apply a predicate to filter based on duration.

let options = PHFetchOptions()
    options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true) ]
    options.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue)
    assets = PHAsset.fetchAssets(with: options)
    print(assets ?? "no video found")
    collectionView.reloadData()

      

Please let me know if anyone has any idea about this. Thank.

+3


source to share


1 answer


Just add a condition to the predicate



options.predicate = NSPredicate(format: "mediaType = %d AND duration < 10", PHAssetMediaType.video.rawValue)

      

+4


source







All Articles