//
// 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下的视频分帧处理的更多相关文章

  1. HTTP2.0的二进制分帧

    1.帧的类型: 在二进制分帧的结构中,头部有8个字节(64Bit),其中有一个字节(8Bit)来标志帧的类型: HTTP2.0规定了如下帧类型: DATA: 用于传输HTTP消息体 HEADERS:用 ...

  2. PHP分帧后台模板页面css样式,js引入方法

    一,首先把下载好的分帧后台模板放到对应的目录中,HTML显示页面放到View目录中,CSS和JS.img图片等公共资源放到include目录中 二.然后开始准备更改CSS和JS , img路径操作:( ...

  3. C#视频取帧图

    由于项目里页面有许多视频资料需要展示给用户查看,因此需要做一个视频列表,原设计是列表显示视频第一帧图,但实际上很多视频第一帧是纯黑底色. 于是想到用js利用canvas截图,最后发现由于浏览器跨域限制 ...

  4. Java截取视频首帧并旋转正向

    package test; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Image; import j ...

  5. Android之使用MediaMetadataRetriever类获取视频第一帧

    一.首先,来介绍一下MediaMetadataRetriever类,此类位于android.media包下,这里,先附上可查看此类的API地址:MediaMetadataRetriever类.大家能够 ...

  6. 在使用TCP协议进行消息发送时,对消息分帧

    成帧与解析 阅读 <java TCP/IP Socket 编程>第三章笔记 成帧技术(frame)是解决如何在接收端定位消息的首尾位置的问题.在进行数据收发时,必须指定消息接收者如何确定何 ...

  7. python视频与帧图片的相互转化,以及查看视频分辨率

    1.拆分视频为帧图片 import cv2 def video2frame(videos_path,frames_save_path,time_interval): vidcap = cv2.Vide ...

  8. Android 视频播放器切换到下个视频时残留上个视频画面的解决办法

    最近在做一个Android视频播放器,遇到一个问题:切换到下一个视频时,中间会停留上一个视频的残存画面.   这是怎么回事?   我在网上找了很多资料,终于找到了原因:我是用自定义一个surfacev ...

  9. HTML的窗口分帧

    下面通过一个后台管理的部分设计来说明窗口分帧 frameset.html代码 <!-- <frameset>标签(常用来做后台管理界面) 属性:rows(行).cols(列).可以使 ...

随机推荐

  1. PAT乙级1004. 成绩排名 (20)

    读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 学号 成绩 第3行:第2个学生 ...

  2. cp执行命令,如何直接覆盖不提示

    参数说明 -i, --interactive prompt before overwrite (overrides a previous -n option)   #文件存在是,交互式提示是否覆盖 - ...

  3. Android Studio新手

    目标:Android Studio新手–>下载安装配置–>零基础入门–>基本使用–>调试技能–>构建项目基础–>使用AS应对常规应用开发 AS简介 经过2年时间的研 ...

  4. Redis性能问题排查解决手册

    转自:http://www.cnblogs.com/mushroom/p/4738170.html 阅读目录: 性能相关的数据指标 内存使用率used_memory 命令处理总数total_comma ...

  5. PhotoSwipe简介

    官方网站http://www.photoswipe.com/源码示例http://github.com/downloads/codecomputerlove/PhotoSwipe/code.photo ...

  6. Pthon修炼5

    函数    一.自定义函数 def:表示函数的关键字 函数名:函数的名称 函数体:函数中进行一系列的逻辑计算 参数:为函数体提供数据 返回值:执行完函数return 一个值 例:def count(x ...

  7. mysql索引类型-形式-使用时机-不足之处--注意事项

    一.索引的类型 1.普通索引   增加 create  index  index_name on table(colume(length));                       例子:cre ...

  8. C# lesson1

    一.C#和.net平台 .net是一个软件,一个平台(一般在windows自带 或者在vs里面已经有了) C#是一门运行在.net平台上的语言,需要编译:C#经过.net framework里面的编译 ...

  9. Base64编码 图片与base64编码互转

    package com.education.util; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import jav ...

  10. input输入框只允许输入数字/ 数字+小数点/ 文字+字母/ 等解决方法

    1.只允许输入数字: <input type="text" onkeyup="this.value=this.value.replace(/[^0-9]/g,'') ...