Friday, March 25, 2016

How to configure AVCaptureSession for high res still images and low res (video) preview?

Leave a Comment

I'd like to capture high resolution still images using AVCaptureSession. Therefore AVCaptureSession preset is set to Photo.

This is working well so far. On an iPhone 4 the final still image resolution is at its maximum of 2448x3264 pixels and the preview (video) resolution is 852x640 pixels.

Now, because the preview frames are analyzed to detect objects in the scene, I'd like to lower their resolution. How can this be done? I've tried to set AVVideoSettings with a lower width/height to AVCaptureVideoDataOutput, but this leads to the following error message:

AVCaptureVideoDataOutput setVideoSettings:] - videoSettings dictionary contains one or more unsupported (ignored) keys: (AVVideoHeightKey, AVVideoWidthKey 

So it seems this is not the right approach to configure the size of the preview frames received by AVCaptureVideoDataOutput / AVCaptureVideoDataOutputSampleBufferDelegate. Do you have any ideas how the resolution of the preview frames can be configured?

Any advise is welcome, Thank you.

2 Answers

Answers 1

If you want to specify the settings manually, you need to set activeFormat on the AVCaptureDevice. This will be implicitly set the session preset to AVCaptureSessionPresetInputPriority.

The activeFormat takes a AVCaptureDeviceFormat but you can only take one from the list of AVCaptureDevice.formats. You'll need to go through the list and find one that fits your needs. Specifically, check that highResolutionStillImageDimensions is high enough for desired still capture and formatDescription (which needs to be inspected with CMFormatDescription* functions, e.g., CMVideoFormatDescriptionGetDimensions) matches your desired preview settings.

Answers 2

To lower the size of the output of AVCaptureVideoDataOutput you can set the bitrate to be lower thus producing a small sample size.

commonly used keys for AVCaptureVideoDataOutput are:

AVVideoAverageBitRateKey AVVideoProfileLevelKey AVVideoExpectedSourceFrameRateKey AVVideoMaxKeyFrameIntervalKey 

For example:

private static let videoCompressionOptionsMedium = [AVVideoAverageBitRateKey : 1750000,                                                     AVVideoProfileLevelKey : AVVideoProfileLevelH264BaselineAutoLevel,                                                     AVVideoExpectedSourceFrameRateKey : Int(30),                                                     AVVideoMaxKeyFrameIntervalKey : Int(30)] 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment