[FFmpeg-devel] [PATCH] ffmpeg.c: allow ffmpeg to output stats for each video stream

Wang Cao doubleecao at gmail.com
Sat May 19 02:54:25 EEST 2018


- Make ffmpeg to output stats for each video streams and each ouptut file ffmpeg output log in print_report.
- The report of video/audio sizes is clear as previously all output
video/audio sizes were combined to report and it is unclear such stats
is for one output files or aggregates for all output files.

Signed-off-by: Wang Cao <wangcao at google.com>
---
 fftools/ffmpeg.c | 182 ++++++++++++++++++++++++++---------------------
 1 file changed, 101 insertions(+), 81 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 5a19a09d9a..4aa6c1d3e4 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1526,17 +1526,28 @@ 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;
+    AVFormatContext *oc;
 
     for (i = 0; i < nb_output_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;
@@ -1545,25 +1556,37 @@ static void print_final_stats(int64_t total_size)
         }
         extra_size += ost->enc_ctx->extradata_size;
         data_size  += ost->data_size;
-        if (   (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
+
+        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_output_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;
+                
+            av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB subtitle:%1.0fkB other streams:%1.0fkB global headers:%1.0fkB muxing overhead: ",
+                   video_size / 1024.0,
+                   audio_size / 1024.0,
+                   subtitle_size / 1024.0,
+                   other_size / 1024.0,
+                   extra_size / 1024.0);
+                   
+            if (percent >= 0.0)
+                av_log(NULL, AV_LOG_INFO, "%f%%", percent);
+            else
+                av_log(NULL, AV_LOG_INFO, "unknown");
+            av_log(NULL, AV_LOG_INFO, "\n");
+        }
     }
 
-    if (data_size && total_size>0 && total_size >= data_size)
-        percent = 100.0 * (total_size - data_size) / data_size;
 
-    av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB subtitle:%1.0fkB other streams:%1.0fkB global headers:%1.0fkB muxing overhead: ",
-           video_size / 1024.0,
-           audio_size / 1024.0,
-           subtitle_size / 1024.0,
-           other_size / 1024.0,
-           extra_size / 1024.0);
-    if (percent >= 0.0)
-        av_log(NULL, AV_LOG_INFO, "%f%%", percent);
-    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++) {
@@ -1676,13 +1699,6 @@ 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, 1);
@@ -1693,12 +1709,12 @@ 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) {
+        if (enc->codec_type == AVMEDIA_TYPE_VIDEO && (is_last_report || !is_last_report && !vid)) {
             float fps;
 
             frame_number = ost->frame_number;
@@ -1750,75 +1766,80 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
                            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),
+        
+            /* 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));
-        if (is_last_report)
-            nb_frames_drop += ost->last_dropped;
-    }
+            if (is_last_report)
+                nb_frames_drop += ost->last_dropped;
 
-    secs = FFABS(pts) / AV_TIME_BASE;
-    us = FFABS(pts) % AV_TIME_BASE;
-    mins = secs / 60;
-    secs %= 60;
-    hours = mins / 60;
-    mins %= 60;
-    hours_sign = (pts < 0) ? "-" : "";
+            total_size = ost->data_size;
 
-    bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
-    speed = t != 0.0 ? (double)pts / AV_TIME_BASE / t : -1;
+            secs = FFABS(pts) / AV_TIME_BASE;
+            us = FFABS(pts) % AV_TIME_BASE;
+            mins = secs / 60;
+            secs %= 60;
+            hours = mins / 60;
+            mins %= 60;
+            hours_sign = (pts < 0) ? "-" : "";
 
-    if (total_size < 0) av_bprintf(&buf, "size=N/A time=");
-    else                av_bprintf(&buf, "size=%8.0fkB time=", total_size / 1024.0);
-    if (pts == AV_NOPTS_VALUE) {
-        av_bprintf(&buf, "N/A ");
-    } else {
-        av_bprintf(&buf, "%s%02d:%02d:%02d.%02d ",
-                   hours_sign, hours, mins, secs, (100 * us) / AV_TIME_BASE);
-    }
+            bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
+            speed = t != 0.0 ? (double)pts / AV_TIME_BASE / t : -1;
 
-    if (bitrate < 0) {
-        av_bprintf(&buf, "bitrate=N/A");
-        av_bprintf(&buf_script, "bitrate=N/A\n");
-    }else{
-        av_bprintf(&buf, "bitrate=%6.1fkbits/s", bitrate);
-        av_bprintf(&buf_script, "bitrate=%6.1fkbits/s\n", bitrate);
-    }
+            if (total_size < 0) av_bprintf(&buf, "size=N/A time=");
+            else                av_bprintf(&buf, "size=%8.0fkB time=", total_size / 1024.0);
+            if (pts == AV_NOPTS_VALUE) {
+                av_bprintf(&buf, "N/A ");
+            } else {
+                av_bprintf(&buf, "%s%02d:%02d:%02d.%02d ",
+                           hours_sign, hours, mins, secs, (100 * us) / AV_TIME_BASE);
+            }
 
-    if (total_size < 0) av_bprintf(&buf_script, "total_size=N/A\n");
-    else                av_bprintf(&buf_script, "total_size=%"PRId64"\n", total_size);
-    if (pts == AV_NOPTS_VALUE) {
-        av_bprintf(&buf_script, "out_time_ms=N/A\n");
-        av_bprintf(&buf_script, "out_time=N/A\n");
-    } else {
-        av_bprintf(&buf_script, "out_time_ms=%"PRId64"\n", pts);
-        av_bprintf(&buf_script, "out_time=%s%02d:%02d:%02d.%06d\n",
-                   hours_sign, hours, mins, secs, us);
-    }
+            if (bitrate < 0) {
+                av_bprintf(&buf, "bitrate=N/A");
+                av_bprintf(&buf_script, "bitrate=N/A\n");
+            }else{
+                av_bprintf(&buf, "bitrate=%6.1fkbits/s", bitrate);
+                av_bprintf(&buf_script, "bitrate=%6.1fkbits/s\n", bitrate);
+            }
+
+            if (total_size < 0) av_bprintf(&buf_script, "total_size=N/A\n");
+            else                av_bprintf(&buf_script, "total_size=%"PRId64"\n", total_size);
+            if (pts == AV_NOPTS_VALUE) {
+                av_bprintf(&buf_script, "out_time_ms=N/A\n");
+                av_bprintf(&buf_script, "out_time=N/A\n");
+            } else {
+                av_bprintf(&buf_script, "out_time_ms=%"PRId64"\n", pts);
+                av_bprintf(&buf_script, "out_time=%s%02d:%02d:%02d.%06d\n",
+                           hours_sign, hours, mins, secs, us);
+            }
 
-    if (nb_frames_dup || nb_frames_drop)
-        av_bprintf(&buf, " dup=%d drop=%d", nb_frames_dup, nb_frames_drop);
-    av_bprintf(&buf_script, "dup_frames=%d\n", nb_frames_dup);
-    av_bprintf(&buf_script, "drop_frames=%d\n", nb_frames_drop);
+            if (nb_frames_dup || nb_frames_drop)
+                av_bprintf(&buf, " dup=%d drop=%d", nb_frames_dup, nb_frames_drop);
+            av_bprintf(&buf_script, "dup_frames=%d\n", nb_frames_dup);
+            av_bprintf(&buf_script, "drop_frames=%d\n", nb_frames_drop);
 
-    if (speed < 0) {
-        av_bprintf(&buf, " speed=N/A");
-        av_bprintf(&buf_script, "speed=N/A\n");
-    } else {
-        av_bprintf(&buf, " speed=%4.3gx", speed);
-        av_bprintf(&buf_script, "speed=%4.3gx\n", speed);
+            if (speed < 0) {
+                av_bprintf(&buf, " speed=N/A");
+                av_bprintf(&buf_script, "speed=N/A\n");
+            } else {
+                av_bprintf(&buf, " speed=%4.3gx", speed);
+                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
             av_log(NULL, AV_LOG_INFO, "%s    %c", buf.str, end);
-
-    fflush(stderr);
+        fflush(stderr);
     }
     av_bprint_finalize(&buf, NULL);
 
@@ -1835,9 +1856,8 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
                        "Error closing progress log, loss of information possible: %s\n", av_err2str(ret));
         }
     }
-
     if (is_last_report)
-        print_final_stats(total_size);
+        print_final_stats();
 }
 
 static void ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParameters *par)
-- 
2.17.0.441.gb46fe60e1d-goog



More information about the ffmpeg-devel mailing list