diff --git a/libavformat/hdsenc.c b/libavformat/hdsenc.c
index 98106f4b71ec03a3d17e5f7e7d9d5b0dfdacf39d..eec9bb8a6ae5b82ed2eb3cd5d1e338a26a529355 100644
--- a/libavformat/hdsenc.c
+++ b/libavformat/hdsenc.c
@@ -322,8 +322,8 @@ static int hds_write_header(AVFormatContext *s)
     AVOutputFormat *oformat;
 
     if (mkdir(s->filename, 0777) == -1 && errno != EEXIST) {
-        av_log(s, AV_LOG_ERROR , "Failed to create directory %s\n", s->filename);
         ret = AVERROR(errno);
+        av_log(s, AV_LOG_ERROR , "Failed to create directory %s\n", s->filename);
         goto fail;
     }