1 、获取网络链接的视频大小和时长
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];// url:网络视频的连接
NSArray *arr = [asset tracksWithMediaType:AVMediaTypeVideo];// 项目中是明确媒体类型为视频,其他没试过
CGSize videoSize =CGSizeZero;
for (AVAssetTrack *track in arr) {
if([track.mediaType isEqualToString:AVMediaTypeVideo])
{
if (track.totalSampleDataLength >= 1048576) {//1048576bt = 1M 小于1m的显示KB 大于1m显示M
lable.text = [NSString stringWithFormat:@"%.2lldM",track.totalSampleDataLength/1024/1024];
} else {
lable.text = [NSString stringWithFormat:@"%.1lldKB",track.totalSampleDataLength/1024];
}
videoSize = track.naturalSize;
}
}
2 、获取保存在你app沙盒中的视频文件
导入 #import <AVFoundation/AVFoundation.h>
/**
* @method
*
* @brief 根据路径获取视频时长和大小
* @param path 视频路径
* @return 字典 @"size"--文件大小 @"duration"--视频时长
*/
- (NSDictionary *)getVideoInfoWithSourcePath:(NSString *)path{
AVURLAsset * asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:path]];
CMTime time = [asset duration];
int seconds = ceil(time.value/time.timescale);
NSInteger fileSize = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil].fileSize;
return @{@"size" : @(fileSize),
@"duration" : @(seconds)};
}
3 、获取PHAsset 文件大小
PHAssetResource *resource = [[PHAssetResource assetResourcesForAsset:asset] firstObject];
long long size = [[resource valueForKey:@"fileSize"] longLongValue];
和
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHVideoRequestOptionsVersionOriginal;
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
if ([asset isKindOfClass:[AVURLAsset class]]) {
AVURLAsset* urlAsset = (AVURLAsset*)asset;
NSNumber *size;
[urlAsset.URL getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
});
} else {
imageLable.text = [NSString stringWithFormat:@"0KB"];
}
}];