diff --git a/doc/muxers.texi b/doc/muxers.texi
index 8ce964b..01f7fe4 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -874,6 +874,10 @@ publishing it repeatedly every after 30 segments i.e. every after 60s.
 @item http_persistent
 Use persistent HTTP connections. Applicable only for HTTP output.
 
+@item cc_instream_id
+Add @code{#EXT-X-MEDIA} tag in the master playlist with the specified instream ID. It
+accepts the values in the range 1-4.
+
 @end table
 
 @anchor{ico}
diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index 3345b89..39d0afe 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -820,7 +820,7 @@ static int write_manifest(AVFormatContext *s, int final)
                 stream_bitrate += max_audio_bitrate;
             }
             get_hls_playlist_name(playlist_file, sizeof(playlist_file), NULL, i);
-            ff_hls_write_stream_info(st, out, stream_bitrate, playlist_file, agroup);
+            ff_hls_write_stream_info(st, out, stream_bitrate, playlist_file, agroup, NULL);
         }
         avio_close(out);
         if (use_rename)
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index e6f3241..bff3e6e 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -206,6 +206,7 @@ typedef struct HLSContext {
     int http_persistent;
     AVIOContext *m3u8_out;
     AVIOContext *sub_m3u8_out;
+    int cc_instream_id; /* closed captions INSTREAM-ID */
 } HLSContext;
 
 static int mkdir_p(const char *path) {
@@ -1116,6 +1117,7 @@ static int create_master_playlist(AVFormatContext *s,
     unsigned int i, j;
     int m3u8_name_size, ret, bandwidth;
     char *m3u8_rel_name;
+    char cc_group[16] = {0};
 
     input_vs->m3u8_created = 1;
     if (!hls->master_m3u8_created) {
@@ -1142,6 +1144,14 @@ static int create_master_playlist(AVFormatContext *s,
 
     ff_hls_write_playlist_version(hls->m3u8_out, hls->version);
 
+    if (hls->cc_instream_id) {
+        av_strlcpy(cc_group, "group_cc", sizeof(cc_group));
+        avio_printf(hls->m3u8_out, "#EXT-X-MEDIA:TYPE=CLOSED-CAPTIONS,GROUP-ID=\"%s\"",
+                    cc_group);
+        avio_printf(hls->m3u8_out, ",NAME=\"captions\",INSTREAM-ID=\"CC%d\"\n",
+                    hls->cc_instream_id);
+    }
+
     /* For audio only variant streams add #EXT-X-MEDIA tag with attributes*/
     for (i = 0; i < hls->nb_varstreams; i++) {
         vs = &(hls->var_streams[i]);
@@ -1227,7 +1237,8 @@ static int create_master_playlist(AVFormatContext *s,
         bandwidth += bandwidth / 10;
 
         ff_hls_write_stream_info(vid_st, hls->m3u8_out, bandwidth, m3u8_rel_name,
-                aud_st ? vs->agroup : NULL);
+                                 aud_st ? vs->agroup : NULL,
+                                 vid_st ? cc_group : NULL);
 
         av_freep(&m3u8_rel_name);
     }
@@ -2436,6 +2447,7 @@ static const AVOption options[] = {
     {"master_pl_name", "Create HLS master playlist with this name", OFFSET(master_pl_name), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,    E},
     {"master_pl_publish_rate", "Publish master play list every after this many segment intervals", OFFSET(master_publish_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, UINT_MAX, E},
     {"http_persistent", "Use persistent HTTP connections", OFFSET(http_persistent), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
+    {"cc_instream_id", "Closed captions INSTREAM-ID", OFFSET(cc_instream_id), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4, E},
     { NULL },
 };
 
diff --git a/libavformat/hlsplaylist.c b/libavformat/hlsplaylist.c
index 098dc89..1bfcce9 100644
--- a/libavformat/hlsplaylist.c
+++ b/libavformat/hlsplaylist.c
@@ -46,7 +46,8 @@ void ff_hls_write_audio_rendition(AVIOContext *out, char *agroup,
 }
 
 void ff_hls_write_stream_info(AVStream *st, AVIOContext *out,
-                              int bandwidth, char *filename, char *agroup) {
+                              int bandwidth, char *filename, char *agroup,
+                              char *cc_group) {
     if (!out || !filename)
         return;
 
@@ -62,6 +63,8 @@ void ff_hls_write_stream_info(AVStream *st, AVIOContext *out,
                 st->codecpar->height);
     if (agroup && strlen(agroup) > 0)
         avio_printf(out, ",AUDIO=\"group_%s\"", agroup);
+    if (cc_group && strlen(cc_group) > 0)
+        avio_printf(out, ",CLOSED-CAPTIONS=\"%s\"", cc_group);
     avio_printf(out, "\n%s\n\n", filename);
 }
 
diff --git a/libavformat/hlsplaylist.h b/libavformat/hlsplaylist.h
index 9969315..0ff99f1 100644
--- a/libavformat/hlsplaylist.h
+++ b/libavformat/hlsplaylist.h
@@ -40,7 +40,8 @@ void ff_hls_write_playlist_version(AVIOContext *out, int version);
 void ff_hls_write_audio_rendition(AVIOContext *out, char *agroup,
                                   char *filename, int name_id, int is_default);
 void ff_hls_write_stream_info(AVStream *st, AVIOContext *out,
-                              int bandwidth, char *filename, char *agroup);
+                              int bandwidth, char *filename, char *agroup,
+                              char *cc_group);
 void ff_hls_write_playlist_header(AVIOContext *out, int version, int allowcache,
                                   int target_duration, int64_t sequence,
                                   uint32_t playlist_type);
