前言
正文
實時的視頻流數據采集用AVFoundation框架的AVCaptureSession類
創建AVCaptureSession->設置輸入輸出對象-> 用AVCaptureConnection連接輸入輸出->在回調方法中返回視頻數據做處理
重要知識點
由于iphone設備的不同型號的前置、后置攝像頭所支持的最高分辨率各不相同,所以在設置時,一定進行判斷,不然會采集不出數據.
采集、設置
//初始化AVCaptureSession
self.videoSession =[[AVCaptureSession alloc]init];
//開始配置
[self.videoSession beginConfiguration];
//設置分辨率
[self set_capture_present];
//獲取視頻設備對象
self.videoDevice = [self cameraWithPosition:position];
//初始化視頻捕獲輸入對象
self.VideoInput = [[AVCaptureDeviceInput alloc]initWithDevice:self.videoDevice error:nil];
//初始化視頻捕獲輸出對象
self.VideoOutput = [[AVCaptureVideoDataOutput alloc]init];
// 是否卡頓時丟幀
[self.VideoOutput setAlwaysDiscardsLateVideoFrames:YES];
// 設置像素格式
self.VideoOutput.videoSettings =[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
//將輸出對象添加到隊列、并設置代理
dispatch_queue_t mProcessQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
[self.VideoOutput setSampleBufferDelegate:self queue:mProcessQueue];
//輸入,輸出對象添加到Session
if ([self.videoSession canAddInput:self.VideoInput])
{
[self.videoSession addInput:self.VideoInput];
}
if ([self.videoSession canAddOutput:self.VideoOutput])
{
[self.videoSession addOutput:self.VideoOutput];
}
//創建連接 AVCaptureConnection輸入對像和捕獲輸出對象之間建立連接。
_videoConnection=[self.VideoOutput connectionWithMediaType:AVMediaTypeVideo];
//視頻的方向
[_videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
//設置穩定性,判斷connection連接對象是否支持視頻穩定
if ([_videoConnection isVideoStabilizationSupported]) {
//這個穩定模式最適合連接
_videoConnection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
}
//縮放裁剪系數
_videoConnection.videoScaleAndCropFactor = _videoConnection.videoMaxScaleAndCropFactor;
[self.videoSession commitConfiguration];
自動適配設備最高分辨率
- (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;
}
}
回調
//視頻采集數據回調
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
if (connection == self.videoConnection)
{
//判斷是否為采集的第一幀數據
if (!self.isComed)
{
//設置一個起始時間戳
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;
}
把視頻數據和時間戳傳給代理類
[self.delegate backWithVideobuffer:sampleBuffer andTimeStamp:self.timeStamp];
}
}