add create folder question

This commit is contained in:
mrbesen 2020-10-13 14:17:47 +02:00
parent f17254b886
commit 70a6c9f3b1
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
1 changed files with 60 additions and 2 deletions

View File

@ -21,6 +21,10 @@
#include <stdint.h>
//mkdir
#include <sys/stat.h>
#include <sys/types.h>
#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);