ffmpeg使用x11录屏

version

#define FFMPEG_VERSION "6.1.1"

note

x11视频采集结构:AVInputFormat ff_xcbgrab_demuxer

code

void CFfmpegOps::CaptureVideo(const char *outFileName)
{
    const AVInputFormat *iFmt = nullptr;
    size_t n = 0;
    AVFormatContext *iFmtCtx = nullptr;
    AVDictionary *iFmtCtxOps = nullptr;
    AVStream *video_stream = nullptr;
    int video_stream_index = 0;
    const AVPixFmtDescriptor *pix_fmt_desc = nullptr;
    int bits_per_pixel = 0;         // 像素数据
    int padding_bits_per_pixel = 0; // 像素数据+附加数据
    int pixel_fmt_is_planar = 0;
    int width = 0;
    int height = 0;
    int img_buffer_size = 0;
    int ret = -1;
    int i = 0;
    AVPacket* avpacket = nullptr;
    int capture_cnt = 1000;
    FILE *out_fp = nullptr;

    if (!outFileName)
    {
        printf("return for !outFileName\n");
        return;
    }

    iFmt = av_find_input_format("x11grab");
    if (!iFmt)
    {
        printf("av_find_input_format error\n");
        goto end;
    }

#if 0
    // 设置x11grab输入格式的可选参数
    ret = av_dict_set(&iFmtCtxOps, "framerate", "60", 0);
    if (ret < 0)
    {
        printf("av_dict_set framerate error:%s\n", GetFfmpegERR(ret));
        goto end;
    }
#endif

    ret = avformat_open_input(&iFmtCtx, nullptr, iFmt, &iFmtCtxOps);
    if (ret < 0)
    {
        printf("avformat_open_input error:%s\n", GetFfmpegERR(ret));
        goto end;
    }

    if (iFmtCtx->nb_streams < 1)
    {
        printf("no stream\n");
        goto end;
    }

    video_stream = iFmtCtx->streams[video_stream_index];

    width = video_stream->codecpar->width;
    height = video_stream->codecpar->height;

    // bgr0:b g r 0
    pix_fmt_desc = av_pix_fmt_desc_get((AVPixelFormat)(video_stream->codecpar->format));
    bits_per_pixel = av_get_bits_per_pixel(pix_fmt_desc);
    padding_bits_per_pixel = av_get_padded_bits_per_pixel(pix_fmt_desc);
    pixel_fmt_is_planar = (AV_PIX_FMT_FLAG_PLANAR & pix_fmt_desc->flags) ? 1 : 0;

    img_buffer_size = av_image_get_buffer_size((AVPixelFormat)(video_stream->codecpar->format), width, height, 1);
    if (img_buffer_size == (bits_per_pixel / 8 * width * height))
    {
        printf("img_buffer_size with bits_per_pixel\n");
    }
    else if (img_buffer_size == (padding_bits_per_pixel / 8 * width * height))
    {
        printf("img_buffer_size with padding_bits_per_pixel\n");
    }

    avpacket = av_packet_alloc();
    if (!avpacket)
    {
        printf("av_packet_alloc error\n");
        goto end;
    }

    out_fp = fopen(outFileName, "wb");
    if (!out_fp)
    {
        printf("fopen error\n");
        goto end;
    }

    while (capture_cnt)
    {
        ret = av_read_frame(iFmtCtx, avpacket);
        if (ret < 0)
        {
            printf("av_read_frame error\n");
            continue;
        }

        if (avpacket->stream_index != video_stream_index)
        {
            av_packet_unref(avpacket);
            continue;
        }

        if (avpacket->size == img_buffer_size)
        {
            for (i = 0; i < width * height; i++)
            {
                // write bgr of bgr0
                n = fwrite(avpacket->data + i * 4, sizeof(uint8_t), 3, out_fp);
            }
        }

        av_packet_unref(avpacket);

        --capture_cnt;
    }

end:
    if (out_fp)
    {
        fclose(out_fp);
        out_fp = nullptr;
    }

    if (avpacket)
    {
        av_packet_free(&avpacket);
        avpacket = nullptr;
    }

    if (iFmtCtxOps)
    {
        av_dict_free(&iFmtCtxOps);
        iFmtCtxOps = nullptr;
    }

    if (iFmtCtx)
    {
        avformat_close_input(&iFmtCtx);
    }
}

performance

相关推荐
Black蜡笔小新13 分钟前
WebRTC嵌入式视频通话SDK:EasyRTC从免插件到轻量级带来的音视频通话技术
音视频·webrtc·sdk·rtc·webp2p
EasyNVR15 分钟前
EasyRTC:开启智能硬件与全平台互动新时代
网络·音视频·webrtc·p2p·智能硬件·视频监控
EasyGBS16 分钟前
从开发到部署:EasyRTC嵌入式视频通话SDK如何简化实时音视频通信的集成与应用
音视频·webrtc·实时音视频·视频监控
AI服务老曹9 小时前
通过感知、分析、预测、控制,最大限度发挥效率的智慧油站开源了
人工智能·开源·自动化·音视频
St-sun9 小时前
短视频导演编导制片人编剧教程策划影视媒体电商摄影后期 知识总结
音视频·媒体
SKYDROID云卓小助手11 小时前
无人设备遥控器之视频回传篇
网络·人工智能·嵌入式硬件·目标检测·计算机视觉·音视频
EasyNVR12 小时前
智能硬件新时代,EasyRTC开启物联音视频新纪元
运维·服务器·音视频·webrtc·p2p·智能硬件·视频监控
X@AKS13 小时前
http 响应码影响 video 标签播放视频
网络协议·http·音视频
深耕AI15 小时前
使用DeepSeek自动获取视频双语字幕srt文件【工具说明】
音视频·deepseek
科技小E15 小时前
EasyRTC:智能硬件适配,实现多端音视频互动新突破
网络协议·安全·小程序·音视频·webrtc·p2p·视频监控