简体   繁体   中英

macOS swift AVCaptureSession sessionPreset doesn't work

I'm using AVFoundation to capture photo. But no matter however I changed sessionPreset, the output remains videoDevice.activeFormat Optional(<AVCaptureDeviceFormat: 0x608000001eb0> 'vide'/'2vuy' enc dims = 1280x720, pres dims = 1280x720 { 29.97 25.00 24.00 15.00 fps })

All I want is to change the output to high quality.

My whole code is

    self.avCaptureSession = AVCaptureSession()

    let devices = AVCaptureDevice.devices()
    self.avCurrentCaptureDevice = devices!.last as? AVCaptureDevice
    do {
        self.avCaptureInput = try AVCaptureDeviceInput(device: self.avCurrentCaptureDevice)
    } catch {
    }

    do {
        try self.avCurrentCaptureDevice?.lockForConfiguration()
    } catch {
        return
    }

    self.avCaptureSession?.beginConfiguration()
    self.avCaptureSession?.addInput(self.avCaptureInput)

    self.avCaptureSession?.sessionPreset = AVCaptureSessionPreset320x240

    self.avOutput = AVCaptureVideoDataOutput()
    if (self.avCaptureSession?.canAddOutput(self.avOutput) != nil) {
        self.avCaptureSession?.addOutput(self.avOutput)
    }
    self.avOutput?.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString) : NSNumber(value: kCVPixelFormatType_32BGRA as UInt32)]
    self.avOutput?.alwaysDiscardsLateVideoFrames = true

    let queue: DispatchQueue = DispatchQueue(label: "myqueue",  attributes: [])
    self.avOutput?.setSampleBufferDelegate(self, queue: queue)

    self.avCaptureSession?.commitConfiguration()

    print("videoDevice.activeFormat", self.avCurrentCaptureDevice?.activeFormat! as Any)

Capture input and capture output format can be different.

Try check input device's largest dimension and fps first. I guess the preset is basiccaly applied to output format.

Also My code may be your help. AVCaptureManager

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM