1

I need to implement screen recording in my software but I have a problem with getting gdigrab in windows 10. This code return

"can't find input device." (av_find_input_format("gdigrab") return NULL).

If I try to record desktop by ffmpeg.exe using ffmpeg -f gdigrab -i desktop out.avi, the desktop is recording correctly.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

extern "C"
{
#include "ffmpeg-x86-4.4/include/libavcodec/avcodec.h" 
#include "ffmpeg-x86-4.4/include/libavformat/avformat.h"
#include "ffmpeg-x86-4.4/include/libswscale/swscale.h"
#include "ffmpeg-x86-4.4/include/libavdevice/avdevice.h"
#include "ffmpeg-x86-4.4/include/libavutil/imgutils.h"
#include "ffmpeg-x86-4.4/include/libavutil/dict.h"
#include "SDL2/include/SDL.h"
}


#define OUTPUT_YUV420P 1
#define OUTPUT_H264 1

int main(int argc, char* argv[])
{
    AVFormatContext* pFormatCtx;
    AVStream* videoStream;
    AVCodecContext* pCodecCtx;
    AVCodec* pCodec;
    AVFrame* pFrame, * pFrameYUV;
    AVPacket* pPacket;
    SwsContext* pImgConvertCtx;

    int videoIndex = -1;
    unsigned int i = 0;

    SDL_Window* screen;
    SDL_Renderer* sdlRenderer;
    SDL_Texture* sdlTexture;
    SDL_Rect sdlRect;

    int screen_w = 0;
    int screen_h = 0;

    printf("Starting...\n");



    //AVInputFormat* p = NULL;

    //register device
    avdevice_register_all();
    
    //use gdigrab
    AVInputFormat* ifmt = av_find_input_format("gdigrab");

    if (!ifmt)
    {
        printf("can't find input device.\n");
        return -1;
    }

    AVDictionary* options = NULL;
    if (avformat_open_input(&pFormatCtx, "desktop", ifmt, &options) != 0)
    {
        printf("can't open input stream.\n");
        return -1;
    }

    if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
    {
        printf("can't find stream information.\n");
        return -1;
    }
Obsidian
  • 3,719
  • 8
  • 17
  • 30
  • When i tried this, i setup ffmpeg using conan ffmpeg/4.4 and it didn't work. But when i downgraded to ffmpeg/4.2.1 the same code worked. Maybe it's an issue with ffmpeg/4.4. – Vladimir Oct 20 '21 at 07:20
  • Got the similar problem, because, I didn't add avdevice_register_all, however, after adding it, everything wa sworking properly with the ffmpeg version 5.0.1 – Oleg Krymskyi Jul 25 '22 at 06:03

0 Answers0