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/2] 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/2] 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.