<图形图像,动画,多媒体> 读书笔记 --- 录制与编辑视频
使用UIImagePickerController 进行录制
- #import "ViewController.h"
- #import <MobileCoreServices/MobileCoreServices.h>
- #import <QuartzCore/QuartzCore.h>
- @interface ViewController ()
- <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
- - (IBAction)videoRecod:(id)sender;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (IBAction)videoRecod:(id)sender {
- if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
- UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
- imagePickerController.delegate = self;
- imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
- imagePickerController.mediaTypes = [[NSArray alloc]
- initWithObjects:(NSString *)kUTTypeMovie, nil];
- //录制质量设定
- imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
- //仅仅同意最多录制30秒时间
- imagePickerController.videoMaximumDuration = 30.0f;
- [self presentViewController:imagePickerController animated:YES completion:nil];
- } else {
- NSLog(@"摄像头不可用。
- ");
- }
- }
- - (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- - (void) imagePickerController: (UIImagePickerController *) picker
- didFinishPickingMediaWithInfo: (NSDictionary *) info {
- NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
- NSString *tempFilePath = [url path];
- if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(tempFilePath) ) {
- //video:didFinishSavingWithError:contextInfo: 必须保存成这类回调
- UISaveVideoAtPathToSavedPhotosAlbum( tempFilePath,
- self,
- @selector(video:didFinishSavingWithError:contextInfo:),
- (__bridge void *)(tempFilePath));
- }
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(NSString *)contextInfo {
- NSString *title; NSString *message;
- if (!error) {
- title = @"视频保存";
- message = @"视频已经保存到设备的相机胶卷中";
- } else {
- title = @"视频失败";
- message = [error description];
- }
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
- message:message
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alert show];
- }
- - (void)navigationController:(UINavigationController *)navigationController
- willShowViewController:(UIViewController *)viewController
- animated:(BOOL)animated
- {
- NSLog(@"选择器将要显示。");
- }
- - (void)navigationController:(UINavigationController *)navigationController
- didShowViewController:(UIViewController *)viewController
- animated:(BOOL)animated
- {
- NSLog(@"选择器显示结束。
- ");
- }
使用AVFoundation
AVCaptureSession,捕获会话,是为了实现从摄像头和麦克风捕获数据,须要使用AVCaptureSession对象协调输入输出数据
AVCaptureDevice,捕获设备,代表输入一个设备,比如摄像头和麦克风
AVCaptureDeviceInput,捕获会话的一个输入数据源
AVCaptureOutput,捕获会话的一个输出目标,比如输出的视频文件和静态图片
AVCaptureMovieFileOutput,是AVCaptureOutput的子类,通过它能够将捕获的数据输出到QuickTime视频文件里(MOV)
AVCaptureVideoPreviewLayer,是CALayer子类,能够使用它来显示录制的视频
AVCaptureConnection,捕获连接,在一个捕获会话中输入和输出之间的连接
- #import "ViewController.h"
- #import <AVFoundation/AVFoundation.h>
- #import <AssetsLibrary/AssetsLibrary.h>
- @interface ViewController ()
- <AVCaptureFileOutputRecordingDelegate>
- {
- BOOL isRecording;
- }
- @property (weak, nonatomic) IBOutlet UILabel *label;
- @property (weak, nonatomic) IBOutlet UIButton *button;
- @property (strong, nonatomic) AVCaptureSession *session;
- @property (strong, nonatomic) AVCaptureMovieFileOutput *output;
- - (IBAction)recordPressed:(id)sender;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.session = [[AVCaptureSession alloc] init];
- self.session.sessionPreset = AVCaptureSessionPresetMedium;
- AVCaptureDevice *cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
- NSError *error = nil;
- AVCaptureDeviceInput *camera = [AVCaptureDeviceInput deviceInputWithDevice:cameraDevice error:&error];
- AVCaptureDevice *micDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
- AVCaptureDeviceInput *mic = [AVCaptureDeviceInput deviceInputWithDevice:micDevice error:&error];
- if (error || !camera || !mic) {
- NSLog(@"Input Error");
- } else {
- //增加捕获音频视频
- [self.session addInput:camera];
- [self.session addInput:mic];
- }
- self.output = [[AVCaptureMovieFileOutput alloc] init];
- if ([self.session canAddOutput:self.output]) {
- [self.session addOutput:self.output];//输出
- }
- AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
- previewLayer.frame = CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height);
- [self.view.layer insertSublayer:previewLayer atIndex:0];
- [self.session startRunning];
- isRecording = NO;
- self.label.text = @"";
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- if (![self.session isRunning])
- {
- [self.session startRunning];
- }
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- if ([self.session isRunning])
- {
- [self.session stopRunning];
- }
- }
- - (IBAction)recordPressed:(id)sender {
- if (!isRecording)
- {
- [self.button setTitle:@"停止" forState:UIControlStateNormal];
- self.label.text = @"录制中...";
- isRecording = YES;
- NSURL *fileURL = [self fileURL];
- [self.output startRecordingToOutputFileURL:fileURL recordingDelegate:self];
- }
- else
- {
- [self.button setTitle:@"录制" forState:UIControlStateNormal];
- self.label.text = @"停止";
- [self.output stopRecording];
- isRecording = NO;
- }
- }
- - (NSURL *) fileURL
- {
- NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"movie.mov"];
- NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
- NSFileManager *manager = [[NSFileManager alloc] init];
- if ([manager fileExistsAtPath:outputPath])
- {
- [manager removeItemAtPath:outputPath error:nil];
- }
- return outputURL;
- }
- #pragma mark-- AVCaptureFileOutputRecordingDelegate托付协议实现方法
- - (void)captureOutput:(AVCaptureFileOutput *)captureOutput
- didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
- fromConnections:(NSArray *)connections error:(NSError *)error
- {
- if (error == nil) {
- ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
- [library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
- completionBlock:^(NSURL *assetURL, NSError *error)
- {
- if (error)
- {
- NSLog(@"写入错误。") ;
- }
- }];
- }
- }
使用UIVideoEditorController
- #import "ViewController.h"
- @interface ViewController ()
- <UIVideoEditorControllerDelegate,UINavigationControllerDelegate>
- - (IBAction)editButtonPress:(id)sender;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (IBAction)editButtonPress:(id)sender {
- NSBundle *bundle = [NSBundle mainBundle];
- NSString *moviePath = [bundle pathForResource:@"YY"
- ofType:@"mp4"];
- //推断设备是否支持编辑视频
- if ([UIVideoEditorController canEditVideoAtPath:moviePath]){
- UIVideoEditorController *videoEditor =
- [[UIVideoEditorController alloc] init];
- videoEditor.delegate = self;
- videoEditor.videoPath = moviePath;
- [self presentViewController:videoEditor animated:YES completion:NULL];
- } else {
- NSLog(@"不能编辑这个视频");
- }
- }
- - (void)videoEditorController:(UIVideoEditorController *)editor
- didSaveEditedVideoToPath:(NSString *)editedVideoPath{
- [editor dismissViewControllerAnimated:YES completion:NULL];
- if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(editedVideoPath) ) {
- UISaveVideoAtPathToSavedPhotosAlbum(editedVideoPath, self,
- @selector(video:didFinishSavingWithError:contextInfo:),
- (__bridge void *)(editedVideoPath));
- }
- }
- - (void)videoEditorController:(UIVideoEditorController *)editor
- didFailWithError:(NSError *)error{
- NSLog(@"编辑视频出错");
- NSLog(@"Video editor error occurred = %@", error);
- [editor dismissViewControllerAnimated:YES completion:NULL];
- }
- - (void)videoEditorControllerDidCancel:(UIVideoEditorController *)editor{
- NSLog(@"视频编辑取消");
- [editor dismissViewControllerAnimated:YES completion:NULL];
- }
- - (void)video:(NSString *)videoPath
- didFinishSavingWithError:(NSError *)error
- contextInfo:(NSString *)contextInfo {
- NSString *title; NSString *message;
- if (!error) {
- title = @"视频保存";
- message = @"视频已经保存到设备的相机胶卷中";
- } else {
- title = @"视频失败";
- message = [error description];
- }
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
- message:message
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alert show];
- }
个人感言,事实上视频我之前做过,也有类似文章,在国外的一些站点上全然也能找到比这更好的更有技术含量的代码和文章,只是总之,算是一个学习记录吧~
原书:http://item.jd.com/11522516.html
<图形图像,动画,多媒体> 读书笔记 --- 录制与编辑视频的更多相关文章
- <图形图像,动画,多媒体> 读书笔记 --- AirPlay
AirPlay技术是之前一直没有接触过的技术,正好这次做一个笔记 共用: 1.能够通过AirPlay将iOS和MAC设备上的视频或音频输出到高清电视上或高保真音响 2.能够通过AirPlay将iOS和 ...
- <图形图像,动画,多媒体> 读书笔记 --- 力学行为特性
UIKit力学行为包括了:重力(UIGravityBehavior),碰撞(UICollisionBehavior),吸附(UIAttachmentBehavior),推(UIPushBehavior ...
- <图形图像,动画,多媒体> 读书笔记 --- 音效
音频多媒体文件主要是存放音频数据信息,音频文件在录制的过程中把声音信号,通过音频编码,变成音频数字信号保存到某种格式文件里.在播放过程中在对音频文件解码,解码出的信号通过扬声器等设备就能够转成音波.音 ...
- iOS 图形图像动画 Core Animation
//Core Animation #define WeakSelf __weak __typeof(self) weakSelf = self #define StrongSelf __strong ...
- 《Java并发编程实战》第九章 图形用户界面应用程序界面 读书笔记
一.为什么GUI是单线程化 传统的GUI应用程序通常都是单线程的. 1. 在代码的各个位置都须要调用poll方法来获得输入事件(这样的方式将给代码带来极大的混乱) 2. 通过一个"主事件循环 ...
- 【python下使用OpenCV实现计算机视觉读书笔记3】读写视频文件
代码例如以下: import cv2 videoCapture = cv2.VideoCapture('car.avi') fps = videoCapture.get(cv2.cv.CV_CAP_P ...
- 关东升的《iOS实战:图形图像、动画和多媒体卷(Swift版)》上市了
关东升的<iOS实战:图形图像.动画和多媒体卷(Swift版)>上市了 承蒙广大读者的厚爱我的<iOS实战:图形图像.动画和多媒体卷(Swift版)>京东上市了,欢迎广大读者提 ...
- WPF,Silverlight与XAML读书笔记第三十九 - 可视化效果之3D图形
原文:WPF,Silverlight与XAML读书笔记第三十九 - 可视化效果之3D图形 说明:本系列基本上是<WPF揭秘>的读书笔记.在结构安排与文章内容上参照<WPF揭秘> ...
- WPF,Silverlight与XAML读书笔记第四十三 - 多媒体支持之文本与文档
说明:本系列基本上是<WPF揭秘>的读书笔记.在结构安排与文章内容上参照<WPF揭秘>的编排,对内容进行了总结并加入一些个人理解. Glyphs对象(WPF,Silverlig ...
随机推荐
- Python27天 反射 ,isinstance与ssubclass 内置方法
所学内容 反射 1.hasattr ( 判断一个属性在对象里有没有 ) -------------------- [对象,字符串属性]本质是:# 判断 ' name ' in obj.__dict__ ...
- tp3.2 复合查询or
tp3.2 复合查询or $where['goods_name'] = array("like","%$q%");$where['goods_sn'] = ar ...
- 10.Flask-上下文
1.1.local线程隔离对象 不用local对象的情况 from threading import Thread request = ' class MyThread(Thread): def ru ...
- Spark2.2,IDEA,Maven开发环境搭建附测试
前言: 停滞了一段时间,现在要沉下心来学习点东西,出点货了. 本文没有JavaJDK ScalaSDK和 IDEA的安装过程,网络上会有很多文章介绍这个内容,因此这里就不再赘述. 一.在IDEA上安装 ...
- DeltaFish 校园物资共享平台 第四次小组会议
一.上周记录汇报 齐天扬 学习慕课HTML至14章.构建之法10-14章 李 鑫 学习制作简易的JSP页面和servlet,看完关于HTML的慕课 陈志锴 学习编制简易JSP页面和servlet, ...
- OpenCL C
OpenCL C OpenCL 简介 opencl C是ISO C99的一个扩展,主要区别如下: 去除了C99的一些特性,如:标准C99头文件,函数指针,递归,变长数组,和位域 增加了一些特性用于并 ...
- 编码的来历和使用 utf-8 和GB2312比较
经常我们打开外国网站的时候出现乱码,又或者打开很多非英语的外国网站的时候,显示的都是口口口口口的字符, wordpress程序是用的UTF-8,很多cms用的是GB2312. ● 为什么有这么多编码? ...
- RRDtool入门详解
---------------原创内容,转载请注明出处.<yaoyao0777@Gmail.com>------------ 一.概述 RRDtool(round-robin databa ...
- Metric Learning度量学习:**矩阵学习和图学习
DML学习原文链接:http://blog.csdn.net/lzt1983/article/details/7884553 一篇metric learning(DML)的综述文章,对DML的意义.方 ...
- 读白帽子web安全笔记
点击劫持 frame buseting if (top.location != location) { top.location = self.location } html5的sandbox属性 ...