diff --git a/src/lib_ccx/ccx_encoders_common.c b/src/lib_ccx/ccx_encoders_common.c index 749d2e75f..10f408dc6 100644 --- a/src/lib_ccx/ccx_encoders_common.c +++ b/src/lib_ccx/ccx_encoders_common.c @@ -1361,20 +1361,36 @@ 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) { 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); + 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); } - write_subtitle_file_header(enc_ctx, enc_ctx->out); + if (enc_ctx->out->filename != NULL && enc_ctx->out->fh >= 0) + { + write_subtitle_file_header(enc_ctx, enc_ctx->out); + } // Reset counters as we switch output file. enc_ctx->cea_708_counter = 0;