如何避免不支持的帧速率错误?

How do I avoid an unsupported frame rate error?

我在 Macbook 上,我正在尝试使用 ffmpeg 库从摄像头读取视频。相机是一个10米的USB水下相机。我通过名为 ffmpeg-d 的 D 绑定使用 C API。 但是,在尝试打开输入时,我得到了这个:

[avfoundation @ 0x7fe0d2800000] Selected framerate (29.970030) is not supported by the device
[avfoundation @ 0x7fe0d2800000] Supported modes:
[avfoundation @ 0x7fe0d2800000]   160x120@[30.000030 30.000030]fps
[avfoundation @ 0x7fe0d2800000]   176x144@[30.000030 30.000030]fps
[avfoundation @ 0x7fe0d2800000]   320x240@[30.000030 30.000030]fps
[avfoundation @ 0x7fe0d2800000]   352x288@[30.000030 30.000030]fps
[avfoundation @ 0x7fe0d2800000]   640x480@[30.000030 30.000030]fps

那么如何避免这个问题呢?手动设置帧率会是答案吗?如果是这样,我该怎么做?

这是一个非常短的程序来复制我的问题。

import std.stdio;

import ffmpeg.libavdevice.avdevice;
import ffmpeg.libavcodec.avcodec;
import ffmpeg.libavformat.avformat;
import ffmpeg.libavfilter.avfilter;
import ffmpeg.libavutil.avutil;
import ffmpeg.libavutil.mem;
import ffmpeg.libavutil.pixfmt;
import ffmpeg.libswscale.swscale;

import std.string;

void main()
{
    av_register_all();
    avdevice_register_all();

    string           cameraSource        = "USB";

    AVCodecContext*  cameraCodecContext  = null;
    AVFormatContext* cameraFormatContext = avformat_alloc_context();
    AVCodec*         cameraCodec         = null;
    AVInputFormat*   cameraFormat        = av_find_input_format("avfoundation");
    AVFrame*         rawFrame = null, convertedFrame = null;

    if (avformat_open_input(&cameraFormatContext, cameraSource.toStringz, cameraFormat, null) != 0) return;
}

我对 D 或正在使用的库没有任何经验,但我可以为您指明正确的方向。您应该将 AVDictionary** options 传递给 avformat_open_input

options A dictionary filled with AVFormatContext and demuxer-private options. On return this parameter will be destroyed and replaced with a dict containing options that were not found. May be NULL.

对我来说,该词典的结构似乎几乎一对一地遵循命令行选项,因此您应该在键 "framerate" 下添加值“30”。

我注意到在某些 api 中你应该提供“framerate”而不是“r”