Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can AVCaptureSession use custom resolution

I'm using AVCaptureSession to capture and record a video.

I need to record the video at a 4:3 ratio, and with a good resolution.

Is there a way to specify a custom resolution when capturing using AVCaptureSession?

I tried using the native presets but the problem is that I need to capture at a 4:3 ratio, and almost all of the presets are 16:9. and the ones that are 4:3 has very low resolution.

I can't fide any other way to change the preset to a custom one, what if I need to capture a 4:3 video with better resolution? Any ideas?

like image 516
Eyal Avatar asked Jan 06 '16 18:01

Eyal


2 Answers

AVCaptureSession presets cover only a small subset of the capabilities of a device camera (the ones most apps want quick, easy access to). For more fine-grained control — such as to select a capture resolution not provided by a session preset — you need to use capture formats instead.

Look at the capture device's formats property, an array of AVCaptureDeviceFormat objects. Enumerate through that array until you find one whose dimensions are what you want. To get the dimensions, look at the format's underlying CMFormatDescription:

let fdesc = format.formatDescription
let dims = CMVideoFormatDescriptionGetDimensions(fdesc)
NSLog("%d x %d", dims.width, dims.height)

Once you've found the format you want, lock the device for configuration and set its activeFormat:

if try device.lockForConfiguration() {
    device.activeFormat = myChosenFormat
    // set up other things like activeVideoMinFrameDuration if you want
    device.unlockForConfiguration()
}

You can find out more about configuring a capture session via AVCaptureDeviceFormat in Apple's programming guide and the WWDC2013 session that introduced device formats back in iOS 7.0. (Most of what you'll find about this topic is aimed at slow-motion video, taking high-res stills during video, and other things that you can't do with session presets, but those aren't the only things you can do with capture formats.)

like image 165
rickster Avatar answered Nov 11 '22 22:11

rickster


Just record at the given aspect ratio and use an AVMutableComposition to crop the output video to the required aspect ratio: If you adjust the preview layer to mask to 4:3 this will appear seamless to the user.

like image 22
davbryn Avatar answered Nov 11 '22 20:11

davbryn