【问题标题】:Camera issue when setting AVCaptureDevice to 1 fps将 AVCaptureDevice 设置为 1 fps 时的相机问题
【发布时间】:2018-03-27 21:46:38
【问题描述】:

我对 AVFoundation 框架有疑问。每当我将相机的 FPS 设置为 1 时,一段时间后,我的 FaceTime 高清相机 LED 开始以一秒的间隔闪烁。此外,不再调用委托(样本缓冲区委托)。

当我将其设置为 2 FPS 时不会出现此问题,但对于我正在开发的应用程序,我需要将其设置为 1 FPS。

只需在此处检查是否有人在将相机设置为 FPS 时遇到问题。如有必要,我将向 Apple 发布错误报告。 我从 2013 年中期开始使用 Macbook Air,运行 macOS 10.13.3。

这是我用来设置 FPS 的代码:

/// Set the frame rate of the current av capture session, if it is supported by the camera.
///
/// - Parameter fps: The desired frames per second.
func set(fps: Int) {

    guard let captureDevice = captureDevice,
        let format = captureDevice.activeFormat else { return }
    let doubleFPS = Double(fps)

    // Lock the device before changing the frame rate.
    try? captureDevice.lockForConfiguration()

    // Look for a supported frame rate range that is supported by the camera.
    for range in format.videoSupportedFrameRateRanges as! [AVFrameRateRange] {
        if range.minFrameRate <= doubleFPS &&
            range.maxFrameRate >= doubleFPS {
            let time: CMTime = CMTime(value: 1, timescale: CMTimeScale(fps))
            captureDevice.activeVideoMaxFrameDuration = time
            captureDevice.activeVideoMinFrameDuration = time
            break
        }
    }

    // Unlock the device now that we are done.
    captureDevice.unlockForConfiguration()
}

【问题讨论】:

    标签: swift macos avfoundation avcapturesession


    【解决方案1】:

    如何处理帧?如果您使用AVCaptureVideoDataOutput 执行此操作,则可以丢弃您不想达到所需帧速率的任何帧。如果您正在录制到文件(例如 AVCaptureMovieFileOutput),则不能。

    p.s 你描述的问题听起来像一个错误。理想情况下,您会记录它。

    【讨论】:

    • 是的,我正在使用AVCaptureVideoDataOutput。您能否详细说明我如何丢帧以便每秒只处理 1 帧?我在问题中描述的策略似乎是根据:developer.apple.com/library/content/technotes/tn2445/…
    • 该文档是关于无意丢帧的。你想故意丢弃它们——现在你每秒写入/附加太多帧,所以检查样本缓冲区的呈现时间戳,如果前一个小于一秒前,丢弃/不要附加这个新的。通过这种方式,您的帧速率可能会降至 1 FPS 以下,因此您可能需要合并传入帧以获得所需的 1 FPS
    • 我已经实施了您提出的策略,这似乎是目前解决我问题的最佳方案。不过,它仍然无法解释我在问题中描述的问题。
    • 您描述的问题听起来像是 macOS 错误。有时间就记录一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多