From f4036a679de1f4200a7efdcbb78c3c98ff494cec Mon Sep 17 00:00:00 2001 From: Invinsible-Coder <192500415+Invinsible-Coder@users.noreply.github.com> Date: Sat, 13 Jun 2026 01:23:15 +0400 Subject: [PATCH 1/3] Fix output filename handling in encoder context --- src/lib_ccx/ccx_encoders_common.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/lib_ccx/ccx_encoders_common.c b/src/lib_ccx/ccx_encoders_common.c index 749d2e75f..426c76e1c 100644 --- a/src/lib_ccx/ccx_encoders_common.c +++ b/src/lib_ccx/ccx_encoders_common.c @@ -1361,19 +1361,38 @@ void switch_output_file(struct lib_ccx_ctx *ctx, struct encoder_ctx *enc_ctx, in if (enc_ctx->out->filename != NULL) { // Close and release the previous handle free(enc_ctx->out->filename); + enc_ctx->out->filename = NULL; close(enc_ctx->out->fh); + enc_ctx->out->fh = -1; } + const char *ext = get_file_extension(ctx->write_format); char suffix[32]; snprintf(suffix, sizeof(suffix), "_%d", track_id); char *basename = get_basename(enc_ctx->out->original_filename); - if (basename != NULL) + if (basename == NULL) + { + return; + } + + enc_ctx->out->filename = create_outfilename(basename, suffix, ext); + if (enc_ctx->out->filename == NULL) { - enc_ctx->out->filename = create_outfilename(basename, suffix, ext); - enc_ctx->out->fh = open(enc_ctx->out->filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IREAD | S_IWRITE); free(basename); + return; } + + enc_ctx->out->fh = open(enc_ctx->out->filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IREAD | S_IWRITE); + free(basename); + if (enc_ctx->out->fh < 0) + { + // Clean up if the file descriptor failed to open + free(enc_ctx->out->filename); + enc_ctx->out->filename = NULL; + return; + } + write_subtitle_file_header(enc_ctx, enc_ctx->out); // Reset counters as we switch output file. From d20eb82d9b4975b1ab090c9c3917391198039e2e Mon Sep 17 00:00:00 2001 From: Invinsible-Coder <192500415+Invinsible-Coder@users.noreply.github.com> Date: Sat, 13 Jun 2026 01:43:13 +0400 Subject: [PATCH 2/3] Remove unnecessary blank lines in ccx_encoders_common.c --- src/lib_ccx/ccx_encoders_common.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib_ccx/ccx_encoders_common.c b/src/lib_ccx/ccx_encoders_common.c index 426c76e1c..60d5b2513 100644 --- a/src/lib_ccx/ccx_encoders_common.c +++ b/src/lib_ccx/ccx_encoders_common.c @@ -1365,7 +1365,7 @@ void switch_output_file(struct lib_ccx_ctx *ctx, struct encoder_ctx *enc_ctx, in close(enc_ctx->out->fh); enc_ctx->out->fh = -1; } - + const char *ext = get_file_extension(ctx->write_format); char suffix[32]; snprintf(suffix, sizeof(suffix), "_%d", track_id); @@ -1374,14 +1374,14 @@ void switch_output_file(struct lib_ccx_ctx *ctx, struct encoder_ctx *enc_ctx, in { return; } - + enc_ctx->out->filename = create_outfilename(basename, suffix, ext); if (enc_ctx->out->filename == NULL) { free(basename); return; } - + enc_ctx->out->fh = open(enc_ctx->out->filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IREAD | S_IWRITE); free(basename); @@ -1392,7 +1392,7 @@ void switch_output_file(struct lib_ccx_ctx *ctx, struct encoder_ctx *enc_ctx, in enc_ctx->out->filename = NULL; return; } - + write_subtitle_file_header(enc_ctx, enc_ctx->out); // Reset counters as we switch output file. From 13cb3b845090d9e78e59b12f296e8c447f1e920a Mon Sep 17 00:00:00 2001 From: Invinsible-Coder <192500415+Invinsible-Coder@users.noreply.github.com> Date: Mon, 15 Jun 2026 22:09:42 +0400 Subject: [PATCH 3/3] Refactor file handling in ccx_encoders_common.c --- src/lib_ccx/ccx_encoders_common.c | 33 ++++++++++++++----------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/lib_ccx/ccx_encoders_common.c b/src/lib_ccx/ccx_encoders_common.c index 60d5b2513..10f408dc6 100644 --- a/src/lib_ccx/ccx_encoders_common.c +++ b/src/lib_ccx/ccx_encoders_common.c @@ -1369,32 +1369,29 @@ void switch_output_file(struct lib_ccx_ctx *ctx, struct encoder_ctx *enc_ctx, in const char *ext = get_file_extension(ctx->write_format); char suffix[32]; snprintf(suffix, sizeof(suffix), "_%d", track_id); - char *basename = get_basename(enc_ctx->out->original_filename); - if (basename == NULL) - { - return; - } - enc_ctx->out->filename = create_outfilename(basename, suffix, ext); - if (enc_ctx->out->filename == NULL) + char *basename = get_basename(enc_ctx->out->original_filename); + if (basename != NULL) { + enc_ctx->out->filename = create_outfilename(basename, suffix, ext); + if (enc_ctx->out->filename != NULL) + { + enc_ctx->out->fh = open(enc_ctx->out->filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IREAD | S_IWRITE); + if (enc_ctx->out->fh < 0) + { + // Clean up if the file descriptor failed to open + free(enc_ctx->out->filename); + enc_ctx->out->filename = NULL; + } + } free(basename); - return; } - enc_ctx->out->fh = open(enc_ctx->out->filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IREAD | S_IWRITE); - free(basename); - - if (enc_ctx->out->fh < 0) + if (enc_ctx->out->filename != NULL && enc_ctx->out->fh >= 0) { - // Clean up if the file descriptor failed to open - free(enc_ctx->out->filename); - enc_ctx->out->filename = NULL; - return; + write_subtitle_file_header(enc_ctx, enc_ctx->out); } - write_subtitle_file_header(enc_ctx, enc_ctx->out); - // Reset counters as we switch output file. enc_ctx->cea_708_counter = 0; enc_ctx->srt_counter = 0;