garry007
garry007

Reputation: 11

get the length of video selected or Recorded through iPhone Camera xcode

I am working on Fetching a video from Video Library or Recording a new video.Once selected in imagepicker method didfinishpickingmediawithinfo: i need to find th duration of Video that is selected from my Video album also the on i recorded from Camera. I have used AVAsset to fetch the duration in CMTime also i referred to MPMoviePlayer to fetch the duration of video but they don't provide me such property.

Any help would be appreciated

Regards

Upvotes: 1

Views: 3132

Answers (4)

etayluz
etayluz

Reputation: 16416

The other answers to this question using AVPlayerItem didn't work for me, but this did using AVURLAsset:

#import <AVFoundation/AVFoundation.h>

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSURL *videoURL=[info objectForKey:@"UIImagePickerControllerMediaURL"];
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];

    NSTimeInterval durationInSeconds = 0.0;
    if (asset)
        durationInSeconds = CMTimeGetSeconds(asset.duration);
}

Upvotes: 0

uttam
uttam

Reputation: 1364

1) just add the AVFoundation framwork in your app

2) then import the header file to your controller

#import <AVFoundation/AVFoundation.h> 

3) then add the above code written by victor.

NSURL *recordedTmpFile = [info objectForKey:UIImagePickerControllerMediaURL];

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:recordedTmpFile];

CMTime duration = playerItem.duration;
float seconds = CMTimeGetSeconds(duration);

Upvotes: 1

Iducool
Iducool

Reputation: 3653

MPMoviePlayerController has a property duration.

@property (nonatomic, readonly) NSTimeInterval duration

Please note that this property is read-only.

As per documentation

If the duration of the movie is not known, the value in this property is 0.0. If the duration is subsequently determined, this property is updated and a MPMovieDurationAvailableNotification notification is posted.

But why you are preferring MPMoviewPlayerController? AVFoundation is much faster for this type of operation. And you don't need to wait for any notification like in MPMoviewPlayerController.

if you would like to go through AVAsset. Here is a exact answer : MPMoviePlayerController - Duration always 0

Upvotes: 0

Victor
Victor

Reputation: 127

NSURL *recordedTmpFile = [info objectForKey:UIImagePickerControllerMediaURL];

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:recordedTmpFile];

CMTime duration = playerItem.duration;
float seconds = CMTimeGetSeconds(duration);

Upvotes: 1

Related Questions