I am using UIImagePickerController
to choose video file from library. And user can upload the video.
Also I am using videoMaximumDuration
property while user wants to capture video and upload it.
I want to know that How can I get the duration of selected video file ? so that I can restrict the user to upload video that has duration more then 20 seconds.
I am able to get some basic info about selected video by this code :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
NSError *error;
NSDictionary * properties = [[NSFileManager defaultManager] attributesOfItemAtPath:selectedVideoUrl.path error:&error];
NSNumber * size = [properties objectForKey: NSFileSize];
NSLog(@"Vide info :- %@",properties);
}
But there is nothing about duration of selected video.
Thanks...
Got the solution : I use
AVPlayerItem
class andAVFoundation
andCoreMedia
framework.Swift 4
regardless of using
didFinishPickingMediaWithInfo
ordidFinishRecordingToOutputFileAtURL
you can:In the event that you use
AVFoundation
and AssetLibrary frameworks, you can enumerate all assets, apply filter for just videos, and get the duration of each videos with the method- (id)valueForProperty:(NSString *)property
. Pass inALAssetPropertyDuration
for property. Code below prints out the following on the console.video clip number 0 is 66.80833333333334 seconds
video clip number 1 is 190.06 seconds
video clip number 2 is 13.74 seconds
The other answer to this question using AVPlayerItem didn't work for me, but this did using AVURLAsset:
Yes, You can use the property "duration" defined by MPMediaPlayerController. Plese try it out and check the output. U can refer the here duration property
Try to use the MPMediaPlayerController to play the video and then use the property "duration". U will be able to clearly get the details about the duration of the video.
Swift 3.0 and Swift 4