如何确定iOS8中视频PHAsset的磁盘上的文件大小

时间:2014-10-24 14:27:17

标签: ios video photokit photosframework

我可以使用iOS8中的Photos框架请求视频PHAsset。我想知道文件在磁盘上有多大。似乎没有PHAsset的属性来确定它。有没有人有办法解决吗? (不需要使用Photos框架)

3 个答案:

答案 0 :(得分:24)

修改

对于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
    }
}];

答案 1 :(得分:2)

具有文件大小格式的快速版本:

    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)
            }
        }
    }

答案 2 :(得分: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
})