Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Adam Jensen via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Adam Jensen <code@ffmpeg.org>
Subject: [FFmpeg-devel] [PR] avfilter/af_loudnorm: add stats_file option (PR #21766)
Date: Sun, 15 Feb 2026 18:30:50 -0000
Message-ID: <177118025086.25.11196673470250710651@009cbcb3d8cd> (raw)

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 <adam@acj.sh>


>From 9437d3e688fd48b9163e3d649eec500734ee6294 Mon Sep 17 00:00:00 2001
From: Adam Jensen <adam@acj.sh>
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 <adam@acj.sh>
---
 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 -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

                 reply	other threads:[~2026-02-15 18:31 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=177118025086.25.11196673470250710651@009cbcb3d8cd \
    --to=ffmpeg-devel@ffmpeg.org \
    --cc=code@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git