IOS 视频分解图片、图片合成视频
2016-07-09 22:58
525 查看
在IOS视频处理中,视频分解图片和图片合成视频是IOS视频处理中经常遇到的问题,这篇博客就这两个部分对IOS视频图像的相互转换做一下分析。
(1)视频分解图片
这里视频分解图片使用的是AVAssetImageGenerator,利用这个class可以很方便的实现不同时间戳下,视频帧的抓取。注意一般这种视频分解图片帧的方法都是放在子线程中的,而UI更新操作都是放在主线程中的。下面来看看核心代码:
_imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:_asset];
images = [[NSMutableArray alloc]initWithCapacity:10];
_imageGenerator.maximumSize = THUMBNAIL_SIZE;
CMTime duration = _asset.duration;
CMTimeValue intervalSeconds = duration.value / 3;
CMTime time = kCMTimeZero;
NSMutableArray *times = [NSMutableArray array];
for (NSUInteger i = 0; i < 3; i++) {
[times addObject:[NSValue valueWithCMTime:time]];
time = CMTimeAdd(time, CMTimeMake(intervalSeconds, duration.timescale));
}
[_imageGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, CGImageRefcgImage,
CMTime actualTime,
AVAssetImageGeneratorResult result,
NSError *error) {
if (cgImage) {
UIImage *image = [UIImage imageWithCGImage:cgImage];
[images addObject:image];
}
if (images.count == 3) {
dispatch_async(dispatch_get_main_queue(), ^{
self.imageview1.image = [images objectAtIndex:0];
self.imageview2.image = [images objectAtIndex:1];
self.imageview3.image = [images objectAtIndex:2];
});
}
}];
分解之后的帧效果如下: 图片合成视频效果如下:
(2)图片合成视频
图片合成视频的方法相对来说更加复杂一点,我们主要用到的class是这个:
AVAssetWriterInputPixelBufferAdaptor。不同之处在于这里我们还要设置图片合成视频的各种参数,比如帧率,编码方式等等。
2.1 设置文件封装类型
AVFileTypeQuickTimeMovie
2.2 设置图片格式
kCVPixelFormatType_32ARGB
2.3 设置编码方式、图片尺寸
NSDictionary *videoSettings = @{AVVideoCodecKey : AVVideoCodecH264,
AVVideoWidthKey : [NSNumber numberWithInt:(int)width],
AVVideoHeightKey : [NSNumber numberWithInt:(int)height]};
2.4 图片合成开始
CMTime lastTime = CMTimeMake(i, self.frameTime.timescale);
CMTime presentTime = CMTimeAdd(lastTime, self.frameTime);
[self.bufferAdapter appendPixelBuffer:sampleBuffer withPresentationTime:presentTime];
(1)视频分解图片
这里视频分解图片使用的是AVAssetImageGenerator,利用这个class可以很方便的实现不同时间戳下,视频帧的抓取。注意一般这种视频分解图片帧的方法都是放在子线程中的,而UI更新操作都是放在主线程中的。下面来看看核心代码:
_imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:_asset];
images = [[NSMutableArray alloc]initWithCapacity:10];
_imageGenerator.maximumSize = THUMBNAIL_SIZE;
CMTime duration = _asset.duration;
CMTimeValue intervalSeconds = duration.value / 3;
CMTime time = kCMTimeZero;
NSMutableArray *times = [NSMutableArray array];
for (NSUInteger i = 0; i < 3; i++) {
[times addObject:[NSValue valueWithCMTime:time]];
time = CMTimeAdd(time, CMTimeMake(intervalSeconds, duration.timescale));
}
[_imageGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, CGImageRefcgImage,
CMTime actualTime,
AVAssetImageGeneratorResult result,
NSError *error) {
if (cgImage) {
UIImage *image = [UIImage imageWithCGImage:cgImage];
[images addObject:image];
}
if (images.count == 3) {
dispatch_async(dispatch_get_main_queue(), ^{
self.imageview1.image = [images objectAtIndex:0];
self.imageview2.image = [images objectAtIndex:1];
self.imageview3.image = [images objectAtIndex:2];
});
}
}];
分解之后的帧效果如下: 图片合成视频效果如下:
(2)图片合成视频
图片合成视频的方法相对来说更加复杂一点,我们主要用到的class是这个:
AVAssetWriterInputPixelBufferAdaptor。不同之处在于这里我们还要设置图片合成视频的各种参数,比如帧率,编码方式等等。
2.1 设置文件封装类型
AVFileTypeQuickTimeMovie
2.2 设置图片格式
kCVPixelFormatType_32ARGB
2.3 设置编码方式、图片尺寸
NSDictionary *videoSettings = @{AVVideoCodecKey : AVVideoCodecH264,
AVVideoWidthKey : [NSNumber numberWithInt:(int)width],
AVVideoHeightKey : [NSNumber numberWithInt:(int)height]};
2.4 图片合成开始
CMTime lastTime = CMTimeMake(i, self.frameTime.timescale);
CMTime presentTime = CMTimeAdd(lastTime, self.frameTime);
[self.bufferAdapter appendPixelBuffer:sampleBuffer withPresentationTime:presentTime];
相关文章推荐
- iOS开发 ☞ const和宏的选择
- nagios
- iOS多线程技术-上
- 精通iOS开发--第19章 Core Location 和 Map Kit 01 Capabilities 下 示例程序
- 精通iOS开发--第19章 Core Location 和 Map Kit 上
- iOS开发主流的数据解析框架JSONModel
- 不同BIOS下的U盘启动设置方法
- iOS Scrollview 的头部view的拉伸伸缩效果
- iOS-解压zip
- iOS-高德地图
- 敲一下enter键,完成iOS的打包工作
- 打造轻量级 tableViewController 之抽离 DataSource/Delegate
- IOS常用代码总结 - 第三方库部分
- iOS开发经验总结
- (翻译)开始iOS 7中自动布局教程(二)
- 开始iOS 7中自动布局教程(一)
- iOS architecture and framework(Stanford class 1)
- iOS 浅拷贝和深拷贝的区别? copy和mutableCopy的区别?
- 看完就彻底懂了session和cookie
- iOS 隐藏tabbar代码详解