Search code examples
c++ffmpeg

ffmpeg gdigrab support prebuilt library


I am using this build for Windows 64 with MSVC compiler, I need to capture screen using C++ code using gdigrab, but I can see:

Error: Input format not found: gdigrab

So just checked using the code for gdigrab:

const AVInputFormat *fmt = nullptr;
void *opaque = nullptr;

while ((fmt = av_demuxer_iterate(&opaque))) {
    qDebug() << fmt->name;
}

But does not print gdigrab

Then I check the build config for ffmpeg using

.\ffmpeg.exe -buildconf

Which do not include --enable-gdigrab

But when I check the ffmpeg exe with formats arg

.\ffmpeg.exe -formats

Give the result

D d gdigrab GDI API Windows frame grabber

Which says gdigrab is there with ffmpeg exe. And I can screen capture with this exe too.

So is the build for windows does not enabled with gdigrab, if not how the ffmpeg.exe can capture screen?. I assume the lib and binaries are built using the same config.

Edit From here https://github.com/BtbN/FFmpeg-Builds/issues/428 I understand that there is no config,

--enable-gdigrab

Since the ffmpeg.exe works for gdigrab, then the code linked with the library also expected to work.

Here is my simple code for screen recording.

    avformat_network_init();
    
    AVFormatContext *formatCtx = nullptr;
    AVDictionary *options = nullptr;
    
    const char *inputSource = "desktop";
    const char *inputFormat = "gdigrab";

    // Initialize options
    av_dict_set(&options, "video_size", "800x600", 0);
    av_dict_set(&options, "offset_x", "100", 0);
    av_dict_set(&options, "offset_y", "100", 0);
    av_dict_set(&options, "framerate", "30", 0);

    // Debug options
    char *dump = nullptr;
    av_dict_get_string(options, &dump, '=', ',');
    qDebug() << "Options: " << dump;
    av_free(dump);


    const AVInputFormat *fmt = nullptr;
    void *opaque = nullptr;

    while ((fmt = av_demuxer_iterate(&opaque))) {
        qDebug() << fmt->name;
    }


    // Find input format
   const  AVInputFormat *inputFmt = av_find_input_format(inputFormat);
    if (!inputFmt) {
        qDebug() << "Error: Input format not found:" << inputFormat;
    }

    // Open input
    int ret = avformat_open_input(&formatCtx, inputSource, inputFmt, &options);
    if (ret < 0) {
        char errbuf[AV_ERROR_MAX_STRING_SIZE];
        av_strerror(ret, errbuf, sizeof(errbuf));
        qDebug() << "Error: Unable to open input source:" << inputSource << ", " << errbuf;
        return;
    }

It always print

Error: Input format not found: gdigrab
Error: Unable to open input source: desktop ,  No such file or directory

Solution

  • I had not initialized avdevice. Once I added the following code, it worked.

    avdevice_register_all();