diff --git a/libavformat/hdsenc.c b/libavformat/hdsenc.c
index 6217c1f51f4b07360dec7baf038ca2c9caf9f4e2..882f157d9130a6030e185d8d5adc85198d83a73f 100644
--- a/libavformat/hdsenc.c
+++ b/libavformat/hdsenc.c
@@ -323,7 +323,10 @@ static int hds_write_header(AVFormatContext *s)
     int ret = 0, i;
     AVOutputFormat *oformat;
 
-    mkdir(s->filename, 0777);
+    if (mkdir(s->filename, 0777) == -1 && errno != EEXIST) {
+        ret = AVERROR(errno);
+        goto fail;
+    }
 
     oformat = av_guess_format("flv", NULL, NULL);
     if (!oformat) {
diff --git a/libavformat/smoothstreamingenc.c b/libavformat/smoothstreamingenc.c
index 457472dc83145d28f6d06906d38a7087f4706cc8..d955b3437e58d53991d02eaa20dc7f6dd0955ef0 100644
--- a/libavformat/smoothstreamingenc.c
+++ b/libavformat/smoothstreamingenc.c
@@ -292,7 +292,10 @@ static int ism_write_header(AVFormatContext *s)
     int ret = 0, i;
     AVOutputFormat *oformat;
 
-    mkdir(s->filename, 0777);
+    if (mkdir(s->filename, 0777) == -1 && errno != EEXIST) {
+        ret = AVERROR(errno);
+        goto fail;
+    }
 
     oformat = av_guess_format("ismv", NULL, NULL);
     if (!oformat) {
@@ -319,7 +322,10 @@ static int ism_write_header(AVFormatContext *s)
             goto fail;
         }
         snprintf(os->dirname, sizeof(os->dirname), "%s/QualityLevels(%d)", s->filename, s->streams[i]->codec->bit_rate);
-        mkdir(os->dirname, 0777);
+        if (mkdir(os->dirname, 0777) == -1 && errno != EEXIST) {
+            ret = AVERROR(errno);
+            goto fail;
+        }
 
         ctx = avformat_alloc_context();
         if (!ctx) {