PR #21766 opened by Adam Jensen (acj)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21766
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21766.patch

loudnorm provides stats output that's meant to be used for two-pass
normalization. These stats are often interleaved with ffmpeg's stream
descriptions and other output, making them difficult to parse and pass
to the second pass. The new stats_file option enables writing the stats
to a separate file, or to standard output, for simple parsing and other
programmatic usage.

Signed-off-by: Adam Jensen <[email protected]>


>From 9437d3e688fd48b9163e3d649eec500734ee6294 Mon Sep 17 00:00:00 2001
From: Adam Jensen <[email protected]>
Date: Sun, 15 Feb 2026 11:28:25 -0500
Subject: [PATCH] avfilter/af_loudnorm: add stats_file option

loudnorm provides stats output that's meant to be used for two-pass
normalization. These stats are often interleaved with ffmpeg's stream
descriptions and other output, making them difficult to parse and pass
to the second pass. The new stats_file option enables writing the stats
to a separate file, or to standard output, for simple parsing and other
programmatic usage.

Signed-off-by: Adam Jensen <[email protected]>
---
 doc/filters.texi          |  6 ++++++
 libavfilter/af_loudnorm.c | 41 +++++++++++++++++++++++++++++++++++----
 2 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 80a1bda322..9f0218f33a 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -5914,6 +5914,12 @@ Options are true or false. Default is false.
 @item print_format
 Set print format for stats. Options are summary, json, or none.
 Default value is none.
+
+@item stats_file
+If specified, the filter will use the named file to save the stats. The
+format of the file is controlled by @option{print_format}, defaulting to
+json. When @code{-} is given as the filename, stats are written to standard
+output.
 @end table
 
 @section lowpass
diff --git a/libavfilter/af_loudnorm.c b/libavfilter/af_loudnorm.c
index 2636f2245e..23006edb56 100644
--- a/libavfilter/af_loudnorm.c
+++ b/libavfilter/af_loudnorm.c
@@ -20,6 +20,8 @@
 
 /* http://k.ylo.ph/2016/04/04/loudnorm.html */
 
+#include "libavutil/avstring.h"
+#include "libavutil/file_open.h"
 #include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
@@ -64,6 +66,7 @@ typedef struct LoudNormContext {
     int linear;
     int dual_mono;
     enum PrintFormat print_format;
+    char *stats_file_str;
 
     double *buf;
     int buf_size;
@@ -121,6 +124,7 @@ static const AVOption loudnorm_options[] = {
     {     "none",         0,                                   0,              
          AV_OPT_TYPE_CONST,   {.i64 =  NONE},     0,         0,  FLAGS, .unit 
= "print_format" },
     {     "json",         0,                                   0,              
          AV_OPT_TYPE_CONST,   {.i64 =  JSON},     0,         0,  FLAGS, .unit 
= "print_format" },
     {     "summary",      0,                                   0,              
          AV_OPT_TYPE_CONST,   {.i64 =  SUMMARY},  0,         0,  FLAGS, .unit 
= "print_format" },
+    { "stats_file",       "set stats output file",             
OFFSET(stats_file_str),   AV_OPT_TYPE_STRING,  {.str =  NULL},     0,         
0,  FLAGS },
     { NULL }
 };
 
@@ -824,6 +828,8 @@ static av_cold void uninit(AVFilterContext *ctx)
     LoudNormContext *s = ctx->priv;
     double i_in, i_out, lra_in, lra_out, thresh_in, thresh_out, tp_in, tp_out;
     int c;
+    FILE *stats_file = NULL;
+    char *stats = NULL;
 
     if (!s->r128_in || !s->r128_out)
         goto end;
@@ -848,13 +854,31 @@ static av_cold void uninit(AVFilterContext *ctx)
             tp_out = tmp;
     }
 
+
+    if (s->stats_file_str) {
+        if (s->print_format == NONE)
+            s->print_format = JSON;
+
+        if (!strcmp(s->stats_file_str, "-")) {
+            stats_file = stdout;
+        } else {
+            stats_file = avpriv_fopen_utf8(s->stats_file_str, "w");
+            if (!stats_file) {
+                int err = AVERROR(errno);
+                av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
+                       s->stats_file_str, av_err2str(err));
+                goto end;
+            }
+        }
+    }
+
     switch(s->print_format) {
     case NONE:
         break;
 
     case JSON:
-        av_log(ctx, AV_LOG_INFO,
-            "\n{\n"
+        stats = av_asprintf(
+            "{\n"
             "\t\"input_i\" : \"%.2f\",\n"
             "\t\"input_tp\" : \"%.2f\",\n"
             "\t\"input_lra\" : \"%.2f\",\n"
@@ -877,11 +901,13 @@ static av_cold void uninit(AVFilterContext *ctx)
             s->frame_type == LINEAR_MODE ? "linear" : "dynamic",
             s->target_i - i_out
         );
+        av_log(ctx, AV_LOG_INFO, "\n%s", stats);
+        if (stats_file)
+            fprintf(stats_file, "%s", stats);
         break;
 
     case SUMMARY:
-        av_log(ctx, AV_LOG_INFO,
-            "\n"
+        stats = av_asprintf(
             "Input Integrated:   %+6.1f LUFS\n"
             "Input True Peak:    %+6.1f dBTP\n"
             "Input LRA:          %6.1f LU\n"
@@ -905,10 +931,17 @@ static av_cold void uninit(AVFilterContext *ctx)
             s->frame_type == LINEAR_MODE ? "Linear" : "Dynamic",
             s->target_i - i_out
         );
+        av_log(ctx, AV_LOG_INFO, "\n%s", stats);
+        if (stats_file)
+            fprintf(stats_file, "%s", stats);
         break;
     }
 
 end:
+    if (stats)
+        av_free(stats);
+    if (stats_file && stats_file != stdout)
+        fclose(stats_file);
     if (s->r128_in)
         ff_ebur128_destroy(&s->r128_in);
     if (s->r128_out)
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to