Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 453
khushboo Avatar asked Jan 30 '23 02:01

khushboo


1 Answers

Just add the condition to the predicate

options.predicate = NSPredicate(format: "mediaType = %d AND duration < 10", PHAssetMediaType.video.rawValue)
like image 134
vadian Avatar answered Feb 05 '23 15:02

vadian