iOS开发视频分解成图片
来源:互联网 发布:重庆的数据的新闻 编辑:程序博客网 时间:2024/04/28 11:20
视频其实就是一张张图片组成的,将视频拆分成图片。
这里视频分解图片使用的是AVAssetImageGenerator,利用这个类可以很方便的实现不同时间戳下,视频帧的抓取。注意一般这种视频分解图片帧的方法都是放在子线程中的,而UI更新操作都是放在主线程中的。
使用下面方法获取视频的每一帧图片进行处理:
/*! @method generateCGImagesAsynchronouslyForTimes:completionHandler: @abstract Returns a series of CGImageRefs for an asset at or near the specified times. @param requestedTimes An NSArray of NSValues, each containing a CMTime, specifying the asset times at which an image is requested. @param handler A block that will be called when an image request is complete. @discussion Employs an efficient "batch mode" for getting images in time order. The client will receive exactly one handler callback for each requested time in requestedTimes. Changes to generator properties (snap behavior, maximum size, etc...) will not affect outstanding asynchronous image generation requests. The generated image is not retained. Clients should retain the image if they wish it to persist after the completion handler returns.*/- (void)generateCGImagesAsynchronouslyForTimes:(NSArray<NSValue *> *)requestedTimes completionHandler:(AVAssetImageGeneratorCompletionHandler)handler;
下面核心代码:
- (void)splitVideo:(NSURL *)fileUrl fps:(float)fps splitCompleteBlock:(void(^)(BOOL success, NSMutableArray *splitimgs))splitCompleteBlock { if (!fileUrl) { return; } NSMutableArray *splitImages = [NSMutableArray array]; NSDictionary *optDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey]; AVURLAsset *avasset = [[AVURLAsset alloc] initWithURL:fileUrl options:optDict]; CMTime cmtime = avasset.duration; //视频时间信息结构体 Float64 durationSeconds = CMTimeGetSeconds(cmtime); //视频总秒数 NSMutableArray *times = [NSMutableArray array]; Float64 totalFrames = durationSeconds * fps; //获得视频总帧数 CMTime timeFrame; for (int i = 1; i <= totalFrames; i++) { timeFrame = CMTimeMake(i, fps); //第i帧 帧率 NSValue *timeValue = [NSValue valueWithCMTime:timeFrame]; [times addObject:timeValue]; } AVAssetImageGenerator *imgGenerator = [[AVAssetImageGenerator alloc] initWithAsset:avasset]; //防止时间出现偏差 imgGenerator.requestedTimeToleranceBefore = kCMTimeZero; imgGenerator.requestedTimeToleranceAfter = kCMTimeZero; NSInteger timesCount = [times count]; // 获取每一帧的图片 [imgGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, CGImageRef _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) { NSLog("current-----: %lld", requestedTime.value); NSLog("timeScale----: %d",requestedTime.timescale); // 帧率 BOOL isSuccess = NO; switch (result) { case AVAssetImageGeneratorCancelled: NSLog(@"Cancelled"); break; case AVAssetImageGeneratorFailed: NSLog(@"Failed"); break; case AVAssetImageGeneratorSucceeded: { UIImage *frameImg = [UIImage imageWithCGImage:image]; [splitImages addObject:frameImg]; if (requestedTime.value == timesCount) { isSuccess = YES; NSLog(@"completed"); } } break; } if (splitCompleteBlock) { splitCompleteBlock(isSuccess,splitImages); } }];}
贴上github地址:https://github.com/ismilesky/MaxVideo.git
0 0
- iOS开发视频分解成图片
- IOS 视频分解图片、图片合成视频
- IOS视频分解图片、图片合成视频
- 视频分解图片,图片合成视频
- opencv 视频分解成图片 图片合成视频 批量读取图片
- iOS开发15-iOS SQLite存取图片、视频、音频
- iOS开发技巧之:iOS 视频图片压缩上传
- opencv分解视频并保存图片
- 4、批量读取图片、视频分解、视频合成
- iOS开发截取视频任意帧图片——MPMoviePlayerController
- iOS 图片转视频
- iOS学习笔记--gif图片分解
- 在iOS开发中,在视频初始状态时获取视频的第一帧图片
- ios开发视频
- IOS开发---视频录制
- iOS视频开发经验
- iOS视频开发经验
- iOS视频开发经验
- 简单的Android中MVP模式的应用
- Java高级软件工程师面试考纲
- Win10 64位+mongodb集群搭建
- spring mvc messageConverters及406等问题
- hdu1069动态规划
- iOS开发视频分解成图片
- 解决ie的window.open的referer丢失问题。
- js提交数据
- linux 定时任务
- Spring MVC 參數映射轉換(CustomEditorConfigurer)和类型转换器(ConversionServiceFactoryBean)
- jquery中的ajax
- ARM裸机之ADC
- 垃圾回收机制的实验记录
- 【对号入座,看看自己差距】