TMan
TMan

Reputation: 4122

Getting duration and creation date from video from ios library

I'm trying to select/compress a video from the photo library but when I go to get the duration and creation date, they both are returning null (for duration this defaults to 0.0 sec). I'm not sure if I'm doing something wrong here.

- (void)imagePickerController:(UIImagePickerController *)uploadPick didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

 if (CFStringCompare (( CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo)
 {
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

    //Video Duration:
            MPMoviePlayerController *mp = [[MPMoviePlayerController alloc]
                                   initWithContentURL:videoURL];

    VideoTime.text = [NSString stringWithFormat:@"Time: %.2f", mp.duration];

    //Video Creation Date
    NSDictionary *metadataDictionary = (NSDictionary *)[info valueForKey:UIImagePickerControllerMediaMetadata];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    NSString *stringDate = [dateFormatter stringFromDate:metadataDictionary.fileCreationDate];
    [dateFormatter release];
    VideoDateTaken.text = [NSString stringWithFormat:@"Date Taken: %@", stringDate];

 }
}

Upvotes: 0

Views: 879

Answers (1)

tilo
tilo

Reputation: 14169

According to the documentation, UIImagePickerControllerMediaMetadata is only valid for still images:

This key is valid only when using an image picker whose source type is set to UIImagePickerControllerSourceTypeCamera, and applies only to still images.

In order do get the metadata you want, use an ALAsset and the metadata method.

Upvotes: 3

Related Questions