DKImagePickerController icon indicating copy to clipboard operation
DKImagePickerController copied to clipboard

Limiting Video Length

Open islamabdelraouf opened this issue 8 years ago • 20 comments

How can I limit the video length in the same sense as UIImagePickerController setting the maximum video duration?

islamabdelraouf avatar Aug 15 '16 13:08 islamabdelraouf

The DKImagePickerController does not support video recording.

zhangao0086 avatar Aug 16 '16 06:08 zhangao0086

@zhangao0086 I'm talking about selecting a video from the phone gallery, In UIImagePickerController you can set the maximum video duration, if the selected video is longer than the maximum duration your'll be presented with something similar to UIVideoEditorController, which will force you to trim the video before selecting it. Implementing this manually will be something similar to this,

                        let maxDuration:Float64 = 60
                        let videoDuration = asset!.duration
                        let videoDurationSeconds = CMTimeGetSeconds(videoDuration)

                        if videoDurationSeconds > maxDuration
                        {
                            if UIVideoEditorController.canEditVideoAtPath(asset!.URL.path!)
                            {
                                let videoEditor = UIVideoEditorController()
                                self.videoEditor.videoPath = asset!.URL.path!
                                self.videoEditor.videoMaximumDuration = 60.0
                                self.presentViewController(self.videoEditor, animated: true, completion: nil)

                            }

                        }


                        // MARK: - UIVideoEditorControllerDelegate

                        func videoEditorController(editor: UIVideoEditorController, didSaveEditedVideoToPath editedVideoPath: String) {

                            self.videoEditor.dismissViewControllerAnimated(true, completion: nil)
                            //Do whatever you wish with the trimmed video here
                        }

islamabdelraouf avatar Aug 16 '16 09:08 islamabdelraouf

Sounds good to me. I'll add it in the next version :)

zhangao0086 avatar Aug 17 '16 05:08 zhangao0086

@zhangao0086 Do you have implemented this ? Setting max duration of video that can be selected. for ex. we can select max 3 minutes video?

hadwanihardik avatar Sep 20 '16 10:09 hadwanihardik

My fault. This feature has been supported. e.g. Gets less than 10 seconds of video:

pickerController.videoFetchPredicate = NSPredicate(format: "duration <= %f", 10.0)

zhangao0086 avatar Oct 08 '16 08:10 zhangao0086

pickerController.videoFetchPredicate = NSPredicate(format: "duration <= %f", 10.0), is not the same, as this will not show any video above 10 seconds, so basically the user will not see it in the picker, however the code I previously shared will show all videos and once selected will let the user trim the selected video to the desired duration

islamabdelraouf avatar Oct 10 '16 08:10 islamabdelraouf

@islamabdelraouf Sorry for closing this issue. I've created a temporary branch Feature/ObservingAssetChanges that just a demo project. Could you see it works as you expect?

zhangao0086 avatar Oct 10 '16 09:10 zhangao0086

Any update on this

jovanpreet avatar Jan 24 '17 08:01 jovanpreet

Is there any update on this?

bkunarola avatar Jul 23 '19 11:07 bkunarola

Any update on this ??

vadivelmuruganvv avatar Dec 23 '19 08:12 vadivelmuruganvv

Any update on this? It seems videoFetchPredicate has also been removed in v4.2.1. Is there anything that has been added to replace this to limit the duration?

pramittewari avatar Mar 04 '20 12:03 pramittewari

I even tried creating an object of DKImagePickerController with using custom configurations in the DKImageGroupDataManager, which failed to work. The code looks like so:

func createPickerController(forAssetType assetType: DKImagePickerControllerAssetType) -> DKImagePickerController {
        
        let configurations = DKImageGroupDataManagerConfiguration()
        
        let createImagePredicate = { () -> NSPredicate in
            let imagePredicate = NSPredicate(format: "mediaType == %d",
                                             PHAssetMediaType.image.rawValue)
            
            return imagePredicate
        }
        
        let createVideoPredicate = { () -> NSPredicate in
            let videoPredicate = NSPredicate(format: "mediaType == %d",
                                             PHAssetMediaType.video.rawValue)
            
            return videoPredicate
        }
        
        let createVideoDurationPredicate = { () -> NSPredicate in
            let videoPredicate = NSPredicate(format: "duration <= %f", 240.0)
            
            return videoPredicate
        }
        
        var predicate: NSPredicate?
        switch assetType {
        case .allAssets:
            predicate = NSCompoundPredicate(orPredicateWithSubpredicates:
                [createImagePredicate(), createVideoPredicate(), createVideoDurationPredicate()]
            )
        case .allPhotos:
            predicate = createImagePredicate()
            
        case .allVideos:
            predicate = NSCompoundPredicate(orPredicateWithSubpredicates:
            [createVideoPredicate(), createVideoDurationPredicate()]
            )
        }
        
        let assetFetchOptions = PHFetchOptions()
        assetFetchOptions.predicate = predicate
        configurations.assetFetchOptions = assetFetchOptions
        configurations.fetchLimit = 0
        
        let manager = DKImageGroupDataManager(configuration: configurations)
        
        let pickerController = DKImagePickerController(groupDataManager: manager)
        pickerController.assetType = assetType
        
        return pickerController
    }

pramittewari avatar Mar 04 '20 13:03 pramittewari

Hi @pramittewari Try this:

let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [
    NSPredicate(format: "mediaType == %d", PHAssetMediaType.video.rawValue),
    NSPredicate(format: "duration < %d", 10),
])

let configuration = DKImageGroupDataManagerConfiguration()
configuration.assetFetchOptions = fetchOptions

let groupDataManager = DKImageGroupDataManager(configuration: configuration)
let pickerController = DKImagePickerController(groupDataManager: groupDataManager)

It will only give you videos less than 10 seconds.

zhangao0086 avatar Mar 04 '20 13:03 zhangao0086

I've done the same thing in the above function.

pramittewari avatar Mar 04 '20 13:03 pramittewari

The difference I see is:

Mine says: "duration <= %f", 240.0 Yours says: "duration < %d", 10

Is this it?

pramittewari avatar Mar 04 '20 13:03 pramittewari

From my test results: "duration < %d", 10 works for me "duration <= %f", 240.0 failed to work

Let me know if that works for you too :)

zhangao0086 avatar Mar 04 '20 14:03 zhangao0086

That's right. It's the only change that was required. Thanks for the promptness! =)

pramittewari avatar Mar 04 '20 14:03 pramittewari

One more thing, does the new version (4.2.1) have any feature of displaying all videos but upon selection provide an option of trimming if longer than a particular duration?

pramittewari avatar Mar 04 '20 14:03 pramittewari

@pramittewari No, it does not support trimming videos.

zhangao0086 avatar Mar 04 '20 15:03 zhangao0086

非常感谢您的三方,很好用,现在我遇到一个问题,希望在设置只有视频的时候点击相机能跳转到视频拍摄并获取拍摄的视频?我该怎么做呢?

RedBluePeas avatar Mar 27 '20 07:03 RedBluePeas