diff mbox series

[FFmpeg-devel,13/28] ffmpeg: move closing the file into of_write_trailer()

Message ID 20220111095830.31542-13-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,01/28] ffmpeg: pass the muxer context explicitly to some functions | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished
andriy/make_aarch64_jetson success Make finished
andriy/make_fate_aarch64_jetson success Make fate finished
andriy/make_armv7_RPi4 success Make finished
andriy/make_fate_armv7_RPi4 success Make fate finished

Commit Message

Anton Khirnov Jan. 11, 2022, 9:58 a.m. UTC
The current code postpones closing the files until after printing the
final report, which accesses the output file size. Deal with this by
storing the final file size before closing the file.
---
 fftools/ffmpeg.c     | 13 -------------
 fftools/ffmpeg_mux.c | 16 +++++++++++++++-
 2 files changed, 15 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index f28cc17f51..4f5c2cc49a 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -4348,7 +4348,6 @@  static int transcode_step(void)
 static int transcode(void)
 {
     int ret, i;
-    AVFormatContext *os;
     OutputStream *ost;
     InputStream *ist;
     int64_t timer_start;
@@ -4417,18 +4416,6 @@  static int transcode(void)
     /* dump report by using the first video and audio streams */
     print_report(1, timer_start, av_gettime_relative());
 
-    /* close the output files */
-    for (i = 0; i < nb_output_files; i++) {
-        os = output_files[i]->ctx;
-        if (os && os->oformat && !(os->oformat->flags & AVFMT_NOFILE)) {
-            if ((ret = avio_closep(&os->pb)) < 0) {
-                av_log(NULL, AV_LOG_ERROR, "Error closing file %s: %s\n", os->url, av_err2str(ret));
-                if (exit_on_error)
-                    exit_program(1);
-            }
-        }
-    }
-
     /* close each encoder */
     for (i = 0; i < nb_output_streams; i++) {
         ost = output_streams[i];
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 3b322ecb71..13b5d2d220 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -35,6 +35,7 @@ 
 struct Muxer {
     /* filesize limit expressed in bytes */
     int64_t limit_filesize;
+    int64_t final_filesize;
     int header_written;
 };
 
@@ -318,6 +319,17 @@  int of_write_trailer(OutputFile *of)
         return ret;
     }
 
+    of->mux->final_filesize = of_filesize(of);
+
+    if (!(of->format->flags & AVFMT_NOFILE)) {
+        ret = avio_closep(&of->ctx->pb);
+        if (ret < 0) {
+            av_log(NULL, AV_LOG_ERROR, "Error closing file %s: %s\n",
+                   of->ctx->url, av_err2str(ret));
+            return ret;
+        }
+    }
+
     return 0;
 }
 
@@ -374,7 +386,9 @@  int64_t of_filesize(OutputFile *of)
     AVIOContext *pb = of->ctx->pb;
     int64_t ret = -1;
 
-    if (pb) {
+    if (of->mux->final_filesize)
+        ret = of->mux->final_filesize;
+    else if (pb) {
         ret = avio_size(pb);
         if (ret <= 0) // FIXME improve avio_size() so it works with non seekable output too
             ret = avio_tell(pb);