0x00 需求
把視頻拆成圖片,視頻文件其實(shí)是一幀幀的圖片超全,視頻文件信息及結(jié)構(gòu)這里就不再贅述了咆霜,網(wǎng)上有很多講的都比較好的文章,請(qǐng)自行谷歌
0x01 代碼實(shí)現(xiàn)(AVAssetImageGenerator)
/**
* 把視頻文件拆成圖片保存在沙盒中
*
* @param fileUrl 本地視頻文件URL
* @param fps 拆分時(shí)按此幀率進(jìn)行拆分
* @param completedBlock 所有幀被拆完成后回調(diào)
*/
- (void)splitVideo:(NSURL *)fileUrl fps:(float)fps completedBlock:(void(^)())completedBlock {
if (!fileUrl) {
return;
}
NSDictionary *optDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *avasset = [[AVURLAsset alloc] initWithURL:fileUrl options:optDict];
CMTime cmtime = avasset.duration; //視頻時(shí)間信息結(jié)構(gòu)體
Float64 durationSeconds = CMTimeGetSeconds(cmtime); //視頻總秒數(shù)
NSMutableArray *times = [NSMutableArray array];
Float64 totalFrames = durationSeconds * fps; //獲得視頻總幀數(shù)
CMTime timeFrame;
for (int i = 1; i <= totalFrames; i++) {
timeFrame = CMTimeMake(i, fps); //第i幀 幀率
NSValue *timeValue = [NSValue valueWithCMTime:timeFrame];
[times addObject:timeValue];
}
NSLog(@"------- start");
AVAssetImageGenerator *imgGenerator = [[AVAssetImageGenerator alloc] initWithAsset:avasset];
//防止時(shí)間出現(xiàn)偏差
imgGenerator.requestedTimeToleranceBefore = kCMTimeZero;
imgGenerator.requestedTimeToleranceAfter = kCMTimeZero;
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSInteger timesCount = [times count];
[imgGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, CGImageRef _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {
printf("current-----: %lld\n", requestedTime.value);
switch (result) {
case AVAssetImageGeneratorCancelled:
NSLog(@"Cancelled");
break;
case AVAssetImageGeneratorFailed:
NSLog(@"Failed");
break;
case AVAssetImageGeneratorSucceeded: {
NSString *filePath = [cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%lld.png",requestedTime.value]];
NSData *imgData = UIImagePNGRepresentation([UIImage imageWithCGImage:image]);
[imgData writeToFile:filePath atomically:YES];
if (requestedTime.value == timesCount) {
NSLog(@"completed");
if (completedBlock) {
completedBlock();
}
}
}
break;
}
}];
}
0x10 獲取單幀圖片
- (nullable CGImageRef)copyCGImageAtTime:(CMTime)requestedTime actualTime:(nullable CMTime *)actualTime error:(NSError * __nullable * __nullable)outError CF_RETURNS_RETAINED;
requestedTime:
要獲取的幀時(shí)間CMTimeMake(a,b)
0x11 CMTime結(jié)構(gòu)體
/*!
@typedef CMTime
@abstract Rational time value represented as int64/int32.
*/
typedef struct
{
CMTimeValue value; /*! @field value The value of the CMTime. value/timescale = seconds. */
CMTimeScale timescale; /*! @field timescale The timescale of the CMTime. value/timescale = seconds. */
CMTimeFlags flags; /*! @field flags The flags, eg. kCMTimeFlags_Valid, kCMTimeFlags_PositiveInfinity, etc. */
CMTimeEpoch epoch; /*! @field epoch Differentiates between equal timestamps that are actually different because
of looping, multi-item sequencing, etc.
Will be used during comparison: greater epochs happen after lesser ones.
Additions/subtraction is only possible within a single epoch,
however, since epoch length may be unknown/variable. */
} CMTime;
CMTime是專門用于表示視頻時(shí)間的結(jié)構(gòu)體
value不是指時(shí)間
timescale可以理解為幀率
獲得秒數(shù):value / timescale = seconds
創(chuàng)建CMTime 用 CMTimeMake(a, b) 當(dāng)前第a幀嘶朱,每秒b幀