I'm writing application on OS X, which will capture frames from camera.
Is it possible to set capture setting using AVCaptureDevice.activeFormat
property? I had tried this, but it didn't work (session preset overrides it).
I found that on IOS it is possible with setting SessionPreset in AVCaptureSession
to AVCaptureSessionPresetInputPriority
.
The main purpose is to choose more detailed video resolutions than presets.
Best Answer
Updated: April 08, 2020.
In macOS (unlike iOS), a capture session can automatically configure the capture format after you make changes. To prevent automatic changes to the capture format use
lockForConfiguration()
method. Then call thebeginConfiguration()
method, set properties (choose one preset out of a dozen, for instanceAVCaptureSessionPresetiFrame960x540
) and after that call thecommitConfiguration()
method. In the end you need to putunlockForConfiguration()
after changing a device properties.Or follow these steps:
Call
lockForConfiguration()
to acquire access to the device’s config properties.Change the device’s
activeFormat
property (as mentioned above & below).Begin capture with the session’s
startRunning()
method.Unlock the device with the
unlockForConfiguration()
.startRunning()
andstopRunning()
methods must be invoked to start and stop the flow of your data from the inputs to the outputs, respectively.However, you might hold onto a lock, without releasing that lock, if you require the device properties to remain unchanged.
Here are details in developer's documentation lockForConfiguration().
If you attempt to set the active format to one not present in the accessible formats, will throw an
invalidArgumentException
.Also, there's an explanation how to change properties: macOS AVFoundation Video Capture
Here's a macOS code snippet written in Swift:
And here's a code snippet written in Objective-C setting min and max fps: