How to place check whether Video file is greater than 2MB?

有些话、适合烂在心里 提交于 2019-12-07 22:58:36

问题


Suppose I have taken a video file from iphone library. I want to put a check that Video file shouldn't be greater than 2MB.

I cant use videoMaximumDuration method. Because if any video is hd quality even 1 min duration video could be huge in size.

Any views ?


回答1:


urlvideo contains the url of selected video file

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



回答2:


If you wish to ensure the file size of video than following will help.

http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/LowLevelFileMgmt/Articles/FileInfo.html#//apple_ref/doc/uid/TP40009068-SW1

http://developer.apple.com/library/mac/#documentation/cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/instm/NSURL/resourceValuesForKeys:error: and

http://developer.apple.com/library/mac/#documentation/cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/instm/NSURL/getResourceValue:forKey:error:

Thanks,




回答3:


NSURL solutions won't work because the url to a video from the user's iPod or Photo library will not be a file url, but a special scheme that the MediaPlayer or ALAssetLibrary handles. (I'm not positive on the ALAssetLibrary doing this, but I know the MediaPlayer does it and would imagine the photo library does it too so you can't muck with stuff behind its back).

The best solution I can think of is to create an AVURLAsset with the URL, and then iterate through the tracks and multiply the estimatedDataRate by the track duration in seconds. That should give you a rough size estimate for each track.



来源:https://stackoverflow.com/questions/5987912/how-to-place-check-whether-video-file-is-greater-than-2mb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!