拍摄视频后,我使用UIImagePickerController
创建了相机。我点击了使用视频,我将此视频保存到了照片库。现在我想获得这个视频的大小。我使用AlAssetLibrary
将视频保存到Photo库。如何才能获得视频大小?请帮我。提前谢谢。
-(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){}
];
}
}
我尝试使用以下代码来获取视频大小但不起作用:
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];
答案 0 :(得分:1)
1)请参阅this 获取视频的问题。
2)获取视频后,将其转换为NSData
作为引用this question。
3)现在使用NSData的length function。
它会让您获得视频文件的长度。
答案 1 :(得分:0)
您也可以使用它来获取视频大小:
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:YOURURL];
CMTime duration = playerItem.duration;
float seconds = CMTimeGetSeconds(duration);
答案 2 :(得分:0)
你可以用三种方式做到这一点
1.使用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.使用ALAssetRepresentationsize
3. 最坏情况场景:使用指向已保存视频文件的videoPathURL
,使用ALAsset
再次检索并计算大小。