[FFmpeg-devel] [PATCH 14/24] ffmpeg: move closing the file into of_write_trailer()

Anton Khirnov anton at khirnov.net
Mon Dec 13 17:20:32 EET 2021


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 | 15 ++++++++++++++-
 2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index b7f26fe288..81a41fc8d1 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 3ae20fdeeb..3ee0fc0667 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -35,6 +35,7 @@
 struct Muxer {
     /* filesize limit expressed in bytes */
     uint64_t limit_filesize;
+    uint64_t final_filesize;
     int header_written;
 };
 
@@ -318,6 +319,17 @@ int of_write_trailer(OutputFile *of)
         return ret;
     }
 
+    if (!(of->format->flags & AVFMT_NOFILE)) {
+        of->mux->final_filesize = of->ctx->pb->bytes_written;
+
+        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;
 }
 
@@ -375,5 +387,6 @@ int of_finished(OutputFile *of)
 int64_t of_bytes_written(OutputFile *of)
 {
     AVIOContext *pb = of->ctx->pb;
-    return pb ? pb->bytes_written : -1;
+    return of->mux->final_filesize ? of->mux->final_filesize :
+            pb ? pb->bytes_written : -1;
 }
-- 
2.33.0



More information about the ffmpeg-devel mailing list