How to use custom video resolution when using AVFoundation and AVCaptureVideoDataOutput on Mac

I need to process each frame of the captured video frame, although it AVCaptureDevice.formatsprovided so many different frame sizes, it seems that it AVCaptureSessionsupports only those frame sizes that are defined in the presets.

I also tried to install AVCaptureDevice.activeFormatbefore AVCaptureInputDeviceor after, no matter what setting I set, if I set AVCaptureSessionPresetHighto AVCaptureSession, it always gives me a 1280x720 frame. Similarly, if I set AVCaptureSessionPreset640x480, then I can only get a frame size of 640x480.

So, how can I set my own video fragment size, for example 800x600? Using Media Foundation under windows or V4L2 under Linux, it is easy to set any custom frame size when capturing.

It seems impossible to do under Mac.

+5
source share
3 answers

Set the parameters kCVPixelBufferWidthKeyand kCVPixelBufferHeightKeyon the object AVCaptureVideoDataOutput. Minimum sample as shown below (add error checking).

_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 overrides the width / height of the session preset (if different).

+1
source

AFAIK there is no way to do this. All the code I saw to capture the video uses presets.

The documentation for AVCaptureVideoDataOutput for the video settings property says

kCVPixelBufferPixelFormatTypeKey.

, .

+1

Use the videoSettings property for AVCapturwVideoDataOutput to describe the format, width, and height of the pixel. In dictionary.

0
source

All Articles