IOS 视频分解图片、图片合成视频
在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];
IOS 视频分解图片、图片合成视频的更多相关文章
- opencv 将视频分解成图片和使用本地图片合成视频
代码如下: // cvTest.cpp : Defines the entry point for the console application. #include "stdafx.h&q ...
- opencv学习之路(3)、批量读取图片、视频分解、视频合成
一.批量有序读取图片 #include<opencv2/opencv.hpp> using namespace cv; void main() { //批量读取图片(有序) ]; ]; M ...
- 在Ubuntu14.04下安装 ffmpeg-2.4.13(处理视频用,将视频保存为图片序列)
首先在 http://www.ffmpeg.org/olddownload.html 下载 ffmpeg-2.4.13.tar.bz2 : 然后安装 yasm 和 libx264: apt-get i ...
- python视频与帧图片的相互转化,以及查看视频分辨率
1.拆分视频为帧图片 import cv2 def video2frame(videos_path,frames_save_path,time_interval): vidcap = cv2.Vide ...
- iOS 解决LaunchScreen中图片加载黑屏问题
iOS 解决LaunchScreen中图片加载黑屏问题 原文: http://blog.csdn.net/chengkaizone/article/details/50478045 iOS 解决Lau ...
- iOS根据Url 获取图片尺寸
iOS根据Url 获取图片尺寸 // 根据图片url获取图片尺寸 +(CGSize)getImageSizeWithURL:(id)imageURL { NSURL* URL = nil; if([i ...
- IOS第六天(3:scrollView 图片轮播器)
IOS第六天(3:scrollView 图片轮播器) #import "HMViewController.h" #define kImageCount 5 @interface H ...
- iOS网络加载图片缓存策略之ASIDownloadCache缓存优化
iOS网络加载图片缓存策略之ASIDownloadCache缓存优化 在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...
- ios里面如何压缩图片
在iOS里面,压缩图片跟在其他环境里面差不多,都和累死, 就是对当前图片从新画图,制定一个尺寸的问题 UIImage* image = [UIImage imageNamed:@"cat.j ...
随机推荐
- gcc 创建库及使用
gcc -shared hello.c -o libhello.so gcc -o test test.c -L库所在目录 -l库名
- 89C51单片机实现的流水灯
/*******************************************Copyright: 2014.02.09.version1.0File name: led.cDescrip ...
- ASP.NET Web API 入门大杂烩
[前言] 本文是大杂烩,意思即是:到处Copy再加一点点思考而混在一起的文章,引用来源因为太多太杂故而省略,望原作者原谅. [概述] ASP.NET Web API随ASP.NET MVC 4一起发行 ...
- DataTemplate和ControlTemplate联系与区别
---恢复内容开始--- 正如标题中的两个拼接的单词所说,DataTemplate就是数据显示的模板,而ControlTemplate是控件自身的模板.(个人理解,错误请指出,谢谢) 我们看这二者在两 ...
- LeetCode-Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- C++四则运算出题器---有答案版
一.实验题目 四则运算扩展----能接受答案并判断对错然后给出成绩. 二.实验思路 在每次输出算式后面输入答案,然后判断对错,对则统计. 稍微优化了一下界面. 三.代码 // 12345.cpp : ...
- Object常用方法
1.clone() 创建并返回对象的一个副本,要进行“克隆”的对象所属的类必须实现java.lang.Cloneable接口 2.equals() 功能:比较引用数据类型的等价性 等价标准:引用类型比 ...
- Node.js 学习(三) NPM 使用介绍
NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...
- websphere OSGi应用环境下服务调用saaj包加载问题分析报告
websphere OSGi应用环境下服务调用saaj包加载问题分析报告 作者:bingjava 版权声明:本文为博主原创文章,转载请说明出处:http://www.cnblogs.com/bingj ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树
题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...