如何获取视频PHAsset磁盘文件大小?

时间:2016-08-06 08:25:15

标签: avasset phasset photokit

我正在尝试确定PHAsset视频的磁盘上的文件大小。

以下代码返回错误“文件”IMG_0188.mov“无法打开,因为您无权查看它。”

PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
            options.version = PHVideoRequestOptionsVersionOriginal;

            [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {
                videoURL = [(AVURLAsset*)avAsset URL];

                NSNumber *videoDiskFileSize;
                NSError *error;
                [videoURL getResourceValue:&videoDiskFileSize forKey:NSURLFileSizeKey error:&error];

                if(error){
                    NSLog(@"ERROR %@", error.localizedDescription);
                }

                NSLog(@"size is %f",[videoDiskFileSize floatValue]/(1024.0*1024.0));
            }];

1 个答案:

答案 0 :(得分:0)

这是正确的方法。

PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
            options.version = PHVideoRequestOptionsVersionOriginal;

            [[PHImageManager defaultManager] requestExportSessionForVideo:asset options:options exportPreset:AVAssetExportPresetHighestQuality resultHandler:^(AVAssetExportSession *exportSession, NSDictionary *info) {
                CMTime videoDuration = CMTimeMultiplyByFloat64(exportSession.asset.duration, 1);
                exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, videoDuration);
                NSLog(@"Byyte size = %lld", exportSession.estimatedOutputFileLength);

                videoSize = exportSession.estimatedOutputFileLength;
            }];