前言
正文
實(shí)時(shí)的視頻流數(shù)據(jù)采集用AVFoundation框架的AVCaptureSession類
創(chuàng)建AVCaptureSession->設(shè)置輸入輸出對(duì)象-> 用AVCaptureConnection連接輸入輸出->在回調(diào)方法中返回視頻數(shù)據(jù)做處理
重要知識(shí)點(diǎn)
由于iphone設(shè)備的不同型號(hào)的前置、后置攝像頭所支持的最高分辨率各不相同,所以在設(shè)置時(shí),一定進(jìn)行判斷,不然會(huì)采集不出數(shù)據(jù).
采集拴疤、設(shè)置
//初始化AVCaptureSession
self.videoSession =[[AVCaptureSession alloc]init];
//開始配置
[self.videoSession beginConfiguration];
//設(shè)置分辨率
[self set_capture_present];
//獲取視頻設(shè)備對(duì)象
self.videoDevice = [self cameraWithPosition:position];
//初始化視頻捕獲輸入對(duì)象
self.VideoInput = [[AVCaptureDeviceInput alloc]initWithDevice:self.videoDevice error:nil];
//初始化視頻捕獲輸出對(duì)象
self.VideoOutput = [[AVCaptureVideoDataOutput alloc]init];
// 是否卡頓時(shí)丟幀
[self.VideoOutput setAlwaysDiscardsLateVideoFrames:YES];
// 設(shè)置像素格式
self.VideoOutput.videoSettings =[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
//將輸出對(duì)象添加到隊(duì)列、并設(shè)置代理
dispatch_queue_t mProcessQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
[self.VideoOutput setSampleBufferDelegate:self queue:mProcessQueue];
//輸入,輸出對(duì)象添加到Session
if ([self.videoSession canAddInput:self.VideoInput])
{
[self.videoSession addInput:self.VideoInput];
}
if ([self.videoSession canAddOutput:self.VideoOutput])
{
[self.videoSession addOutput:self.VideoOutput];
}
//創(chuàng)建連接 AVCaptureConnection輸入對(duì)像和捕獲輸出對(duì)象之間建立連接。
_videoConnection=[self.VideoOutput connectionWithMediaType:AVMediaTypeVideo];
//視頻的方向
[_videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
//設(shè)置穩(wěn)定性痒谴,判斷connection連接對(duì)象是否支持視頻穩(wěn)定
if ([_videoConnection isVideoStabilizationSupported]) {
//這個(gè)穩(wěn)定模式最適合連接
_videoConnection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
}
//縮放裁剪系數(shù)
_videoConnection.videoScaleAndCropFactor = _videoConnection.videoMaxScaleAndCropFactor;
[self.videoSession commitConfiguration];
自動(dòng)適配設(shè)備最高分辨率
- (void)set_capture_present
{
if (![self.videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPreset1920x1080])
{
if (![self.videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPreset1280x720])
{
if (![self.videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPresetiFrame960x540])
{
if (![self.videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPreset640x480])
{
if (![self.videoDevice supportsAVCaptureSessionPreset:AVCaptureSessionPreset352x288])
{
NSLog(@"what the fuck?");
}else
{
self.videoSession.sessionPreset = AVCaptureSessionPreset352x288;
}
}else
{
self.videoSession.sessionPreset = AVCaptureSessionPresetiFrame960x540;
}
}else
{
self.videoSession.sessionPreset = AVCaptureSessionPresetiFrame960x540;
}
}else
{
self.videoSession.sessionPreset = AVCaptureSessionPreset1280x720;
}
}else
{
self.videoSession.sessionPreset = AVCaptureSessionPreset1920x1080;
}
}
回調(diào)
//視頻采集數(shù)據(jù)回調(diào)
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
if (connection == self.videoConnection)
{
//判斷是否為采集的第一幀數(shù)據(jù)
if (!self.isComed)
{
//設(shè)置一個(gè)起始時(shí)間戳
self.startTime = [[NSDate date] timeIntervalSince1970]*1000*1000;
self.isComed = 1;
self.timeStamp = 0;
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:self.startTime forKey:@"startTime"];
}
else
{
self.timeStamp = ([[NSDate date] timeIntervalSince1970]*1000*1000-self.startTime)*0.09;
}
把視頻數(shù)據(jù)和時(shí)間戳傳給代理類
[self.delegate backWithVideobuffer:sampleBuffer andTimeStamp:self.timeStamp];
}
}