[FFmpeg-devel] [PATCH] x11grab: capture a window instead of the whole screen

Andriy Gelman andriy.gelman at gmail.com
Sun Feb 7 21:52:53 EET 2021


Hi,

On Tue, 26. Jan 11:23, sgerwk-at-aol.com at ffmpeg.org wrote:
> In my previous email the patch got mangled by the web mail interface, so I am
> sending it again. Sorry for the duplicate.

I couldn't apply this version, but previous one applied ok:
https://patchwork.ffmpeg.org/project/ffmpeg/patch/942317827.2397172.1611160298506@mail.yahoo.com/

The author name appears as sgerwk-at-aol.com at ffmpeg.org. You can update
if you want something else.

Also the commit title should start with avdevice/xcbgrab: or lavd/xcbgrab:

> 
> This patch allows ffmpeg to capture a specific window instead of the whole
> screen. An example:

> 
> ffmpeg -f x11grab -window_id 0xa00000a -i :0 output.mp4

If you try with -show_region 1, the border gets drawn in the wrong place.

> 
> ---
>  doc/indevs.texi       |  3 +++
>  libavdevice/xcbgrab.c | 19 ++++++++++++-------
>  2 files changed, 15 insertions(+), 7 deletions(-)
> 
> diff --git a/doc/indevs.texi b/doc/indevs.texi
> index 3924d03..0738919 100644
> --- a/doc/indevs.texi
> +++ b/doc/indevs.texi
> @@ -1564,6 +1564,9 @@ With @var{follow_mouse}:
>  ffmpeg -f x11grab -follow_mouse centered -show_region 1 -framerate 25 -video_size cif -i :0.0 out.mpg
>  @end example

> 
> + at item window_id
> +Grab this window, instead of the whole screen.
> +

I think it would be useful to say how to get a window id.
Probably worth adding what happens if the window size changes or it disappears.

>  @item video_size
>  Set the video frame size. Default is the full desktop.
> 
> diff --git a/libavdevice/xcbgrab.c b/libavdevice/xcbgrab.c
> index 95bdc8a..6e4ef5d 100644
> --- a/libavdevice/xcbgrab.c
> +++ b/libavdevice/xcbgrab.c
> @@ -60,6 +60,7 @@ typedef struct XCBGrabContext {
>      AVRational time_base;
>      int64_t frame_duration;
> 
> +    xcb_window_t window_id;
>      int x, y;
>      int width, height;
>      int frame_size;
> @@ -82,6 +83,7 @@ typedef struct XCBGrabContext {
>  #define OFFSET(x) offsetof(XCBGrabContext, x)
>  #define D AV_OPT_FLAG_DECODING_PARAM
>  static const AVOption options[] = {

> +    { "window_id", "Window to capture", OFFSET(window_id), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },

xcb_window_t is uint32_t, so max should be UINT32_MAX? 

>      { "x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
>      { "y", "Initial y coordinate.", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
>      { "grab_x", "Initial x coordinate.", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
> @@ -157,7 +159,7 @@ static int xcbgrab_frame(AVFormatContext *s, AVPacket *pkt)
>      XCBGrabContext *c = s->priv_data;
>      xcb_get_image_cookie_t iq;
>      xcb_get_image_reply_t *img;
> -    xcb_drawable_t drawable = c->screen->root;
> +    xcb_drawable_t drawable = c->window_id;
>      xcb_generic_error_t *e = NULL;
>      uint8_t *data;
>      int length;
> @@ -267,7 +269,7 @@ static int xcbgrab_frame_shm(AVFormatContext *s, AVPacket *pkt)
>      XCBGrabContext *c = s->priv_data;
>      xcb_shm_get_image_cookie_t iq;
>      xcb_shm_get_image_reply_t *img;
> -    xcb_drawable_t drawable = c->screen->root;
> +    xcb_drawable_t drawable = c->window_id;
>      xcb_generic_error_t *e = NULL;
>      AVBufferRef *buf;
>      xcb_shm_seg_t segment;
> @@ -566,7 +568,7 @@ static int create_stream(AVFormatContext *s)
> 
>      avpriv_set_pts_info(st, 64, 1, 1000000);
> 
> -    gc  = xcb_get_geometry(c->conn, c->screen->root);
> +    gc  = xcb_get_geometry(c->conn, c->window_id);
>      geo = xcb_get_geometry_reply(c->conn, gc, NULL);
>      if (!geo)
>          return AVERROR_EXTERNAL;
> @@ -745,7 +747,7 @@ static int select_region(AVFormatContext *s)
>              press_position = (xcb_point_t){ press->event_x, press->event_y };
>              rectangle.x = press_position.x;
>              rectangle.y = press_position.y;
> -            xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
> +            xcb_poly_rectangle(conn, c->window_id, gc, 1, &rectangle);
>              was_pressed = 1;
>              break;
>          }
> @@ -754,14 +756,14 @@ static int select_region(AVFormatContext *s)
>                  xcb_motion_notify_event_t *motion =
>                      (xcb_motion_notify_event_t *)event;
>                  xcb_point_t cursor_position = { motion->event_x, motion->event_y };
> -                xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
> +                xcb_poly_rectangle(conn, c->window_id, gc, 1, &rectangle);
>                  rectangle = rectangle_from_corners(&press_position, &cursor_position);
> -                xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
> +                xcb_poly_rectangle(conn, c->window_id, gc, 1, &rectangle);
>              }
>              break;
>          }
>          case XCB_BUTTON_RELEASE: {
> -            xcb_poly_rectangle(conn, root_window, gc, 1, &rectangle);
> +            xcb_poly_rectangle(conn, c->window_id, gc, 1, &rectangle);
>              done = 1;
>              break;
>          }
> @@ -825,6 +827,9 @@ static av_cold int xcbgrab_read_header(AVFormatContext *s)
>          return AVERROR(EIO);
>      }
> 
> +    if (c->window_id == 0)
> +        c->window_id = c->screen->root;
> +

Can 0 be a valid window_id? 
Maybe it's better to use -1 for default. I saw the function xcb_generate_id() returns -1 on error. 

Thanks,
-- 
Andriy


More information about the ffmpeg-devel mailing list