获取视屏封面和时长和大小

/**

* @method

*

* @brief 根据路径获取视频时长和大小

* @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;

}

//获取本地视频缩略图,网上说需要添加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;

}

你可能感兴趣的:(获取视屏封面和时长和大小)