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(列).可以使 ...
随机推荐
- 搭建Gitlab
这个周末好奇想搭建一个私有的Gitlab,以前折腾过一次.现在记录如下. 一.安装Ubuntu 16.04 LTS server amd64 到Virtual Box 二.安装gitlab服务器 按照 ...
- transient关键字小结
java中实现序列化有两种实现方式,一种是自动的,只要实现Serilizable接口,另一种是需要手动指定需要序列化的成员变量,实现Externalizable接口. transient的特点: 1. ...
- F4IF_FIELD_VALUE_REQUEST 和 F4IF_INT_TABLE_VALUE_REQUEST的不同
F4IF_FIELD_VALUE_REQUEST 和 F4IF_INT_TABLE_VALUE_REQUEST的不同 F4IF_FIELD_VALUE_REQUEST主要功能是将表里的字段对应的sea ...
- 没事抽空学——c语言指针操作基础概念
指针基础 理解指针的最佳方法是画图,学习使用基本指针,不要产生空指针. 存储控件分配 存储控件分配是指在内存预留空间的过程.就像一个虚拟菜谱一样,指针对应菜名,其所指的内存空间中的数据对应实际的菜. ...
- Python基础篇-day6
本节简介: 1.模块1.1 时间模块1.2 random模块1.3 shutil模块1.4 shelve模块1.5 XML模块1.6 ConfigParser模块1.7 hashlib模块1.8 lo ...
- servlet的url-pattern匹配规则
首先需要明确几容易混淆的规则: servlet容器中的匹配规则既不是简单的通配,也不是正则表达式,而是特定的规则.所以不要用通配符或者正则表达式的匹配规则来看待servlet的url-pattern. ...
- Webpack学习笔记(二)
市面上已经存在的模块管理和打包工具并不适合大型的项目,尤其单页面 Web 应用程序.最紧迫的原因是如何在一个大规模的代码库中,维护各种模块资源的分割和存放,维护它们之间的依赖关系,并且无缝的将它们整合 ...
- vs2015下载及预览与发布
VS2015 RC发布下载,通用Windows平台必备神器! 几个月前微软发布了Visual Studio 2015的技术预览版本,之后又先后发布了6个更新版本.如今,微软已在其官方页面中公布了最新开 ...
- English Vocabulary
Creative - producing or using original and unusual ideas Computer literate - familiarity with comput ...
- 我的linux云服务器配置记录
配置vps的时候,随手记录一下~~ 添加一些源: vi /etc/apt/sources.list deb http://mirrors.aliyun.com/ubuntu/ xenial main ...