diff --git a/src/guacenc/video.c b/src/guacenc/video.c index 99d990e8..8c138442 100644 --- a/src/guacenc/video.c +++ b/src/guacenc/video.c @@ -34,10 +34,14 @@ #include #include +#include +#include #include +#include #include #include #include +#include guacenc_video* guacenc_video_alloc(const char* path, const char* codec_name, int width, int height, int bitrate) { @@ -91,10 +95,18 @@ guacenc_video* guacenc_video_alloc(const char* path, const char* codec_name, } /* Open output file */ - FILE* output = fopen(path, "wb"); - if (output == NULL) { + int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR); + if (fd == -1) { guacenc_log(GUAC_LOG_ERROR, "Failed to open output file \"%s\": %s", path, strerror(errno)); + goto fail_output_fd; + } + + /* Create stream for output file */ + FILE* output = fdopen(fd, "wb"); + if (output == NULL) { + guacenc_log(GUAC_LOG_ERROR, "Failed to allocate stream for output " + "file \"%s\": %s", path, strerror(errno)); goto fail_output_file; } @@ -123,6 +135,9 @@ fail_video: fclose(output); fail_output_file: + close(fd); + +fail_output_fd: av_freep(&frame->data[0]); fail_frame_data: diff --git a/src/guacenc/video.h b/src/guacenc/video.h index b9cbf139..61c7ff8f 100644 --- a/src/guacenc/video.h +++ b/src/guacenc/video.h @@ -93,9 +93,10 @@ typedef struct guacenc_video { /** * Allocates a new guacenc_video which encodes video according to the given - * specifications, saving the output in the given file. The output file will be - * created if necessary and truncated if it already exists. Frames will be - * scaled up or down as necessary to fit the given width and height. + * specifications, saving the output in the given file. If the output file + * already exists, encoding will be aborted, and the original file contents + * will be preserved. Frames will be scaled up or down as necessary to fit the + * given width and height. * * @param path * The full path to the file in which encoded video should be written.