使用AVFoundation实现视频录制详解
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
一、前言
AVCaptureSession 是 AVFoundation 的核心类,用于管理捕获对象 AVCaptureInput 的视频和音频的输入,协调捕获的输出 AVCaptureOutput。
AVCaptureOutput 的输出有两种方法:
- 一种是直接以 movieFileUrl 方式输出;
- 一种是以原始数据流 data 的方式输出
流程对比图如下:
下面详细讲解录制视频的方案:
二、AVCaptureSession + AVCaptureMovieFileOutput
1.创建AVCaptureSession
//导入 AVFoundation.framework? #import <AVFoundation/AVFoundation.h> //声明属性 @property (nonatomic, strong) AVCaptureSession *captureSession; //懒加载 AVCapturesession - (AVCaptureSession *)captureSession { ? ? if (!_captureSession) { ? ? ? ? _captureSession = [[AVCaptureSession alloc] init]; ? ? ? ?? ? ? ? ? //设置分辨率 ? ? ? ? if ([_captureSession canSetSessionPreset:AVCaptureSessionPresetHigh]) { ? ? ? ? ? ? [_captureSession setSessionPreset:AVCaptureSessionPresetHigh]; ? ? ? ? } ? ? } ? ? return _captureSession; }
注意:AVCaptureSession 的调用是会阻塞线程的,建议单独开辟子线程处理。2.设置音频、视频输入
//声明属性 @property (nonatomic, strong) AVCaptureDeviceInput *videoInput; @property (nonatomic, strong) AVCaptureDeviceInput *audioInput; //设置视频,音频输入源 - (void)setCaptureDeviceInput { ? ? //1. 视频输入源 ? ? //获取视频输入设备, 默认后置摄像头 ? ? AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; ? ?? ? ? NSError *error = nil; ? ? self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error]; ? ?? ? ? if ([self.captureSession canAddInput:self.videoInput]) { ? ? ? ? [self.captureSession addInput:self.videoInput]; ? ? } ? ?? ? ?? ? ? //2. 音频输入源 ? ? AVCaptureDevice *audioCaptureDevice = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] firstObject]; ? ? self.audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error]; ? ? if ([self.captureSession canAddInput:self.audioInput]) { ? ? ? ? [self.captureSession addInput:self.audioInput]; ? ? } ? ?? }
3.设置文件输出源
//声明属性 @property (nonatomic, strong) AVCaptureMovieFileOutput *movieFileOutput; @property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer; //设置文件输出源 - (void)setDeviceFileOutput { ? ?? ? ? //初始化文件输出对象 ? ? self.movieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; ? ?? ? ? //捕获会话中特定捕获输入对象和捕获输出对象之间的连接 ? ? AVCaptureConnection *captureConnection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo]; ? ?? ? ? //设置防抖 ? ? if ([captureConnection isVideoStabilizationSupported]) { ? ? ? ? captureConnection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto; ? ? } ? ?? ? ? //预览图层和视频方向保持一致 ? ? captureConnection.videoOrientation = [self.previewLayer connection].videoOrientation; ? ?? ? ? //添加文件输出源 ? ? if ([self.captureSession canAddOutput:self.movieFileOutput]) { ? ? ? ? [self.captureSession addOutput:self.movieFileOutput]; ? ? } ? ?? }
4.添加视频预览层
- (void)setVideoPreviewLayer { ? ? self.previewLayer.frame = [UIScreen mainScreen].bounds; ? ?? ? ? [self.superView.layer addSubLayer:self.previewLayer]; } - (AVCaptureVideoPreviewLayer *)previewLayer { ? ? if (!_previewLayer) { ? ? ? ? _previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession]; ? ? ? ? _previewLayer.masksToBounds = YES; ? ? ? ? _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;//填充模式 ? ? } ? ? return _previewLayer; }
5. 开始采集
//声明属性 @property (nonatomic, strong) dispatch_queue_t sessionQueue; //开始采集 - (void)startCapture { ?? ?self.sessionQueue = dispatch_queue_create("com.capturesession.queue", DISPATCH_QUEUE_CONCURRENT); ? ? if (![self.captureSession isRunning]) { ? ? ? ? __weak __typeof(self) weakSelf = self; ? ? ? ?? ? ? ? ? dispatch_async(self.sessionQueue, ^{ ? ? ? ? ? ? [weakSelf.captureSession startRunning]; ? ? ? ? }); ? ? ? ?? ? ? } }
6. 开始录制
//开始录制 - (void)startRecord { ? ?? ? ? [self.movieFileOutput startRecordingToOutputFileURL:[self createVideoPath] recordingDelegate:self]; }
当实际的录制开始或停止时,系统会有代理回调。当开始录制之后,这时可能还没有真正写入,真正开始写入会回调下面代理,停止录制也是如此,所以如果你需要对录制视频起始点操作,建议通过系统的回调代理:
//实现协议 <AVCaptureFileOutputRecordingDelegate>中的方法 #pragma mark _ AVCaptureFileOutputRecordingDelegate //起始点 - 开始录制 - (void)captureOutput:(AVCaptureFileOutput *)output didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray<AVCaptureConnection *> *)connections { ? ?? } //结束录制 -(void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error { ? ? NSLog(@"视频录制完成. 文件路径:%@",[outputFileURL absoluteString]); }
7.停止录制
//停止录制 - (void)stopRecord { ? ? if ([self.movieFileOutput isRecording]) { ? ? ? ? [self.movieFileOutput stopRecording]; ? ? } }
8.停止采集
//停止采集 - (void)stopCapture { ? ? if ([self.captureSession isRunning]) { ? ? ? ? __weak __typeof(self) weakSelf = self; ? ? ? ? dispatch_async(self.sessionQueue, ^{ ? ? ? ? ? ? [weakSelf.captureSession stopRunning]; ? ? ? ? ? ? weakSelf.captureSession = nil; ? ? ? ? }); ? ? } }
到此这篇关于使用AVFoundation实现视频录制详解的文章就介绍到这了,更多相关AVFoundation实现视频录制内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!
相关文章
简单谈谈c/c++中#import、#include和@class的区别
对于#import,我想做过iOS开发的人应该都不陌生。在开发过程中,当我们需要声明某一个类时,都需要去引用。而#imclude的话,在我们学习C时就已经知道了,他的作用也是引用声明的意思。在表面上他们的作用似乎都是一样的。但是在具体功能实现方式上,还是有着很大的区别。2018-01-01iOS使用自带的UIViewController实现qq加号下拉菜单的功能(实例代码)
这篇文章主要介绍了iOS使用自带的UIViewController实现qq加号下拉菜单的功能(实例代码),需要的朋友可以参考下2017-05-05
最新评论