菜鸟教程小白 发表于 2022-12-11 19:17:40

ios - 使用 AVCaptureVideo 在 Swift 中捕获 240 fps 的视频


                                            <p><p>我想在我的 iPhone 显示屏上看到 240 fps 的视频预览。<br/>
我的代码(简化)是这样的(如下):
进行 session ,激活相机,并在显示屏上显示实际的视频预览。 </p>

<pre><code>var session: AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?



override func viewDidLoad() {
    super.viewDidLoad()
    super.viewWillAppear(true)

    session = AVCaptureSession()

    let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
    var error: NSError?
    var input: AVCaptureDeviceInput!
    do {
      input = try AVCaptureDeviceInput(device: backCamera)

    } catch let errorgesehen as NSError {
      error = errorgesehen
      input = nil
      print(error!.localizedDescription)
    }

    configureCameraForHighestFrameRate(device: backCamera!)


    if error == nil &amp;&amp; session!.canAddInput(input) {
      session!.addInput(input)
    }

    videoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
    videoPreviewLayer!.videoGravity = AVLayerVideoGravityResizeAspect
    videoPreviewLayer!.frame = CGRect(x: 0.0, y: 0.0, width: view.bounds.size.width, height: view.bounds.size.height)
    videoPreviewLayer!.backgroundColor = UIColor(red: 0, green: 1, blue: 0, alpha: 1).cgColor
    view.layer.addSublayer(videoPreviewLayer!)


    session?.startRunning()

    UIApplication.shared.isIdleTimerDisabled = true
}
</code></pre>

<p>现在我希望此视频预览以 240fps 显示(60 或 120 也可以,但 240 是最好的)。
为了解决这个问题,我使用了这个:<a href="https://developer.apple.com/documentation/avfoundation/avcapturedevice" rel="noreferrer noopener nofollow">https://developer.apple.com/documentation/avfoundation/avcapturedevice</a> (我已将其转换为 Swift)。
<code>configureCameraForHighestFrameRate</code> 函数代码如下:</p>

<pre><code>func configureCameraForHighestFrameRate(device: AVCaptureDevice) {
    var bestFormat: AVCaptureDeviceFormat? = nil
    var bestFrameRateRange: AVFrameRateRange? = nil
    for formatf in device.formats {
      var format = formatf as! AVCaptureDeviceFormat
      print(format)
      for rangef in format.videoSupportedFrameRateRanges {
            var range = rangef as! AVFrameRateRange
            print(range)
            if (bestFrameRateRange == nil) {
                bestFormat = format
                bestFrameRateRange = range
            } else if range.maxFrameRate &gt; bestFrameRateRange!.maxFrameRate {
                bestFormat = format
                bestFrameRateRange = range
            }
      }
    }

    if (bestFormat == nil) {
      print(&#34;Es gibt keine Formate, die Apokalypse ist ausgebrochen.&#34;)
      return;
    } else if (bestFrameRateRange == nil) {
      print(&#34;Es gibt keine Bilder, die Apokalypse ist ausgebrochen.&#34;)
      return;
    }

    let Richtig = bestFormat!
    let fps = bestFrameRateRange!
    do {
      try device.lockForConfiguration()
    }
    catch let error as NSError {
      print(error.description)
    }
    device.activeFormat = Richtig
    device.activeVideoMinFrameDuration = fps.minFrameDuration
    device.activeVideoMaxFrameDuration = fps.minFrameDuration
    device.unlockForConfiguration()
}
</code></pre>

<p>此代码查找 fps 数最大 (=240) 的 AVCaptureDeviceFormat。实际格式设置为此格式,并将 fps 数设置为最大值 (=240)。<br/>
但是我有一个问题,视频输出仍然只有每秒 30 帧(如果我很快转动 iPhone,我会认出来)。<br/>
此外,有几种格式的最大 fps 为 240。这段代码只选择了其中的第一种,但我问我是否有区别以及选择哪种以获得最佳输出。谁能说出几种格式的区别,或者它们中的哪种格式,“相机”应用程序在慢动作工具中使用?
非常感谢:)</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您应该在将格式配置设置为 <code>AVCaptureDevice</code></p> 之前启动您的 <code>AVCaptureSession</code></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用 AVCaptureVideo 在 Swift 中捕获 240 fps 的视频,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47295233/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47295233/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用 AVCaptureVideo 在 Swift 中捕获 240 fps 的视频