Search code examples
iosswiftvideocore-dataphasset

fetch videos with duration less than 10 seconds


I am integrating custom image picker and i have to show videos from camera roll whose duration is less than 10 seconds. Below code fetches all videos from gallery but i want to apply predicate to filter based on duration as well.

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 regarding the same. Thanks.


Solution

  • Just add the condition to the predicate

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