iOS 9以60fps录制视频

时间:2016-02-04 23:42:42

标签: ios swift avfoundation

尝试使用AVFoundation和Swift以60fps录制视频,但下面的代码无效。即使设置了60fps,视频仍以30fps记录。

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()

1 个答案:

答案 0 :(得分:1)

创建AVCaptureDeviceInput后设置相机属性。

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()