1.獲取視頻的時長
創(chuàng)建一個model類來創(chuàng)建方法
//model.h文件中實現(xiàn)
+ (CGFloat)getTimeFromVideoPath:(NSString *)filePath;
//model.m文件中實現(xiàn)
+ (CGFloat)getTimeFromVideoPath:(NSString *)filePath
{
AVURLAsset * asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:filePath]];
CMTime time = [asset duration];
CGFloat seconds = time.value/time.timescale;
return seconds;
}
2.獲取視頻的縮略圖
//model.h文件中實現(xiàn)
+ (UIImage *)getScreenShotImageFromVideoPath:(NSString *)filePath;
//model.m文件中實現(xiàn)
+ (UIImage *)getScreenShotImageFromVideoPath:(NSString *)filePath
{
//視頻路徑URL
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:fileURL 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 *shotImage = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return shotImage;
}
上述代碼中filePath為本地視頻的路徑幔崖,CMTime為C的一個結(jié)構(gòu)體食店,duration為AVAsset中表示時長的屬性值。value和timescale都是CMTime結(jié)構(gòu)體中的值赏寇,其中value 表示當(dāng)前的CMTime 的值吉嫩,timescale 當(dāng)前的CMTime 的時間刻度,time.value/time.timescale即為時長嗅定,結(jié)構(gòu)函數(shù)CMTimeMakeWithSeconds包含兩個參數(shù)自娩,第一個參數(shù)表示當(dāng)前的時間,第二個參數(shù)表示每秒的幀數(shù)渠退,使用copyCGImageAtTime方法來獲取指定時間段time的截圖(例如代碼中設(shè)置設(shè)置為0忙迁,即表示截取第一秒的界面作為縮略圖)。
3.VC中調(diào)用
//視頻時長
CGFloat time = [PublicModel getTimeFromVideoPath:@"本地視頻的路徑"];
//縮略圖
UIImage *image = [PublicModel getScreenShotImageFromVideoPath:@"本地視頻的路徑"];