AVFoundation下的视频分帧处理
//
// ViewController.m
// VideoFrame
//
// Created by wiseman on 16/1/27.
// Copyright (c) 2016年 wiseman. All rights reserved.
// #import "ViewController.h"
#import <AVFoundation/AVFoundation.h> @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *myFlowLayout; @property(nonatomic,strong) NSMutableArray *Arr; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//注册cell
static NSString *ID = @"mycell";
[self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID]; self.myFlowLayout.itemSize = CGSizeMake((self.view.bounds.size.width-)/, (self.view.bounds.size.width-)/); [self getVideoFrame];
} #pragma mark - Array
-(NSMutableArray *)Arr{
if (!_Arr) {
_Arr = [NSMutableArray array];
}
return _Arr;
} #pragma mark - GetVideoFrame
-(void)getVideoFrame{
//1.本地mp4的地址
NSString *path = [[NSBundle mainBundle] pathForResource:@"ddd" ofType:@"mp4"];
//2.URL
NSURL *pathURL = [NSURL fileURLWithPath:path];
//3.Setting
//3.1初始化asset对象
AVURLAsset *videoAsset = [[AVURLAsset alloc]initWithURL:pathURL options:nil];
//3.2 获取总视频的长度 = 总帧数 / 每秒的帧数
long videoSumTime = videoAsset.duration.value / videoAsset.duration.timescale;
//3.3 根据人眼每秒24帧图像,计算出每秒24帧情况下的总帧数
long eyeSumValue = videoSumTime * ;
//总帧数 / 块数
long kuai = videoAsset.duration.value / eyeSumValue;
//4.创建AVAssetImageGenerator对象
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc]initWithAsset:videoAsset];
generator.maximumSize = self.view.frame.size;
generator.appliesPreferredTrackTransform = YES;
generator.requestedTimeToleranceBefore = kCMTimeZero;
generator.requestedTimeToleranceAfter = kCMTimeZero;
//5. 添加需要帧数的时间集合
NSMutableArray *arr1 = [NSMutableArray array];
for (int i = ; i < eyeSumValue; i ++) {
CMTime time = CMTimeMake(i * kuai, videoAsset.duration.timescale);
NSValue *value = [NSValue valueWithCMTime:time];
[arr1 addObject:value];
} static long count = ;
[generator generateCGImagesAsynchronouslyForTimes:arr1 completionHandler:^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
// NSString *requestedTimeString = (NSString *)
// CFBridgingRelease(CMTimeCopyDescription(NULL, requestedTime));
// NSString *actualTimeString = (NSString *)
// CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));
// NSLog(@"Requested: %@; actual %@", requestedTimeString, actualTimeString);
if (result == AVAssetImageGeneratorSucceeded) {
count++;
NSLog(@"%ld",count);
[self.Arr addObject:[UIImage imageWithCGImage:im]];
if (count == eyeSumValue) {
[self.myCollectionView reloadData];
}
} if (result == AVAssetImageGeneratorFailed) {
NSLog(@"Failed with error: %@", [error localizedDescription]);
} if (result == AVAssetImageGeneratorCancelled) {
NSLog(@"AVAssetImageGeneratorCancelled");
}
}]; } #pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.Arr.count;
} - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"mycell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
if (!cell) {
cell = [[UICollectionViewCell alloc]init];
} UIImageView *imgView = [[UIImageView alloc]initWithFrame:cell.bounds];
imgView.image = self.Arr[indexPath.row];
[cell.contentView addSubview:imgView]; return cell;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
AVFoundation下的视频分帧处理的更多相关文章
- HTTP2.0的二进制分帧
1.帧的类型: 在二进制分帧的结构中,头部有8个字节(64Bit),其中有一个字节(8Bit)来标志帧的类型: HTTP2.0规定了如下帧类型: DATA: 用于传输HTTP消息体 HEADERS:用 ...
- PHP分帧后台模板页面css样式,js引入方法
一,首先把下载好的分帧后台模板放到对应的目录中,HTML显示页面放到View目录中,CSS和JS.img图片等公共资源放到include目录中 二.然后开始准备更改CSS和JS , img路径操作:( ...
- C#视频取帧图
由于项目里页面有许多视频资料需要展示给用户查看,因此需要做一个视频列表,原设计是列表显示视频第一帧图,但实际上很多视频第一帧是纯黑底色. 于是想到用js利用canvas截图,最后发现由于浏览器跨域限制 ...
- Java截取视频首帧并旋转正向
package test; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Image; import j ...
- Android之使用MediaMetadataRetriever类获取视频第一帧
一.首先,来介绍一下MediaMetadataRetriever类,此类位于android.media包下,这里,先附上可查看此类的API地址:MediaMetadataRetriever类.大家能够 ...
- 在使用TCP协议进行消息发送时,对消息分帧
成帧与解析 阅读 <java TCP/IP Socket 编程>第三章笔记 成帧技术(frame)是解决如何在接收端定位消息的首尾位置的问题.在进行数据收发时,必须指定消息接收者如何确定何 ...
- python视频与帧图片的相互转化,以及查看视频分辨率
1.拆分视频为帧图片 import cv2 def video2frame(videos_path,frames_save_path,time_interval): vidcap = cv2.Vide ...
- Android 视频播放器切换到下个视频时残留上个视频画面的解决办法
最近在做一个Android视频播放器,遇到一个问题:切换到下一个视频时,中间会停留上一个视频的残存画面. 这是怎么回事? 我在网上找了很多资料,终于找到了原因:我是用自定义一个surfacev ...
- HTML的窗口分帧
下面通过一个后台管理的部分设计来说明窗口分帧 frameset.html代码 <!-- <frameset>标签(常用来做后台管理界面) 属性:rows(行).cols(列).可以使 ...
随机推荐
- 输入身份证号码自动读取生日与性别(delphi)
一.格式介绍: [15位号码格式] 1.第l一6位数为行政区划代码; 2.第7-12位数为出生日期代码; 3.第13---15位数为分配顺序代码; (1).行政区划代码,是指公民第一次申领居民身份证时 ...
- Java版经典兔子繁殖迭代问题——斐波那契(Fibonacci)数列
/** * 题目: * 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子. * 假如兔子都不死,问经过month个月后,兔子的总数为多少对? */ public ...
- wso2 CEP集成storm实验
1.三台机子:192.168.225.1(manager,nimbus), 192.168.225.2(worker,supervisor), 192.168.225.3(worker,supervi ...
- rsync 断点续传
# rsync -avzP file root@172.20.7.219:/root/tmp (我这里file指要传送的文件)
- 常用python处理try except异常的三种方式
如果你在写python程序时遇到异常后想进行如下处理的话,一般用try来处理异常,假设有下面的一段程序: try: 语句1 语句2 . . 语句N except ...
- webAppRootKey
web.xml中webAppRootKey ------------------------------------------------------------------------------ ...
- java 移位运算
移位运算 :将整数转化为二进制(以补码的形式),按位平移. << 左移 >> 右移 >>> 无符号右移 << 右移: 按位做平 ...
- caffe编译出现的新错误
1.include/caffe/blob.hpp:9:34: fatal error: caffe/proto/caffe.pb.h: No such file or directory #inclu ...
- iframe自适应高度计算,iframe自适应
计算页面的实际高度,iframe自适应会用到 IfrHeight: function (iframeId, callback) { var height; function calcPageHeigh ...
- Misha and Palindrome Degree
Misha and Palindrome Degree 题目链接:http://codeforces.com/problemset/problem/501/E 贪心 如果区间[L,R]满足条件,那么区 ...