diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 93c47b631b..f613e35984 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -103,6 +103,7 @@ typedef enum HLSFlags {
     HLS_SECOND_LEVEL_SEGMENT_INDEX = (1 << 8), // include segment index in segment filenames when use_localtime  e.g.: %%03d
     HLS_SECOND_LEVEL_SEGMENT_DURATION = (1 << 9), // include segment duration (microsec) in segment filenames when use_localtime  e.g.: %%09t
     HLS_SECOND_LEVEL_SEGMENT_SIZE = (1 << 10), // include segment size (bytes) in segment filenames when use_localtime  e.g.: %%014s
+    HLS_SECOND_LEVEL_SEGMENT_MICROSECOND = (1 << 15), // include microseconds of localtime in segment filenames when use_localtime  e.g.: %%f
     HLS_TEMP_FILE = (1 << 11),
     HLS_PERIODIC_REKEY = (1 << 12),
     HLS_INDEPENDENT_SEGMENTS = (1 << 13),
@@ -496,7 +497,7 @@ static int replace_str_data_in_filename(char **s, const char *filename, char pla
     return found_count;
 }
 
-static int replace_int_data_in_filename(char **s, const char *filename, char placeholder, int64_t number)
+static int replace_int_data_in_filename_forced(char **s, const char *filename, char placeholder, int64_t number, int forced_digits)
 {
     const char *p;
     char c;
@@ -521,6 +522,9 @@ static int replace_int_data_in_filename(char **s, const char *filename, char pla
                 nd = nd * 10 + *(p + addchar_count) - '0';
                 addchar_count++;
             }
+            if (forced_digits > nd) {
+                nd = forced_digits;
+            }
 
             if (*(p + addchar_count) == placeholder) {
                 av_bprintf(&buf, "%0*"PRId64, (number < 0) ? nd : nd++, number);
@@ -544,6 +548,11 @@ static int replace_int_data_in_filename(char **s, const char *filename, char pla
     return found_count;
 }
 
+static int replace_int_data_in_filename(char **s, const char *filename, char placeholder, int64_t number)
+{
+    return replace_int_data_in_filename_forced(s, filename, placeholder, number, 0);
+}
+
 static void write_styp(AVIOContext *pb)
 {
     avio_wb32(pb, 24);
@@ -1020,6 +1029,20 @@ static int sls_flags_filename_process(struct AVFormatContext *s, HLSContext *hls
             }
             ff_format_set_url(vs->avf, filename);
         }
+        if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_MICROSECOND) {
+            char *filename = NULL;
+            double mod_res;
+            if (replace_int_data_in_filename_forced(&filename, vs->avf->url,
+                                             'f',  1000000 * modf(vs->curr_prog_date_time, &mod_res), 6) < 1) {
+                av_log(hls, AV_LOG_ERROR,
+                       "Invalid second level segment filename template '%s', "
+                       "you can try to remove second_level_segment_microsecond flag\n",
+                       vs->avf->url);
+                av_freep(&filename);
+                return AVERROR(EINVAL);
+            }
+            ff_format_set_url(vs->avf, filename);
+        }
     }
     return 0;
 }
@@ -1043,6 +1066,11 @@ static int sls_flag_check_duration_size_index(HLSContext *hls)
                "second_level_segment_index hls_flag requires strftime to be true\n");
         ret = AVERROR(EINVAL);
     }
+    if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_MICROSECOND) {
+        av_log(hls, AV_LOG_ERROR,
+               "second_level_segment_microsecond hls_flag requires strftime to be true\n");
+        ret = AVERROR(EINVAL);
+    }
 
     return ret;
 }
@@ -1063,12 +1091,17 @@ static int sls_flag_check_duration_size(HLSContext *hls, VariantStream *vs)
                "second_level_segment_size hls_flag works only with file protocol segment names\n");
         ret = AVERROR(EINVAL);
     }
+    if ((hls->flags & HLS_SECOND_LEVEL_SEGMENT_MICROSECOND) && !segment_renaming_ok) {
+        av_log(hls, AV_LOG_ERROR,
+               "second_level_segment_microsecond hls_flag works only with file protocol segment names\n");
+        ret = AVERROR(EINVAL);
+    }
 
     return ret;
 }
 
 static void sls_flag_file_rename(HLSContext *hls, VariantStream *vs, char *old_filename) {
-    if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
+    if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION | HLS_SECOND_LEVEL_SEGMENT_MICROSECOND)) &&
         strlen(vs->current_segment_final_filename_fmt)) {
         ff_rename(old_filename, vs->avf->url, hls);
     }
@@ -1088,7 +1121,7 @@ static int sls_flag_use_localtime_filename(AVFormatContext *oc, HLSContext *c, V
         }
         ff_format_set_url(oc, filename);
     }
-    if (c->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) {
+    if (c->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION | HLS_SECOND_LEVEL_SEGMENT_MICROSECOND)) {
         av_strlcpy(vs->current_segment_final_filename_fmt, oc->url,
                    sizeof(vs->current_segment_final_filename_fmt));
         if (c->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {
@@ -1113,6 +1146,17 @@ static int sls_flag_use_localtime_filename(AVFormatContext *oc, HLSContext *c, V
             }
             ff_format_set_url(oc, filename);
         }
+        if (c->flags & HLS_SECOND_LEVEL_SEGMENT_MICROSECOND) {
+            char *filename = NULL;
+            if (replace_int_data_in_filename(&filename, oc->url, 'f', 0) < 1) {
+                av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', "
+                                        "you can try to remove second_level_segment_microsecond flag\n",
+                       oc->url);
+                av_freep(&filename);
+                return AVERROR(EINVAL);
+            }
+            ff_format_set_url(oc, filename);
+        }
     }
     return 0;
 }
@@ -3176,6 +3220,7 @@ static const AVOption options[] = {
     {"second_level_segment_index", "include segment index in segment filenames when use_localtime", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SECOND_LEVEL_SEGMENT_INDEX }, 0, UINT_MAX,   E, "flags"},
     {"second_level_segment_duration", "include segment duration in segment filenames when use_localtime", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SECOND_LEVEL_SEGMENT_DURATION }, 0, UINT_MAX,   E, "flags"},
     {"second_level_segment_size", "include segment size in segment filenames when use_localtime", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SECOND_LEVEL_SEGMENT_SIZE }, 0, UINT_MAX,   E, "flags"},
+    {"second_level_segment_microsecond", "include microseconds of localtime in segment filenames when use_localtime", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SECOND_LEVEL_SEGMENT_MICROSECOND }, 0, UINT_MAX,   E, "flags"},
     {"periodic_rekey", "reload keyinfo file periodically for re-keying", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_PERIODIC_REKEY }, 0, UINT_MAX,   E, "flags"},
     {"independent_segments", "add EXT-X-INDEPENDENT-SEGMENTS, whenever applicable", 0, AV_OPT_TYPE_CONST, { .i64 = HLS_INDEPENDENT_SEGMENTS }, 0, UINT_MAX, E, "flags"},
     {"iframes_only", "add EXT-X-I-FRAMES-ONLY, whenever applicable", 0, AV_OPT_TYPE_CONST, { .i64 = HLS_I_FRAMES_ONLY }, 0, UINT_MAX, E, "flags"},
