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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With