vo_image: add outdir option

The function checked_mkdir is taken from vo_jpeg, with the error
handling stripped down to a reasonable level.
This commit is contained in:
wm4 2012-08-06 17:52:47 +02:00
parent c7b66d99d1
commit eb1182eb50
1 changed files with 42 additions and 8 deletions

View File

@ -20,10 +20,14 @@
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <libswscale/swscale.h>
#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;
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);
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);
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,