diff --git a/libavformat/utils.c b/libavformat/utils.c
index af2f96710d64b1c86f05cef67af398bfd08901d2..962370c7eca674eae498c87510058d43a4c22e2e 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -2888,6 +2888,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
         int err = 0;
         av_init_packet(&empty_pkt);
 
+        if (ret >= 0)
         ret = -1; /* we could not have all the codec parameters before EOF */
         for(i=0;i<ic->nb_streams;i++) {