[FFmpeg-devel] [PATCH] x11grab: capture a window instead of the whole screen
sgerwk at aol.com
sgerwk at aol.com
Tue Jan 26 12:23:51 EET 2021
In my previous email the patch got mangled by the web mail interface, so I am
sending it again. Sorry for the duplicate.
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
---
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.
+
@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 },
{ "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;
+
if (c->select_region) {
ret = select_region(s);
if (ret < 0) {
--
2.29.2
More information about the ffmpeg-devel
mailing list