I can request a video PHAsset using the Photos framework in iOS8. I'd like to know how big the file is on disk. There doesn't seem to be a property of PHAsset to determine that. Does anyone have a solution? (Using Photos framework not required)
问问题
12384 次
3 回答
30
编辑
至于iOS 9.3,requestImageDataForAsset
在视频类型上使用PHAsset
会产生一个图像,这是视频的第一帧,所以它不再起作用了。改用下面的方法,对于普通视频,请求选项可以是nil
,但对于慢动作视频,PHVideoRequestOptionsVersionOriginal
需要设置。
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];
NSLog(@"size is %f",[size floatValue]/(1024.0*1024.0)); //size is 43.703005
}
}];
//原始答案
对于 PHAsset,使用这个:
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
float imageSize = imageData.length;
//convert to Megabytes
imageSize = imageSize/(1024*1024);
NSLog(@"%f",imageSize);
}];
对于 ALAsset:
ALAssetRepresentation *rep = [asset defaultRepresentation];
float imageSize = rep.size/(1024.0*1024.0);
我在一个视频资产上进行了测试,PHAsset 显示大小为 43.703125,ALAsset 显示大小为 43.703005。
编辑
PHAsset,另一种获取文件大小的方法。但是正如@Alfie Hanssen 提到的,它适用于普通视频,对于慢动作视频,以下方法将在块中返回一个 AVComposition 资产,因此我添加了对其类型的检查。对于慢动作视频,请使用该requestImageDataForAsset
方法。
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:nil 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];
NSLog(@"size is %f",[size floatValue]/(1024.0*1024.0)); //size is 43.703005
NSData *data = [NSData dataWithContentsOfURL:urlAsset.URL];
NSLog(@"length %f",[data length]/(1024.0*1024.0)); // data size is 43.703005
}
}];
于 2014-10-24T16:16:24.723 回答
2
带有文件大小格式的 Swift 版本:
let options = PHVideoRequestOptions()
options.version = .original
PHImageManager.default().requestAVAsset(forVideo: asset, options: options) { avAsset, _, _ in
if let urlAsset = avAsset as? AVURLAsset { // Could be AVComposition class
if let resourceValues = try? urlAsset.url.resourceValues(forKeys: [.fileSizeKey]),
let fileSize = resourceValues.fileSize {
let formatter = ByteCountFormatter()
formatter.countStyle = .file
let string = formatter.string(fromByteCount: Int64(fileSize))
print(string)
}
}
}
于 2020-07-07T08:36:35.287 回答
0
你很有可能,你想知道的视频的大小不是AVURLAsset
. 但是,在引擎盖下有更多的文件是您的视频合成的(例如原始样本、慢动作时间范围、过滤器等),因为您想知道具体可播放文件的大小。我不确定在这种情况下估计的文件大小如何满足现实,但应该这样做:
PHImageManager.defaultManager().requestExportSessionForVideo(asset, options: nil, exportPreset: AVAssetExportPresetHighestQuality, resultHandler: { (assetExportSession, info) -> Void in // Here you set values that specifies your video (original, after edit, slow-mo, ...) and that affects resulting size.
assetExportSession.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(asset.duration, 30)) // Time interval is default from zero to infinite so it needs to be set prior to file size computations. Time scale is I believe (it is "only" preferred value) not important in this case.
let HERE_YOU_ARE = assetExportSession.estimatedOutputFileLength
})
于 2015-04-02T17:33:18.453 回答