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

相关推荐
superconvert5 小时前
主流流媒体的综合性能大 PK ( smart_rtmpd, srs, zlm, nginx rtmp )
websocket·ffmpeg·webrtc·hevc·rtmp·h264·hls·dash·rtsp·srt·flv
小东来9 小时前
电脑端视频剪辑软件哪个好用,十多款剪辑软件分享
音视频
cuijiecheng201811 小时前
音视频入门基础:AAC专题(8)——FFmpeg源码中计算AAC裸流AVStream的time_base的实现
ffmpeg·音视频·aac
0点51 胜11 小时前
[ffmpeg] 视频格式转换
ffmpeg
Mr数据杨11 小时前
我的AI工具箱Tauri版-VideoIntroductionClipCut视频介绍混剪
人工智能·音视频
神一样的老师11 小时前
基于环境音频和振动数据的人类活动识别
人工智能·音视频
启明云端wireless-tag11 小时前
设备稳定流畅视频体验,乐鑫ESP32-S3芯片方案无线音视频传输通信
物联网·音视频·交互·乐鑫·wifi模组
中关村科金14 小时前
中关村科金推出得助音视频鸿蒙SDK,助力金融业务系统鸿蒙化提速
华为·音视频·harmonyos
DisonTangor15 小时前
上海人工智能实验室开源视频生成模型Vchitect 2.0 可生成20秒高清视频
人工智能·音视频
美狐美颜sdk15 小时前
探索视频美颜SDK与直播美颜工具的开发实践方案
人工智能·计算机视觉·音视频·直播美颜sdk·视频美颜sdk