MAC OSX AVFoundation video capture

2019-04-09 20:34发布

I am new bie to MAC OSX development. I wanted to capture video as raw frames using AVFoundation on OSX 10.7. I am not understanding setting specific video resolution to camera device, somehow I setted using VideoSettings, but if I set 320x240, it is capturing at 320x176. I am not understanding is there any API call mismatch.

Please help me yo sort out this issue. Awaiting for your reply.....Thanks in advance.......

Regards, Anand

2条回答
再贱就再见
2楼-- · 2019-04-09 21:11

The answer by user692178 works. But a cleaner approach would be to set kCVPixelBufferWidthKey and kCVPixelBufferHeightKey options on AVCaptureVideoDataOutput object. Then there won't be need to get exclusive access to device by calling AVCaptureDevice lockForConfigration before starting AVCaptureSession. Minimal sample as below.

_session = [[AVCaptureSession alloc] init];
_sessionInput = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
_sessionOutput = [[AVCaptureVideoDataOutput alloc] init];

NSDictionary *pixelBufferOptions = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithDouble:width], (id)kCVPixelBufferWidthKey,
                              [NSNumber numberWithDouble:height], (id)kCVPixelBufferHeightKey,
                              [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey,
                              nil];
[_sessionOutput setVideoSettings:pixelBufferOptions];

Note: This width/height will override session preset width/height (if different).

查看更多
Emotional °昔
3楼-- · 2019-04-09 21:24

In AVCaptureDevice there are two properties. formats and activeFormat. format will return an NSArrary of AVCaptureDeviceFormat with contains all formats exposed by cam. You select any one format from this list and set it to activeFormat. Make sure that you set the format after you receive the exclusive access to the devlce by calling AVCaptureDevice lockForConfigration. After you set the format release the lock with AVCaptureDevice unlockForConfigration. Then start the AVCaptureSession which will give you the video frames of the format you set.

AVCaptureFormat is a wraper for CMFormatDescription. CMVideoFotmatDescription is the concreete subclass of CMFormatDescription. Use CMVideoFormatDescriptionGetDimentions() to get the width and height in the set format. Use CMFormatDescriptionGetMediaSubType() to get the video codec. For raw fotmats video codec mostly is yuvs or vuy2. For compressed formats its h264, dmb1(mjpeg) and many more.

查看更多
登录 后发表回答