使用AVFoundation实现视频录制详解

 更新时间:2022年09月05日 08:44:44   作者:reyzhang  
这篇文章主要介绍了使用AVFoundation实现视频录制详解的相关资料,需要的朋友可以参考下

一、前言

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实现视频录制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • ajax 三种实现方法实例代码

    ajax 三种实现方法实例代码

    这篇文章主要介绍了ajax 三种实现方法实例代码的相关资料,需要的朋友可以参考下
    2016-09-09
  • iOS实现去除html标签的方法汇总

    iOS实现去除html标签的方法汇总

    相信大家在做网站的时候,经常会遇到去除html标签的问题,下面这篇文章主要给大家总结介绍了关于iOS如何实现去除html标签的一些方法,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-10-10
  • iOS9开放的新API--Spotlight使用指南

    iOS9开放的新API--Spotlight使用指南

    作为苹果iOS9的重要特性之一,Spotlight搜索如今重新回到主界面最左侧(同样支持主界面下滑呼出),通过API的支持,还带来了全新的Universal Search通用搜索功能,除了网络以及系统本身内容之外,还能直接搜索第三方应用内的相关内容。下面我们就来详细研究下Spotlight
    2015-11-11
  • ios 不支持 iframe 的完美解决方法(兼容iOS&安卓)

    ios 不支持 iframe 的完美解决方法(兼容iOS&安卓)

    下面小编就为大家带来一篇ios 不支持 iframe 的完美解决方法(兼容iOS&安卓)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • iOS中的线程死锁实例详解

    iOS中的线程死锁实例详解

    这篇文章主要给大家介绍了关于iOS中线程死锁的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-09-09
  • iOS中指纹识别常见问题汇总

    iOS中指纹识别常见问题汇总

    最近在公司做了一个app要使用指纹支付的功能,在实现过程中遇到各种坑,今天小编抽抗给大家总结把遇到问题汇总特此分享到脚本之家平台,需要的朋友参考下
    2016-12-12
  • iOS中封装.framework及使用的方法详解

    iOS中封装.framework及使用的方法详解

    这篇文章主要给大家介绍了关于iOS中封装.framework及使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧。
    2018-04-04
  • Xcode 10升级导致项目报错的常见问题解决

    Xcode 10升级导致项目报错的常见问题解决

    这篇文章主要给大家介绍了关于Xcode 10升级导致项目报错的常见问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-12-12
  • IOS点击按钮隐藏状态栏详解及实例代码

    IOS点击按钮隐藏状态栏详解及实例代码

    这篇文章主要介绍了IOS点击按钮隐藏状态栏详解及实例代码的相关资料,需要的朋友可以参考下
    2017-02-02
  • IOS提醒用户重新授权打开定位功能

    IOS提醒用户重新授权打开定位功能

    这篇文章主要介绍了IOS提醒用户重新授权打开定位功能的相关资料,需要的朋友可以参考下
    2015-12-12

最新评论