diff --git a/ffprobe.c b/ffprobe.c
index ddeaeee7b87445df0ec7bc4d285ede6607869a4e..42832d7390a12d504b186b343f218f0ce79b9cdf 100644
--- a/ffprobe.c
+++ b/ffprobe.c
@@ -1399,16 +1399,16 @@ static int probe_file(const char *filename)
 
     if ((ret = writer_open(&wctx, w, w_args, NULL)) < 0)
         goto end;
-    if ((ret = open_input_file(&fmt_ctx, filename)))
-        goto end;
 
     writer_print_header(wctx);
+    ret = open_input_file(&fmt_ctx, filename);
+    if (ret >= 0) {
     PRINT_CHAPTER(packets);
     PRINT_CHAPTER(streams);
     PRINT_CHAPTER(format);
-    writer_print_footer(wctx);
-
     avformat_close_input(&fmt_ctx);
+    }
+    writer_print_footer(wctx);
     writer_close(&wctx);
 
 end: