视频采集
AVFoundation 完成视频采集,做个笔记吧.
初始化页面布局
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = @"音视频采集";
[self.view addSubview:titleLabel];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).offset(30);
make.centerX.equalTo(self.view);
}];
UIButton *beginInputVideo = [UIButton buttonWithType:UIButtonTypeCustom];
[beginInputVideo setTitle:@"开始视频采集" forState:UIControlStateNormal];
[beginInputVideo setTitle:@"停止视频采集" forState:UIControlStateSelected];
beginInputVideo.backgroundColor = [UIColor orangeColor];
[beginInputVideo addTarget:self action:@selector(beginInputVideoClcik:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:beginInputVideo];
[beginInputVideo mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(titleLabel.mas_bottom).offset(20);
make.centerX.equalTo(titleLabel);
make.width.equalTo(@150);
}];
self.captureVideoPreviewView = [[UIView alloc] init];
self.captureVideoPreviewView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.captureVideoPreviewView];
[self.captureVideoPreviewView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(beginInputVideo.mas_bottom).offset(20);
make.left.right.bottom.equalTo(self.view);
}];
懒加载需要用的对象
/** 图像sesson */
@property (nonatomic, strong) AVCaptureSession *captureSession;
/** 采集数据用的相机设备 */
@property (nonatomic, strong) AVCaptureDevice *inputCamera;
/** 输入 */
@property (nonatomic, strong) AVCaptureDeviceInput *captureDeviceInput;
/** 输出 */
@property (nonatomic, strong) AVCaptureVideoDataOutput *captureVideoDataOutput;
/** 相机采集页面的layer */
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;
/** 相机采集页面View */
@property (nonatomic, strong) UIView *captureVideoPreviewView;
下面是懒加载的代码
- (AVCaptureSession *)captureSession {
if (!_captureSession) {
_captureSession = [[AVCaptureSession alloc] init];
// 设置session显示分辨率, 用于设置output输出流的bitrate或者说画面质量
[_captureSession setSessionPreset:AVCaptureSessionPresetHigh];
/*
AVCaptureSessionPresetHigh 质量最高
AVCaptureSessionPresetPhoto Photo模式不能输出视频
AVCaptureSessionPresetMedium 中等质量
AVCaptureSessionPresetLow 最低质量
AVCaptureSessionPreset320x240 320x240大小(不支持IPhone)
AVCaptureSessionPreset352x288 (支持iPhone)
AVCaptureSessionPreset640x480(支持iPhone)等等...
*/
if ([_captureSession canAddInput:self.captureDeviceInput]) {
[_captureSession addInput:self.captureDeviceInput];
}
if ([_captureSession canAddOutput:self.captureVideoDataOutput]) {
[_captureSession addOutput:self.captureVideoDataOutput];
}
}
return _captureSession;
}
- (AVCaptureDevice *)inputCamera {
if (!_inputCamera) {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if ([device position] == AVCaptureDevicePositionBack) {
_inputCamera = device;
}
}
}
return _inputCamera;
}
- (AVCaptureDeviceInput *)captureDeviceInput {
if (!_captureDeviceInput) {
NSError *error;
_captureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:self.inputCamera error:&error];
NSLog(@"%@", error);
}
return _captureDeviceInput;
}
- (AVCaptureVideoDataOutput *)captureVideoDataOutput {
if (!_captureVideoDataOutput) {
_captureVideoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
_captureVideoDataOutput.alwaysDiscardsLateVideoFrames = YES;
[_captureVideoDataOutput setAlwaysDiscardsLateVideoFrames:NO];
[_captureVideoDataOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
[_captureVideoDataOutput setSampleBufferDelegate:self queue:encodeQueue];
AVCaptureConnection *captureConnection = [_captureVideoDataOutput connectionWithMediaType:AVMediaTypeVideo];
[captureConnection setVideoOrientation:AVCaptureVideoOrientationPortrait | AVCaptureVideoOrientationLandscapeRight | AVCaptureVideoOrientationLandscapeLeft];
}
return _captureVideoDataOutput;
}
- (AVCaptureVideoPreviewLayer *)captureVideoPreviewLayer {
if (!_captureVideoPreviewLayer) {
_captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
/*
AVLayerVideoGravityResizeAspect // 适合层范围内(完全显示,可能有黑边)
AVLayerVideoGravityResizeAspectFill // 填充层边界(平铺,不拉伸,但是会被裁剪,内容可能显示不全)
AVLayerVideoGravityResize // 拉伸填充层边界(拉伸填充视图)
*/
[_captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
}
return _captureVideoPreviewLayer;
}
开始视频采集
- (void)beginInputVideo {
self.captureVideoPreviewLayer.frame = self.captureVideoPreviewView.bounds;
self.captureVideoPreviewLayer.backgroundColor = [UIColor blackColor].CGColor;
[self.captureVideoPreviewView.layer addSublayer:self.captureVideoPreviewLayer];
[self.captureSession startRunning];
}
采集到视频数据的回调
/**
采集到数据的回调
@param output outPut对象
@param sampleBuffer 数据
@param connection 链接
*/
- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
NSLog(@"%@", sampleBuffer);
}
注: 如发现问题,请留言.谢谢.
网友评论