iOS设备音视频采集

一、采集设备

1.iphone/ipad  摄像头

2.屏幕采集


二、视频采集方案

1.使用苹果提供AVFoundation框架进行采集

采集步骤:

1.创建捕获会话,必须要强引用,否则会被释放 (采集数据需要新建一个采集会话,且该会话必须强引用,否则会被释放)

    AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
    _captureSession = captureSession;

2.寻找相关所需设备

    AVCaptureDevice *videoDevice = [self getVideoDevice:AVCaptureDevicePositionFront]

   AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];

3.基于输入设备生成输入对象,并添加到会话

    AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];

    AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];    

    if ([captureSession canAddInput:videoDeviceInput]){

        [captureSession addInput:videoDeviceInput];

    }

    if ([captureSession canAddInput:audioDeviceInput]) {

        [captureSession addInput:audioDeviceInput];

    }

4.创建输出对象并添加到会话

    AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init];

    // 注意:队列必须是串行队列,才能获取到数据,而且不能为空,原因参考接口说明

    dispatch_queue_t videoQueue = dispatch_queue_create("Video Capture Queue", DISPATCH_QUEUE_SERIAL);

  // 设置代理,捕获视频样品数据

    [videoOutput setSampleBufferDelegate:self queue:videoQueue];

    if ([captureSession canAddOutput:videoOutput]) {

        [captureSession addOutput:videoOutput];

    }

dispatch_queue_t audioQueue = dispatch_queue_create("Audio Capture Queue", DISPATCH_QUEUE_SERIAL);

// 设置代理,捕获音频样品数据

    [audioOutput setSampleBufferDelegate:self queue:audioQueue];

    if ([captureSession canAddOutput:audioOutput]) {

        [captureSession addOutput:audioOutput];

    }

    AVCaptureAudioDataOutput *audioOutput = [[AVCaptureAudioDataOutput alloc] init];

    dispatch_queue_t audioQueue = dispatch_queue_create("Audio Capture Queue", DISPATCH_QUEUE_SERIAL);

    [audioOutput setSampleBufferDelegate:self queue:audioQueue];

    if ([captureSession canAddOutput:audioOutput]) {

        [captureSession addOutput:audioOutput];

    }

5.启动会话
    [captureSession startRunning];


6.获取输入设备数据,有可能是音频有可能是视频

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{

//数据保存于sampleBuffer中

    if (_videoConnection == connection) {

        NSLog(@"采集到视频数据");

    } else {

        NSLog(@"采集到音频数据");

    }

}

demo链接 参考文章https://www.jianshu.com/p/c71bfda055fa

2.使用第三方GPUImage 进行视频采集。(直播中会使用,采集的图像可以添加滤镜,后续附上)


你可能感兴趣的:(iOS设备音视频采集)