獲取視屏封面和時長和大小

/**

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

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容