简体   繁体   中英

iOS 9 record video at 60fps

Trying to record video at 60fps using AVFoundation and Swift, but code below is not working. Video still records at 30fps even though 60fps is set.

captureSession = AVCaptureSession()

var finalFormat = AVCaptureDeviceFormat()
var maxFps: Double = 0
for vFormat in camera!.formats
{
    var ranges      = vFormat.videoSupportedFrameRateRanges as!  [AVFrameRateRange]
    let frameRates  = ranges[0]
    if frameRates.maxFrameRate >= maxFps && frameRates.maxFrameRate <= 60
    {
        maxFps = frameRates.maxFrameRate
        finalFormat = vFormat as! AVCaptureDeviceFormat
    }
}

print(String(maxFps) + " fps"); //prints 60 fps
try camera!.lockForConfiguration()
camera!.activeFormat = finalFormat
camera!.activeVideoMinFrameDuration = CMTimeMake(1, 60)
camera!.activeVideoMaxFrameDuration = CMTimeMake(1, 60)
camera!.unlockForConfiguration()

let video = try AVCaptureDeviceInput(device: camera) as AVCaptureDeviceInput
captureSession!.addInput(video)

let audio = try AVCaptureDeviceInput(device: AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)) as AVCaptureDeviceInput
captureSession!.addInput(audio)

fileOutput = AVCaptureMovieFileOutput()

captureSession?.addOutput(fileOutput)

captureSession!.startRunning()

Setting camera properties after creating the AVCaptureDeviceInput works.

let video = try AVCaptureDeviceInput(device: camera) as AVCaptureDeviceInput
captureSession!.addInput(video)

print(String(maxFps) + " fps"); //prints 60 fps
try camera!.lockForConfiguration()
camera!.activeFormat = finalFormat
camera!.activeVideoMinFrameDuration = CMTimeMake(1, 60)
camera!.activeVideoMaxFrameDuration = CMTimeMake(1, 60)
camera!.unlockForConfiguration()

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