diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 47d8d5f877..e9fb140775 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -3184,7 +3184,7 @@ static av_unused int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
 }
 
 static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index,
-                              int64_t *ppos, int64_t pos_limit)
+                              int64_t *ppos, int64_t pos_limit, int prefer_keyframe)
 {
     MpegTSContext *ts = s->priv_data;
     int64_t pos;
@@ -3200,10 +3200,14 @@ static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index,
         ret = av_read_frame(s, &pkt);
         if (ret < 0)
             return AV_NOPTS_VALUE;
+
         if (pkt.dts != AV_NOPTS_VALUE && pkt.pos >= 0) {
+            int is_keyframe = (pkt.flags & AV_PKT_FLAG_KEY) != 0;
+
             ff_reduce_index(s, pkt.stream_index);
             av_add_index_entry(s->streams[pkt.stream_index], pkt.pos, pkt.dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
-            if (pkt.stream_index == stream_index && pkt.pos >= *ppos) {
+
+            if (pkt.stream_index == stream_index && pkt.pos >= *ppos && (!prefer_keyframe || is_keyframe)) {
                 int64_t dts = pkt.dts;
                 *ppos = pkt.pos;
                 av_packet_unref(&pkt);
@@ -3271,26 +3275,26 @@ void avpriv_mpegts_parse_close(MpegTSContext *ts)
 }
 
 AVInputFormat ff_mpegts_demuxer = {
-    .name           = "mpegts",
-    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
-    .priv_data_size = sizeof(MpegTSContext),
-    .read_probe     = mpegts_probe,
-    .read_header    = mpegts_read_header,
-    .read_packet    = mpegts_read_packet,
-    .read_close     = mpegts_read_close,
-    .read_timestamp = mpegts_get_dts,
-    .flags          = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
-    .priv_class     = &mpegts_class,
+    .name            = "mpegts",
+    .long_name       = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
+    .priv_data_size  = sizeof(MpegTSContext),
+    .read_probe      = mpegts_probe,
+    .read_header     = mpegts_read_header,
+    .read_packet     = mpegts_read_packet,
+    .read_close      = mpegts_read_close,
+    .read_timestamp2 = mpegts_get_dts,
+    .flags           = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
+    .priv_class      = &mpegts_class,
 };
 
 AVInputFormat ff_mpegtsraw_demuxer = {
-    .name           = "mpegtsraw",
-    .long_name      = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
-    .priv_data_size = sizeof(MpegTSContext),
-    .read_header    = mpegts_read_header,
-    .read_packet    = mpegts_raw_read_packet,
-    .read_close     = mpegts_read_close,
-    .read_timestamp = mpegts_get_dts,
-    .flags          = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
-    .priv_class     = &mpegtsraw_class,
+    .name            = "mpegtsraw",
+    .long_name       = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
+    .priv_data_size  = sizeof(MpegTSContext),
+    .read_header     = mpegts_read_header,
+    .read_packet     = mpegts_raw_read_packet,
+    .read_close      = mpegts_read_close,
+    .read_timestamp2 = mpegts_get_dts,
+    .flags           = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
+    .priv_class      = &mpegtsraw_class,
 };
diff --git a/tests/ref/fate/concat-demuxer-simple2-lavf-ts b/tests/ref/fate/concat-demuxer-simple2-lavf-ts
index 0f03d6e06b..86fdbc733e 100644
--- a/tests/ref/fate/concat-demuxer-simple2-lavf-ts
+++ b/tests/ref/fate/concat-demuxer-simple2-lavf-ts
@@ -180,6 +180,37 @@ audio|0|180321|2.003567|180321|2.003567|2351|0.026122|N/A|N/A|209|N/A|K_
 video|1|174764|1.941822|171164|1.901822|3600|0.040000|N/A|N/A|12678|347800|__MPEGTS Stream ID
 
 video|1|178364|1.981822|174764|1.941822|3600|0.040000|N/A|N/A|24711|361336|K_
+video|1|110782|1.230911|107182|1.190911|3600|0.040000|N/A|N/A|24786|181420|K_MPEGTS Stream ID
+
+video|1|114382|1.270911|110782|1.230911|3600|0.040000|N/A|N/A|17440|206988|__MPEGTS Stream ID
+
+video|1|117982|1.310911|114382|1.270911|3600|0.040000|N/A|N/A|15019|224848|__MPEGTS Stream ID
+
+video|1|121582|1.350911|117982|1.310911|3600|0.040000|N/A|N/A|13449|240640|__MPEGTS Stream ID
+
+video|1|125182|1.390911|121582|1.350911|3600|0.040000|N/A|N/A|12398|254552|__MPEGTS Stream ID
+
+video|1|128782|1.430911|125182|1.390911|3600|0.040000|N/A|N/A|13455|267336|__MPEGTS Stream ID
+
+audio|0|99515|1.105722|99515|1.105722|2351|0.026122|N/A|N/A|209|308508|K_MPEGTS Stream ID
+
+audio|0|101866|1.131844|101866|1.131844|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|104217|1.157967|104217|1.157967|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|106568|1.184089|106568|1.184089|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|108919|1.210211|108919|1.210211|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|111270|1.236333|111270|1.236333|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|113621|1.262456|113621|1.262456|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|115972|1.288578|115972|1.288578|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|118323|1.314700|118323|1.314700|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|120674|1.340822|120674|1.340822|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|123025|1.366944|123025|1.366944|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|125376|1.393067|125376|1.393067|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|127727|1.419189|127727|1.419189|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|130078|1.445311|130078|1.445311|2351|0.026122|N/A|N/A|209|N/A|K_
+video|1|132382|1.470911|128782|1.430911|3600|0.040000|N/A|N/A|13836|281624|__MPEGTS Stream ID
+
+video|1|135982|1.510911|132382|1.470911|3600|0.040000|N/A|N/A|12163|295912|__MPEGTS Stream ID
+
 video|1|139582|1.550911|135982|1.510911|3600|0.040000|N/A|N/A|12692|311516|__MPEGTS Stream ID
 
 video|1|143182|1.590911|139582|1.550911|3600|0.040000|N/A|N/A|10824|325052|__MPEGTS Stream ID
@@ -201,6 +232,41 @@ audio|0|155939|1.732656|155939|1.732656|2351|0.026122|N/A|N/A|209|N/A|K_
 video|1|150382|1.670911|146782|1.630911|3600|0.040000|N/A|N/A|12678|347800|__MPEGTS Stream ID
 
 video|1|153982|1.710911|150382|1.670911|3600|0.040000|N/A|N/A|24711|361336|K_
+video|1|125182|1.390911|121582|1.350911|3600|0.040000|N/A|N/A|24801|564|K_MPEGTS Stream ID
+
+video|1|128782|1.430911|125182|1.390911|3600|0.040000|N/A|N/A|16429|25944|__MPEGTS Stream ID
+
+video|1|132382|1.470911|128782|1.430911|3600|0.040000|N/A|N/A|14508|42864|__MPEGTS Stream ID
+
+video|1|135982|1.510911|132382|1.470911|3600|0.040000|N/A|N/A|12622|58092|__MPEGTS Stream ID
+
+video|1|139582|1.550911|135982|1.510911|3600|0.040000|N/A|N/A|13393|71064|__MPEGTS Stream ID
+
+video|1|143182|1.590911|139582|1.550911|3600|0.040000|N/A|N/A|13092|84788|__MPEGTS Stream ID
+
+video|1|146782|1.630911|143182|1.590911|3600|0.040000|N/A|N/A|12755|98700|__MPEGTS Stream ID
+
+video|1|150382|1.670911|146782|1.630911|3600|0.040000|N/A|N/A|12023|111860|__MPEGTS Stream ID
+
+audio|0|124200|1.380000|124200|1.380000|2351|0.026122|N/A|N/A|208|152844|K_MPEGTS Stream ID
+
+audio|0|126551|1.406122|126551|1.406122|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|128902|1.432244|128902|1.432244|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|131253|1.458367|131253|1.458367|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|133604|1.484489|133604|1.484489|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|135955|1.510611|135955|1.510611|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|138306|1.536733|138306|1.536733|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|140657|1.562856|140657|1.562856|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|143008|1.588978|143008|1.588978|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|145359|1.615100|145359|1.615100|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|147710|1.641222|147710|1.641222|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|150061|1.667344|150061|1.667344|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|152412|1.693467|152412|1.693467|2351|0.026122|N/A|N/A|209|N/A|K_
+audio|0|154763|1.719589|154763|1.719589|2351|0.026122|N/A|N/A|209|N/A|K_
+video|1|153982|1.710911|150382|1.670911|3600|0.040000|N/A|N/A|14098|124268|__MPEGTS Stream ID
+
+video|1|157582|1.750911|153982|1.710911|3600|0.040000|N/A|N/A|13329|139120|__MPEGTS Stream ID
+
 video|1|161182|1.790911|157582|1.750911|3600|0.040000|N/A|N/A|12135|155852|__MPEGTS Stream ID
 
 video|1|164782|1.830911|161182|1.790911|3600|0.040000|N/A|N/A|12282|168448|__MPEGTS Stream ID
@@ -211,5 +277,5 @@ video|1|171982|1.910911|168382|1.870911|3600|0.040000|N/A|N/A|17440|206988|__MPE
 
 video|1|175582|1.950911|171982|1.910911|3600|0.040000|N/A|N/A|15019|224848|__MPEGTS Stream ID
 
-0|mp2|unknown|audio|1/44100|[3][0][0][0]|0x0003|s16p|44100|1|mono|0|N/A|0/0|0/0|1/90000|0|0.000000|N/A|N/A|64000|N/A|N/A|N/A|N/A|89|0|0|0|0|0|0|0|0|0|0|0|0
-1|mpeg2video|4|video|1/25|[2][0][0][0]|0x0002|352|288|0|0|1|1:1|11:9|yuv420p|8|tv|unknown|unknown|unknown|left|progressive|N/A|1|N/A|25/1|25/1|1/90000|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A|60|0|0|0|0|0|0|0|0|0|0|0|0
+0|mp2|unknown|audio|1/44100|[3][0][0][0]|0x0003|s16p|44100|1|mono|0|N/A|0/0|0/0|1/90000|0|0.000000|N/A|N/A|64000|N/A|N/A|N/A|N/A|117|0|0|0|0|0|0|0|0|0|0|0|0
+1|mpeg2video|4|video|1/25|[2][0][0][0]|0x0002|352|288|0|0|1|1:1|11:9|yuv420p|8|tv|unknown|unknown|unknown|left|progressive|N/A|1|N/A|25/1|25/1|1/90000|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A|78|0|0|0|0|0|0|0|0|0|0|0|0
diff --git a/tests/ref/seek/lavf-ts b/tests/ref/seek/lavf-ts
index 3347b7ead5..0fddf3aac7 100644
--- a/tests/ref/seek/lavf-ts
+++ b/tests/ref/seek/lavf-ts
@@ -16,7 +16,7 @@ ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st:-1 flags:1  ts:-0.740831
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st: 0 flags:0  ts: 2.153333
-ret: 0         st: 1 flags:1 dts: 1.794811 pts: 1.794811 pos: 308508 size:   209
+ret: 0         st: 1 flags:1 dts: 2.160522 pts: 2.160522 pos: 386716 size:   209
 ret: 0         st: 0 flags:1  ts: 1.047500
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st: 1 flags:0  ts:-0.058333
@@ -24,7 +24,7 @@ ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 152844 size:   208
 ret: 0         st: 1 flags:1  ts: 2.835833
 ret: 0         st: 1 flags:1 dts: 2.160522 pts: 2.160522 pos: 386716 size:   209
 ret: 0         st:-1 flags:0  ts: 1.730004
-ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 152844 size:   208
+ret: 0         st: 0 flags:1 dts: 1.880000 pts: 1.920000 pos: 181420 size: 24786
 ret: 0         st:-1 flags:1  ts: 0.624171
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st: 0 flags:0  ts:-0.481667
@@ -38,7 +38,7 @@ ret: 0         st: 1 flags:1 dts: 1.429089 pts: 1.429089 pos: 152844 size:   208
 ret: 0         st:-1 flags:0  ts:-0.904994
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st:-1 flags:1  ts: 1.989173
-ret: 0         st: 0 flags:0 dts: 1.960000 pts: 2.000000 pos: 224848 size: 15019
+ret: 0         st: 0 flags:1 dts: 1.880000 pts: 1.920000 pos: 181420 size: 24786
 ret: 0         st: 0 flags:0  ts: 0.883344
 ret: 0         st: 0 flags:1 dts: 1.400000 pts: 1.440000 pos:    564 size: 24801
 ret: 0         st: 0 flags:1  ts:-0.222489
