问题
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.
回答1:
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).
回答2:
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.
回答3:
Use videoSettings property of AVCapturwVideoDataOutput to describe the pixel format, width and height In a dictionary.
来源:https://stackoverflow.com/questions/16073873/how-to-use-custom-video-resolution-when-use-avfoundation-and-avcapturevideodatao