From c6f7f7c78fc3b4764e0ac7c51ef258603274ef0d Mon Sep 17 00:00:00 2001 From: Yi <10001077+yy1021564143@user.noreply.gitee.com> Date: Fri, 3 Dec 2021 12:08:28 +0000 Subject: [PATCH] =?UTF-8?q?update=20capture.hpp.=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E8=87=AA=E9=80=82=E5=BA=94=E5=88=86=E8=BE=A8=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- capture.hpp | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/capture.hpp b/capture.hpp index 43a1707..bb75136 100644 --- a/capture.hpp +++ b/capture.hpp @@ -5,6 +5,17 @@ #include #include +//获取分辨率 +#include +#include +#include +#include +#include +#include +#include +#include +#include + //ffmpeg的库都是c语言标准 #ifdef __cplusplus extern "C" @@ -46,6 +57,11 @@ private: AVFrame *bgr_frame; AVFrame *yuv_frame; + //获取分辨率 + Display *display; + int screen_num; + unsigned int display_width, display_height; + public: capture() { @@ -56,9 +72,22 @@ public: int open_device() { + //动态获取分辨率 + display = XOpenDisplay(NULL); + screen_num = DefaultScreen(display); + display_width = XDisplayWidth(display, screen_num); + display_height = XDisplayHeight(display, screen_num); + printf("%dx%d\n", display_width, display_height); + if (display_width % 2 == 1) //奇数宽或高打不开 + display_width--; + if (display_height % 2 == 1) + display_height--; + char siz[10]; + sprintf(siz, "%dx%d", display_width, display_height); + read_stream_ctx = avformat_alloc_context(); AVDictionary *options = NULL; - av_dict_set(&options, "video_size", "1920x1080", 0); + av_dict_set(&options, "video_size", siz, 0); //"1920x1080"改为siz,下方的1920都改为display_width,1080都改为display_height av_dict_set(&options, "start_time_realtime", 0, 0); av_dict_set(&options, "rtbufsize", "10M", 0); @@ -99,8 +128,8 @@ public: } av_code_ctx = avcodec_alloc_context3(codec); av_code_ctx->bit_rate = 3600000; - av_code_ctx->width = 1920; - av_code_ctx->height = 1080; + av_code_ctx->width = display_width; + av_code_ctx->height = display_height; av_code_ctx->time_base = (AVRational){1, 25}; av_code_ctx->gop_size = 5; av_code_ctx->max_b_frames = 3; @@ -116,14 +145,14 @@ public: return -1; } - sws_ctx = sws_getContext(1920, 1080, av_decode_ctx->pix_fmt, 1920, 1080, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); + sws_ctx = sws_getContext(display_width, display_height, av_decode_ctx->pix_fmt, display_width, display_height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); in_pkt = av_packet_alloc(); bgr_frame = av_frame_alloc(); yuv_frame = av_frame_alloc(); yuv_frame->format = AV_PIX_FMT_YUV420P; - yuv_frame->width = 1920; - yuv_frame->height = 1080; + yuv_frame->width = display_width; + yuv_frame->height = display_height; av_frame_get_buffer(yuv_frame, 32); return 0; } @@ -138,7 +167,7 @@ public: int got_frame = 0; int value = avcodec_decode_video2(av_decode_ctx, bgr_frame, &got_frame, in_pkt); - sws_scale(sws_ctx, bgr_frame->data, bgr_frame->linesize, 0, 1080, yuv_frame->data, yuv_frame->linesize); + sws_scale(sws_ctx, bgr_frame->data, bgr_frame->linesize, 0, display_height, yuv_frame->data, yuv_frame->linesize); avcodec_encode_video2(av_code_ctx, out, yuv_frame, &got_frame); yuv_frame->pts++; -- Gitee