如何检查视频文件是否大于2MB?

时间:2011-05-13 06:11:29

标签: iphone objective-c video size

假设我从iphone库中获取了一个视频文件。我想检查视频文件不应该大于2MB。

我无法使用videoMaximumDuration方法。因为如果任何视频质量很高,即使持续1分钟,视频的大小也会很大。

任何观点?

3 个答案:

答案 0 :(得分:3)

urlvideo包含所选视频文件的网址

            NSString *strurl=[urlvideo path];
            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:strurl error:nil];

        if(fileAttributes != nil)
            {
                NSString *fileSize = [fileAttributes objectForKey:NSFileSize];
                //NSLog(@"File size: %@ kb", fileSize);             
                if ([fileSize intValue] > 2000000) {                    
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"File size greater than 2MB.Please select another video file." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alert show];
                    [alert release];
                }               
                else {
NSLog(@"video size less than 2 mb");
    }

答案 1 :(得分:0)

答案 2 :(得分:0)

NSURL解决方案不起作用,因为来自用户的iPod或照片库的视频的网址不是文件网址,而是MediaPlayer或ALAssetLibrary处理的特殊方案。 (我对ALAssetLibrary这样做并不乐观,但我知道MediaPlayer会这样做,并且可以想象照片库也会这样做,所以你不能用它后面的东西捣碎)。

我能想到的最佳解决方案是使用URL创建AVURLAsset,然后遍历轨道并将estimatedDataRate乘以轨道持续时间(以秒为单位)。这应该为每个曲目提供粗略的大小估计。