diff --git a/libavformat/nut.h b/libavformat/nut.h
index 2d16f3e4690a245413ff14f35e20d80741a3b7d7..39743db1b3ac361e8978b0ef609ee6d4bc13fb63 100644
--- a/libavformat/nut.h
+++ b/libavformat/nut.h
@@ -103,6 +103,8 @@ typedef struct {
     AVRational *time_base;
     struct AVTreeNode *syncpoints;
     int sp_count;
+    int64_t max_pts;
+    AVRational *max_pts_tb;
 } NUTContext;
 
 extern const AVCodecTag ff_nut_subtitle_tags[];
diff --git a/libavformat/nutenc.c b/libavformat/nutenc.c
index 00f4653f09b9b7071d417f38ad69ff432b17f272..97bed1ef1df991c355e59dd07a6cc892e38c8ad3 100644
--- a/libavformat/nutenc.c
+++ b/libavformat/nutenc.c
@@ -863,6 +863,11 @@ static int nut_write_packet(AVFormatContext *s, AVPacket *pkt){
             nus->keyframe_pts[nut->sp_count] = pkt->pts;
     }
 
+    if(!nut->max_pts_tb || av_compare_ts(nut->max_pts, *nut->max_pts_tb, pkt->pts, *nus->time_base) < 0) {
+        nut->max_pts = pkt->pts;
+        nut->max_pts_tb = nus->time_base;
+    }
+
     return 0;
 }