AVAssetWriter能以240fps的速度导出视频慢动作1080p视频吗?

时间:2016-03-03 19:27:22

标签: ios swift avfoundation

我有一个小应用程序,试图以240 fps录制和导出视频。该应用使用AVCaptureVideoDataOutputCMSampleBufferRef个对象投放到AVAssetWriter,并带有视频AVAssetWriterInput

以下是视频输出设置:

let outputSettings = [
  AVVideoCodecKey  : AVVideoCodecH264,
  AVVideoWidthKey  : NSNumber(float: 1920),
  AVVideoHeightKey : NSNumber(float: 1080),
  AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey: Int(640000), AVVideoProfileLevelKey: AVVideoProfileLevelH264High40 ]
]

videoWriterVideoInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: outputSettings)

以下是启用慢动作格式的方法:

private func turnOnSlomoFormat() {
captureSession.stopRunning()

var bestFormat: AVCaptureDeviceFormat?
var bestFrameRateRange: AVFrameRateRange?

for format in backCamera!.device.formats as! [AVCaptureDeviceFormat] {
  for range in format.videoSupportedFrameRateRanges as! [AVFrameRateRange] {
    if bestFormat == nil {
      bestFormat = format
      bestFrameRateRange = range
    }

    if range.maxFrameRate > bestFrameRateRange?.maxFrameRate {
      bestFormat = format
      bestFrameRateRange = range
    }
  }
}

if bestFormat != nil {
  do {
    try backCamera!.device.lockForConfiguration()
  } catch let error as NSError {
    print(error.localizedDescription)
  }

  backCamera!.device.activeFormat = bestFormat
  backCamera!.device.activeVideoMaxFrameDuration = CMTimeMake(1, 240)
  backCamera!.device.activeVideoMinFrameDuration = bestFrameRateRange!.minFrameDuration

  backCamera!.device.unlockForConfiguration()
}

captureSession.startRunning()
}

当视频以1920x1080保存时,其帧速率为96.2859。当我将输出设置更改为1280x720时,其帧速率为200(不完美,但足够接近)。测试是在iPhone 6上完成的。使用AVAssetTrack.nominalFrameRate和VLC媒体检查员检查帧率是否合适。

我尝试过使用AVCaptureSessionPreset1920x1080和AVCaptureSessionPreset1280x720。我可以捕获720p帧并将它们上传到1080p视频,但这会将帧速率降低一半。

0 个答案:

没有答案