diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 71511665ac..bcdd5b3bc1 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -21,6 +21,10 @@ #include +//mkdir +#include +#include + #include "ffmpeg.h" #include "cmdutils.h" @@ -2133,6 +2137,39 @@ static int init_complex_filters(void) return 0; } + +//creates a Folder and if not existing all parent dirs returns 0 on success and -1 on errror +//this function always tries to create the parent of the named file +static int createFolder(char* file) { +//get parent folder + //search for last / + char* lastslash; + for(char* fnameit = file; *fnameit; ++fnameit) { + if((*fnameit) == '/') { + lastslash = fnameit; + } + } + + //copy path to new buffer + int lenparent = lastslash-file; + char* parent = malloc((lenparent +1) * sizeof(char)); + memcpy(parent, file, lenparent); + parent[lenparent] = '\0'; + + //printf("Try to create folder: %s\n", parent); + + //try to create + if(mkdir(parent, 0755)) { + //error + //try to create parent + if(createFolder(parent) == 0) { + return mkdir(parent, 0755); //try again + } + return -1; //error + } + return 0; //success +} + static int open_output_file(OptionsContext *o, const char *filename) { AVFormatContext *oc; @@ -2586,8 +2623,29 @@ loop_end: if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE, &oc->interrupt_callback, &of->opts)) < 0) { - print_error(filename, err); - exit_program(1); + + if(err == -ENOENT) { + //file does not exists + av_log(NULL, AV_LOG_WARNING, "%s: does not exists - create parent folder and try again [N/y]?", filename); + char c = getchar(); + printf("\n"); + if(c == 'y' || c == 'Y') { + if(createFolder(filename) == -1) { + av_log(NULL, AV_LOG_ERROR, "Could not create Directories\n"); + exit_program(1); + } + //try to init stream again + if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE, + &oc->interrupt_callback, + &of->opts)) < 0) { + print_error(filename, err); + exit_program(1); + } + } else { + print_error(filename, err); + exit_program(1); + } + } } } else if (strcmp(oc->oformat->name, "image2")==0 && !av_filename_number_test(filename)) assert_file_overwrite(filename);