Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use custom video resolution when use AVFoundation and AVCaptureVideoDataOutput on mac

I need to process each frame of captured video frame, although AVCaptureDevice.formats provided so many different dimension of frame sizes, it seems AVCaptureSession only support those frame sizes defined in presets.

I've also tried to set AVCaptureDevice.activeFormat before AVCaptureInputDevice or after, no matter what setting I set, if I set AVCaptureSessionPresetHigh in AVCaptureSession, it always give me a frame of 1280x720. Similar , If i set AVCaptureSessionPreset 640x480, then I can only get frame size of 640x480.

So, How can I set a custom video frame size like 800x600? Using Media Foundation under windows or V4L2 under linux, it's easy to set any custom frame size when capture.

It seems not possible to do this under mac.

like image 708
steve3d Avatar asked Apr 18 '13 03:04

steve3d


2 Answers

Set kCVPixelBufferWidthKey and kCVPixelBufferHeightKey options on AVCaptureVideoDataOutput object. Minimal sample as below ( add error check ).

_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).

like image 52
kiranpradeep Avatar answered Nov 05 '22 06:11

kiranpradeep


AFAIK there isn't a way to do this. All the code I've seen to do video capture uses the presets.

The documentation for AVCaptureVideoDataOutput for the video settings property says

The only key currently supported is the kCVPixelBufferPixelFormatTypeKey key.

so the other answers of passing in video settings won't work and it will just ignore these parameters.

like image 37
Bourne Avatar answered Nov 05 '22 06:11

Bourne