diff --git a/libavformat/utils.c b/libavformat/utils.c
index faad9c9db4e39af406892c53847bf702b0dab2f0..8757c101a393c5ce2a9045ca0b2b06f20aaddde8 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -2228,6 +2228,8 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
                              AV_TIME_BASE_Q) >= ic->max_analyze_duration) {
                 av_log(ic, AV_LOG_WARNING, "max_analyze_duration %d reached\n",
                        ic->max_analyze_duration);
+                if (ic->flags & AVFMT_FLAG_NOBUFFER)
+                    av_packet_unref(pkt);
                 break;
             }
         }
@@ -2256,6 +2258,9 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
         try_decode_frame(st, pkt,
                          (options && i < orig_nb_streams) ? &options[i] : NULL);
 
+        if (ic->flags & AVFMT_FLAG_NOBUFFER)
+            av_packet_unref(pkt);
+
         st->codec_info_nb_frames++;
         count++;
     }