Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone :How to get duration of video selected from library?

Tags:

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...

like image 634
Maulik Avatar asked Jan 02 '12 09:01

Maulik


1 Answers

Got the solution : I use AVPlayerItem class and AVFoundation and CoreMedia framework.

#import <AVFoundation/AVFoundation.h> #import <AVFoundation/AVAsset.h>  - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {     selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL];      AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:selectedVideoUrl];      CMTime duration = playerItem.duration;     float seconds = CMTimeGetSeconds(duration);     NSLog(@"duration: %.2f", seconds); } 
like image 172
Maulik Avatar answered Oct 22 '22 17:10

Maulik