From eb1182eb501d62eb8533ba8379323481ade013db Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 6 Aug 2012 17:52:47 +0200 Subject: vo_image: add outdir option The function checked_mkdir is taken from vo_jpeg, with the error handling stripped down to a reasonable level. --- libvo/vo_image.c | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/libvo/vo_image.c b/libvo/vo_image.c index 8830c41e26..b00ee1af8f 100644 --- a/libvo/vo_image.c +++ b/libvo/vo_image.c @@ -20,10 +20,14 @@ #include #include #include +#include #include #include "config.h" +#include "bstr.h" +#include "osdep/io.h" +#include "path.h" #include "talloc.h" #include "mp_msg.h" #include "libvo/video_out.h" @@ -37,6 +41,7 @@ struct priv { struct image_writer_opts *opts; + char *outdir; int frame; @@ -46,6 +51,24 @@ struct priv { struct mp_csp_details colorspace; }; +static bool checked_mkdir(const char *buf) +{ + mp_msg(MSGT_VO, MSGL_INFO, "[vo_image] Creating output directory '%s'...\n", + buf); + if (mkdir(buf, 0755) < 0) { + char *errstr = strerror(errno); + if (errno == EEXIST) { + struct stat stat_p; + if (mp_stat(buf, &stat_p ) == 0 && S_ISDIR(stat_p.st_mode)) + return true; + } + mp_msg(MSGT_VO, MSGL_ERR, "[vo_image] Error creating output directory" + ": %s\n", errstr); + return false; + } + return true; +} + static int config(struct vo *vo, uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, uint32_t format) @@ -55,6 +78,10 @@ static int config(struct vo *vo, uint32_t width, uint32_t height, p->d_width = d_width; p->d_height = d_height; + if (p->outdir && vo->config_count < 1) + if (!checked_mkdir(p->outdir)) + return -1; + return 0; } @@ -74,15 +101,21 @@ static uint32_t draw_image(struct vo *vo, mp_image_t *mpi) { struct priv *p = vo->priv; - mp_image_t tmp = *mpi; - tmp.width = p->d_width; - tmp.height = p->d_height; + mp_image_t img = *mpi; + img.width = p->d_width; + img.height = p->d_height; + + void *t = talloc_new(NULL); + char *filename = talloc_asprintf(t, "%08d.%s", p->frame, + image_writer_file_ext(p->opts)); + + if (p->outdir && strlen(p->outdir)) + filename = mp_path_join(t, bstr0(p->outdir), bstr0(filename)); + + mp_msg(MSGT_VO, MSGL_STATUS, "\nSaving %s\n", filename); + write_image(&img, &p->colorspace, p->opts, filename); - char filename[80]; - snprintf(filename, sizeof(filename), "%08d.%s", p->frame, - image_writer_file_ext(p->opts)); - mp_msg(MSGT_VO, MSGL_STATUS, "Save %s!\n", filename); - write_image(&tmp, &p->colorspace, p->opts, filename); + talloc_free(t); (p->frame)++; @@ -151,6 +184,7 @@ const struct vo_driver video_out_image = }, .options = (const struct m_option[]) { OPT_SUBSTRUCT(opts, image_writer_conf, M_OPT_MERGE), + OPT_STRING("outdir", outdir, 0), {0}, }, .preinit = preinit, -- cgit v1.2.3