[FFmpeg-devel] [PATCH] ffmpeg.c: add an option "detail_stats" to allow ffmpeg to output stats for each stream

Wang Cao doubleecao at gmail.com
Wed Oct 17 01:38:28 EEST 2018


From: Wang Cao <doubleecao at gmail.com>

- Add an option "detail_stats" to ffmpeg to output stats for each
video/audio streams and each ouptut file ffmpeg output log in print_report.
- Make print_final_stats only output stats for one output instead of
aggregate stats.

Signed-off-by: Wang Cao <wangcao at google.com>
---
When creating multiple outputs, the original was to aggregate size of
all output files together and I find this may not be intuitive to
users and choose to output the stats for the first output file instead.
Using -detail_stats to ouptut stats for other output files.

 doc/ffmpeg.texi      |  4 +++
 fftools/ffmpeg.c     | 72 +++++++++++++++++++++++++++++++-------------
 fftools/ffmpeg.h     |  1 +
 fftools/ffmpeg_opt.c |  4 ++-
 4 files changed, 59 insertions(+), 22 deletions(-)

diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index 3717f22d42..53a5be9791 100644
--- a/doc/ffmpeg.texi
+++ b/doc/ffmpeg.texi
@@ -1623,6 +1623,10 @@ rtp stream. (Requires at least one of the output formats to be rtp).
 Allows discarding specific streams or frames of streams at the demuxer.
 Not all demuxers support this.
 
+ at item -detail_stats (@emph{global})
+Allows printing stats for each output stream and output file at the end of
+processing.
+
 @table @option
 @item none
 Discard no frame.
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index da4259a9a8..7286e75c36 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1530,17 +1530,31 @@ static int reap_filters(int flush)
     return 0;
 }
 
-static void print_final_stats(int64_t total_size)
+static void print_final_stats()
 {
     uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
     uint64_t subtitle_size = 0;
     uint64_t data_size = 0;
+    int64_t total_size;
     float percent = -1.0;
     int i, j;
     int pass1_used = 1;
-
-    for (i = 0; i < nb_output_streams; i++) {
+    int nb_display_streams = 0;
+    AVFormatContext *oc;
+    
+    if (nb_output_streams > 0) {
+        nb_display_streams = enable_detail_stats ? nb_output_streams : 1;
+    }
+    for (i = 0; i < nb_display_streams; i++) {
         OutputStream *ost = output_streams[i];
+        if (i > 0 && ost->file_index != output_streams[i-1]->file_index) {
+            video_size = 0;
+            audio_size = 0;
+            extra_size = 0;
+            other_size = 0;
+            subtitle_size = 0;
+            data_size = 0;
+        }
         switch (ost->enc_ctx->codec_type) {
             case AVMEDIA_TYPE_VIDEO: video_size += ost->data_size; break;
             case AVMEDIA_TYPE_AUDIO: audio_size += ost->data_size; break;
@@ -1552,7 +1566,13 @@ static void print_final_stats(int64_t total_size)
         if (   (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
             != AV_CODEC_FLAG_PASS1)
             pass1_used = 0;
-    }
+
+        // Print stats for each output file.
+        if (i == nb_display_streams-1 || ost->file_index != output_streams[i+1]->file_index) {
+            oc = output_files[ost->file_index]->ctx;
+            total_size =  avio_size(oc->pb);
+            if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too
+                total_size = avio_tell(oc->pb);
 
     if (data_size && total_size>0 && total_size >= data_size)
         percent = 100.0 * (total_size - data_size) / data_size;
@@ -1568,6 +1588,8 @@ static void print_final_stats(int64_t total_size)
     else
         av_log(NULL, AV_LOG_INFO, "unknown");
     av_log(NULL, AV_LOG_INFO, "\n");
+        }
+    }
 
     /* print verbose per-stream stats */
     for (i = 0; i < nb_input_files; i++) {
@@ -1680,16 +1702,18 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
 
     t = (cur_time-timer_start) / 1000000.0;
 
-
-    oc = output_files[0]->ctx;
-
-    total_size = avio_size(oc->pb);
-    if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too
-        total_size = avio_tell(oc->pb);
-
     vid = 0;
     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
     av_bprint_init(&buf_script, 0, AV_BPRINT_SIZE_AUTOMATIC);
+
+    /* need to collect final pts upfront because final stats depend on pts*/
+    for (i = 0; i < nb_output_streams; i++) {
+        ost = output_streams[i];
+        /* compute min output value */
+        if (av_stream_get_end_pts(ost->st) != AV_NOPTS_VALUE)
+            pts = FFMAX(pts, av_rescale_q(av_stream_get_end_pts(ost->st),
+                                          ost->st->time_base, AV_TIME_BASE_Q));
+    }
     for (i = 0; i < nb_output_streams; i++) {
         float q = -1;
         ost = output_streams[i];
@@ -1697,12 +1721,14 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
         if (!ost->stream_copy)
             q = ost->quality / (float) FF_QP2LAMBDA;
 
-        if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
+        if (is_last_report && vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
             av_bprintf(&buf, "q=%2.1f ", q);
             av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
                        ost->file_index, ost->index, q);
         }
-        if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
+        // Only print the stats for the first video stream during processing
+        if ((is_last_report && (enable_detail_stats && vid || !vid)) || !is_last_report && !vid) {
+            if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
             float fps;
 
             frame_number = ost->frame_number;
@@ -1753,15 +1779,14 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
                 av_bprintf(&buf_script, "stream_%d_%d_psnr_all=%2.2f\n",
                            ost->file_index, ost->index, p);
             }
-            vid = 1;
         }
-        /* compute min output value */
-        if (av_stream_get_end_pts(ost->st) != AV_NOPTS_VALUE)
-            pts = FFMAX(pts, av_rescale_q(av_stream_get_end_pts(ost->st),
-                                          ost->st->time_base, AV_TIME_BASE_Q));
+            vid = 1;
+
         if (is_last_report)
             nb_frames_drop += ost->last_dropped;
-    }
+            total_size = avio_size(output_files[ost->file_index]->ctx->pb);
+            if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too
+                total_size = avio_tell(output_files[ost->file_index]->ctx->pb);
 
     secs = FFABS(pts) / AV_TIME_BASE;
     us = FFABS(pts) % AV_TIME_BASE;
@@ -1817,8 +1842,13 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
         av_bprintf(&buf_script, "speed=%4.3gx\n", speed);
     }
 
+            if (is_last_report)
+                av_bprintf(&buf, "\n");
+           }
+    }
+
     if (print_stats || is_last_report) {
-        const char end = is_last_report ? '\n' : '\r';
+        const char end = '\r';
         if (print_stats==1 && AV_LOG_INFO > av_log_get_level()) {
             fprintf(stderr, "%s    %c", buf.str, end);
         } else
@@ -1843,7 +1873,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
     }
 
     if (is_last_report)
-        print_final_stats(total_size);
+        print_final_stats();
 }
 
 static void ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParameters *par)
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index eb1eaf6363..275f128435 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -610,6 +610,7 @@ extern char *videotoolbox_pixfmt;
 extern int filter_nbthreads;
 extern int filter_complex_nbthreads;
 extern int vstats_version;
+extern int enable_detail_stats;
 
 extern const AVIOInterruptCB int_cb;
 
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index d4851a2cd8..172459be22 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -110,7 +110,7 @@ float max_error_rate  = 2.0/3;
 int filter_nbthreads = 0;
 int filter_complex_nbthreads = 0;
 int vstats_version = 2;
-
+int enable_detail_stats = 0;
 
 static int intra_only         = 0;
 static int file_overwrite     = 0;
@@ -3545,6 +3545,8 @@ const OptionDef options[] = {
         "this option is deprecated, use the yadif filter instead" },
     { "psnr",         OPT_VIDEO | OPT_BOOL | OPT_EXPERT,                         { &do_psnr },
         "calculate PSNR of compressed frames" },
+    { "detail_stats", OPT_BOOL | OPT_EXPERT,                                     { &enable_detail_stats },
+        "allow stats report for each output" },
     { "vstats",       OPT_VIDEO | OPT_EXPERT ,                                   { .func_arg = opt_vstats },
         "dump video coding statistics to file" },
     { "vstats_file",  OPT_VIDEO | HAS_ARG | OPT_EXPERT ,                         { .func_arg = opt_vstats_file },
-- 
2.19.1.331.ge82ca0e54c-goog



More information about the ffmpeg-devel mailing list