summaryrefslogtreecommitdiffstats
path: root/core/screenshot.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/screenshot.c')
-rw-r--r--core/screenshot.c96
1 files changed, 68 insertions, 28 deletions
diff --git a/core/screenshot.c b/core/screenshot.c
index 4f7a0dbcdd..6b07bce5a0 100644
--- a/core/screenshot.c
+++ b/core/screenshot.c
@@ -278,16 +278,12 @@ static void add_subs(struct MPContext *mpctx, struct mp_image *image)
OSD_DRAW_SUB_ONLY, image);
}
-static void screenshot_save(struct MPContext *mpctx, struct mp_image *image,
- bool with_subs)
+static void screenshot_save(struct MPContext *mpctx, struct mp_image *image)
{
screenshot_ctx *ctx = mpctx->screenshot_ctx;
struct image_writer_opts *opts = mpctx->opts.screenshot_image_opts;
- if (with_subs)
- add_subs(mpctx, image);
-
char *filename = gen_fname(ctx, image_writer_file_ext(opts));
if (filename) {
screenshot_msg(ctx, SMSG_OK, "Screenshot: '%s'", filename);
@@ -295,30 +291,15 @@ static void screenshot_save(struct MPContext *mpctx, struct mp_image *image,
screenshot_msg(ctx, SMSG_ERR, "Error writing screenshot!");
talloc_free(filename);
}
-
- talloc_free(image);
}
-void screenshot_request(struct MPContext *mpctx, int mode, bool each_frame,
- bool osd)
+static struct mp_image *screenshot_get(struct MPContext *mpctx, int mode)
{
+ struct mp_image *image = NULL;
if (mpctx->video_out && mpctx->video_out->config_ok) {
- screenshot_ctx *ctx = mpctx->screenshot_ctx;
-
if (mode == MODE_SUBTITLES && mpctx->osd->render_subs_in_filter)
mode = 0;
- if (each_frame) {
- ctx->each_frame = !ctx->each_frame;
- if (!ctx->each_frame)
- return;
- } else {
- ctx->each_frame = false;
- }
-
- ctx->mode = mode;
- ctx->osd = osd;
-
struct voctrl_screenshot_args args =
{ .full_window = (mode == MODE_FULL_WINDOW) };
@@ -328,14 +309,73 @@ void screenshot_request(struct MPContext *mpctx, int mode, bool each_frame,
if (!args.out_image)
vo_control(mpctx->video_out, VOCTRL_SCREENSHOT, &args);
- if (args.out_image) {
- if (args.has_osd)
- mode = 0;
- screenshot_save(mpctx, args.out_image, mode == MODE_SUBTITLES);
- } else {
- screenshot_msg(ctx, SMSG_ERR, "Taking screenshot failed.");
+ image = args.out_image;
+ if (image) {
+ if (mode == MODE_SUBTITLES && !args.has_osd)
+ add_subs(mpctx, image);
}
}
+ return image;
+}
+
+void screenshot_to_file(struct MPContext *mpctx, const char *filename, int mode,
+ bool osd)
+{
+ screenshot_ctx *ctx = mpctx->screenshot_ctx;
+ struct image_writer_opts opts = *mpctx->opts.screenshot_image_opts;
+ bool old_osd = ctx->osd;
+ ctx->osd = osd;
+
+ if (mp_path_exists(filename)) {
+ screenshot_msg(ctx, SMSG_ERR, "Screenshot: file '%s' already exists.",
+ filename);
+ goto end;
+ }
+ char *ext = mp_splitext(filename, NULL);
+ if (ext)
+ opts.format = ext + 1; // omit '.'
+ struct mp_image *image = screenshot_get(mpctx, mode);
+ if (!image) {
+ screenshot_msg(ctx, SMSG_ERR, "Taking screenshot failed.");
+ goto end;
+ }
+ screenshot_msg(ctx, SMSG_OK, "Screenshot: '%s'", filename);
+ if (!write_image(image, &opts, filename))
+ screenshot_msg(ctx, SMSG_ERR, "Error writing screenshot!");
+ talloc_free(image);
+
+end:
+ ctx->osd = old_osd;
+}
+
+void screenshot_request(struct MPContext *mpctx, int mode, bool each_frame,
+ bool osd)
+{
+ screenshot_ctx *ctx = mpctx->screenshot_ctx;
+
+ if (mode == MODE_SUBTITLES && mpctx->osd->render_subs_in_filter)
+ mode = 0;
+
+ if (each_frame) {
+ ctx->each_frame = !ctx->each_frame;
+ if (!ctx->each_frame)
+ return;
+ } else {
+ ctx->each_frame = false;
+ }
+
+ ctx->mode = mode;
+ ctx->osd = osd;
+
+ struct mp_image *image = screenshot_get(mpctx, mode);
+
+ if (image) {
+ screenshot_save(mpctx, image);
+ } else {
+ screenshot_msg(ctx, SMSG_ERR, "Taking screenshot failed.");
+ }
+
+ talloc_free(image);
}
void screenshot_flip(struct MPContext *mpctx)