user3168540
user3168540

Reputation: 77

Get video size after Save this video to Photo Library in iOS

I created Camera using UIImagePickerController, after take a video. I clicked on Use Video, I saved this video to Photo library. And now I want to get size of this video. I used AlAssetLibrary to save video to Photo library. How can get size of the video has just taken? Please help me. Thanks in advance.

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    NSURL *recordedVideoURL= [info objectForKey:UIImagePickerControllerMediaURL];
    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:recordedVideoURL]) {
        [library writeVideoAtPathToSavedPhotosAlbum:recordedVideoURL
                                    completionBlock:^(NSURL *assetURL, NSError *error){}
         ];
    }

}

I tried below code to get size of video but not works:

 ALAssetRepresentation *rep = [alasset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSError *error = nil;
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:&error];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

Upvotes: 0

Views: 1144

Answers (3)

Shamsudheen TK
Shamsudheen TK

Reputation: 31311

You can do it in three ways

1.Use NSData length

    ALAssetRepresentation *rep = [alasset defaultRepresentation];
    Byte *buffer = (Byte*)malloc(rep.size);
    NSError *error = nil;
    NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:&error];
    NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
    NSLog(@"Size of video %d",data.length); 

2.Make use of ALAssetRepresentationsize

3.Worst-Case Scenario: Use the videoPathURL that points to the saved video file, retrieve it again using ALAsset and caliculate the size.

Upvotes: 0

Shubham
Shubham

Reputation: 570

you can also use this for getting size of video :

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:YOURURL];

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

Upvotes: 0

Ankush
Ankush

Reputation: 2555

1) Please see this question to get your video.

2) After getting your video convert it into NSData as refer this question.

3)Now use NSData's length function .

It will give to to get the length of your video file.

Upvotes: 1

Related Questions