/**
* @method
*
* @brief 根據(jù)路徑獲取視頻時長和大小
* @param path? ? ? 視頻路徑
* @return? ? 字典? ? @"size"--文件大小? @"duration"--視頻時長
*/
- (NSDictionary *)getVideoInfoWithSourcePath:(NSString *)path{
AVURLAsset * asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:path]];
CMTime? time = [asset duration];
int seconds = ceil(time.value/time.timescale);
NSInteger? fileSize = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil].fileSize;
return @{@"size" : @(fileSize),
@"duration" : @(seconds)};
}
//獲取視頻文件的大小,返回的是單位是M。
- (CGFloat)getFileSize:(NSString *)path{
NSFileManager *fileManager = [[NSFileManager alloc] init];
float filesize = -1.0;
if ([fileManager fileExistsAtPath:path]) {
NSDictionary *fileDic = [fileManager attributesOfItemAtPath:path error:nil];//獲取文件的屬性
unsigned long long size = [[fileDic objectForKey:NSFileSize] longLongValue];
filesize = (1.0*size/1024)/1024.0;
}
return filesize;
}
//獲取視頻文件的時長。
- (CGFloat)getVideoLength:(NSURL *)URL{
NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:URL options:opts];
int second = 0;
second = urlAsset.duration.value/urlAsset.duration.timescale;
NSDate? *date = [NSDate dateWithTimeIntervalSince1970:second];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localeDate = [date? dateByAddingTimeInterval: interval];
NSLog(@"enddate=%@",localeDate);
int seconds = second % 60;
int minutes = (second / 60) % 60;
NSLog(@"%02d:%02d", minutes, seconds);
return second;
}
//獲取本地視頻縮略圖,網(wǎng)上說需要添加AVFoundation.framework
- (UIImage *)getImage:(NSURL *)URL{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:URL options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return thumb;
}