1

我需要在我的软件中实现屏幕录制,但在 Windows 10 中获取 gdigrab 时遇到问题。此代码返回

“找不到输入设备。” (av_find_input_format("gdigrab") 返回 NULL)。

如果我尝试使用 ffmpeg.exe 录制桌面ffmpeg -f gdigrab -i desktop out.avi,则桌面正在正确录制。

#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;
    }
4

0 回答 0