Fixing attachment managment & cleaning paths

This commit is contained in:
Zoe Roux
2021-03-21 03:17:12 +01:00
parent db2541af52
commit 1cdb220990
5 changed files with 23 additions and 21 deletions

View File

@@ -10,13 +10,14 @@
#include <libavformat/avformat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
// @return -2 on error, -1 if track has already been extracted, 0 on success.
int create_out_path(stream *track, const char *out_path, int track_id)
{
char *folder_path;
asprintf(&folder_path, "%s/Extra/Subtitles/%s", out_path, track->language ? track->language : "und");
asprintf(&folder_path, "%s/Subtitles/%s", out_path, track->language ? track->language : "und");
if (path_mkdir_p(folder_path, 0775))
return -2;
char *extension = get_extension_from_codec(track->codec);
@@ -43,8 +44,8 @@ int create_out_path(stream *track, const char *out_path, int track_id)
free(file_name);
if (!track->path)
return -2;
// TODO return 0 if the file has a size of 0.
return access(track->path, F_OK) == 0 ? -1 : 0;
struct stat s;
return stat(track->path, &s) == 0 && s.st_size > 0;
}
int extract_stream(AVFormatContext **out_ctx, stream *s, AVFormatContext *int_ctx, AVStream *in_stream)
@@ -64,7 +65,8 @@ int extract_stream(AVFormatContext **out_ctx, stream *s, AVFormatContext *int_ct
if (*out_ctx && !((*out_ctx)->flags & AVFMT_NOFILE))
avio_closep(&(*out_ctx)->pb);
avformat_free_context(*out_ctx);
fprintf(stderr, "An error occurred, cleaning up th output context for the %s stream.\n", s->language);
fprintf(stderr, "An error occurred, cleaning up the output context for the %s stream.\n", s->language);
*out_ctx = NULL;
return -1;
}
@@ -91,8 +93,8 @@ void extract_attachment(stream *font, const char *out_path, AVStream *stream)
if (!font->path)
return;
strcpy(font->path, out_path);
strcat(font->path, "/Extra/Attachments/");
if (path_mkdir(font->path, 0733) < 0)
strcat(font->path, "/Attachments/");
if (path_mkdir(font->path, 0755) < 0)
return free(font->path);
strcat(font->path, filename->value);
long count = strchr(filename->value, '.') - filename->value;
@@ -118,8 +120,8 @@ void extract_chapters(AVFormatContext *ctx, const char *out_path)
if (!path)
return;
strcpy(path, out_path);
strcat(path, "/Extra/Chapters/");
if (path_mkdir(path, 0733) < 0)
strcat(path, "/Chapters/");
if (path_mkdir(path, 0755) < 0)
return;
strcat(path, filename);
tmp = strrchr(path, '.');

View File

@@ -92,14 +92,14 @@ stream *extract_infos(const char *path,
AVStream *stream = ctx->streams[i];
type stream_type = type_fromffmpeg(stream);
if (stream_type != none) {
*track_count += 1;
streams[i] = parse_stream(stream, stream_type, path);
if (stream_type == subtitle)
extract_track(&streams[i], out_path, stream, ctx, &output_list[i], reextract);
if (stream_type == attachment)
extract_attachment(&streams[i], out_path, stream);
}
if (stream_type == none)
continue;
*track_count += 1;
streams[i] = parse_stream(stream, stream_type, path);
if (stream_type == subtitle)
extract_track(&streams[i], out_path, stream, ctx, &output_list[i], reextract);
if (stream_type == attachment)
extract_attachment(&streams[i], out_path, stream);
}
}
if (!list_empty((void **)output_list, ctx->nb_streams))

View File

@@ -89,5 +89,5 @@ int path_mkdir_p(char *path, int mode)
*ptr = '/';
ptr++;
}
return 0;
return path_mkdir(path, mode);
}

View File

@@ -47,7 +47,7 @@ static AVDictionary *create_options_context(const char *out_path)
int folder_index = strrchr(out_path, '/') - out_path;
sprintf(seg_path, "%.*s/segments/", folder_index, out_path);
if (path_mkdir(seg_path, 0733) < 0) {
if (path_mkdir(seg_path, 0755) < 0) {
fprintf(stderr, "Error: Couldn't create segment output folder. "
"Part of the output path does not exist or you don't have write rights.\n");
return NULL;

View File

@@ -16,8 +16,8 @@ const char *type_tostring(type t)
return "Audio";
case subtitle:
return "Subtitle";
case font:
return "Font";
case attachment:
return "Attachment";
default:
return "???";
}
@@ -40,7 +40,7 @@ int main(int argc, char **argv)
stream *streams;
if (argc == 3 && !strcmp(argv[1], "info")) {
streams = extract_infos(argv[2], ".", &stream_count, &track_count);
streams = extract_infos(argv[2], "./Extra", &stream_count, &track_count, true);
puts("Info extracted:");
for (unsigned i = 0; i < track_count; i++) {
printf("%8s: %6s - %3s (%5s), D%d, F%d at %s\n",