[FFmpeg-devel] [PATCH] Changed metadata print option to accept general urls

sami.hult at gmail.com sami.hult at gmail.com
Wed Jun 29 15:26:11 CEST 2016


From: Sami Hult <sami.hult at gmail.com>

NOTE

This is a new patch against the latest master branch. It fixes an error in code
where "file:-" was directed to file descriptor 2 instead of 1.

I changed mode=print file handling of metadata filter to accept any ffmpeg url.
This way multiple streams can for example be silencedetected and the result directed to different outputs.

Example:

ffmpeg -i test.wav -filter_complex "silencedetect=n=-40dB:d=0.1,ametadata=mode=print:file='pipe\:4'" -f null – 4> test.txt

has the same effect as

ffmpeg -i test.wav -filter_complex "silencedetect=n=-40dB:d=0.1,ametadata=mode=print:file=test.txt" -f null –

Signed-off-by: Sami Hult <sami.hult at gmail.com>
---
 Changelog                |  1 +
 doc/filters.texi         | 23 +++++++++++++++++------
 libavfilter/f_metadata.c | 49 ++++++++++++++++++++++++++++--------------------
 3 files changed, 47 insertions(+), 26 deletions(-)

diff --git a/Changelog b/Changelog
index 4a85925..8541091 100644
--- a/Changelog
+++ b/Changelog
@@ -48,6 +48,7 @@ version 3.1:
 - 10-bit depth support in native utvideo decoder
 - libutvideo wrapper removed
 - YUY2 Lossless Codec decoder
+- Changed metadata print option to accept general urls
 
 
 version 3.0:
diff --git a/doc/filters.texi b/doc/filters.texi
index 3cf3d7c..dba3a5c 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -9375,13 +9375,14 @@ Float representation of @code{value} from metadata key.
 
 @item VALUE2
 Float representation of @code{value} as supplied by user in @code{value} option.
- at end table
 
 @item file
-If specified in @code{print} mode, output is written to the named file. When
-filename equals "-" data is written to standard output.
-If @code{file} option is not set, output is written to the log with AV_LOG_INFO
-loglevel.
+If specified in @code{print} mode, output is written to the named file. Instead of
+plain filename any writable url can be specified. Filename ``-'' is a shorthand
+for standard output. If @code{file} option is not set, output is written to the log
+with AV_LOG_INFO loglevel.
+ at end table
+
 @end table
 
 @subsection Examples
@@ -9391,8 +9392,18 @@ loglevel.
 Print all metadata values for frames with key @code{lavfi.singnalstats.YDIF} with values
 between 0 and 1.
 @example
- at end example
 signalstats,metadata=print:key=lavfi.signalstats.YDIF:value=0:function=expr:expr='between(VALUE1,0,1)'
+ at end example
+ at item
+Print silencedetect output to file @file{metadata.txt}.
+ at example
+silencedetect,ametadata=mode=print:file=metadata.txt
+ at end example
+ at item
+Direct all metadata to a pipe with file descriptor 4.
+ at example
+metadata=mode=print:file='pipe\:4'
+ at end example
 @end itemize
 
 @section mpdecimate
diff --git a/libavfilter/f_metadata.c b/libavfilter/f_metadata.c
index ab07ccf..cac2e93 100644
--- a/libavfilter/f_metadata.c
+++ b/libavfilter/f_metadata.c
@@ -31,6 +31,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/timestamp.h"
+#include "libavformat/avio.h"
 #include "avfilter.h"
 #include "audio.h"
 #include "formats.h"
@@ -80,7 +81,7 @@ typedef struct MetadataContext {
     AVExpr *expr;
     double var_values[VAR_VARS_NB];
 
-    FILE *file;
+    AVIOContext* avio_context;
     char *file_str;
 
     int (*compare)(struct MetadataContext *s,
@@ -180,8 +181,11 @@ static void print_file(AVFilterContext *ctx, const char *msg, ...)
     va_list argument_list;
 
     va_start(argument_list, msg);
-    if (msg)
-        vfprintf(s->file, msg, argument_list);
+    if (msg) {
+        char buf[128];
+        vsnprintf(buf, sizeof(buf), msg, argument_list);
+        avio_put_str(s->avio_context, buf);
+    }
     va_end(argument_list);
 }
 
@@ -236,25 +240,30 @@ static av_cold int init(AVFilterContext *ctx)
         }
     }
 
-    if (s->file_str) {
-        if (!strcmp(s->file_str, "-")) {
-            s->file = stdout;
-        } else {
-            s->file = fopen(s->file_str, "w");
-            if (!s->file) {
-                int err = AVERROR(errno);
-                char buf[128];
-                av_strerror(err, buf, sizeof(buf));
-                av_log(ctx, AV_LOG_ERROR, "Could not open file %s: %s\n",
-                       s->file_str, buf);
-                return err;
-            }
-        }
+    if (s->mode == METADATA_PRINT && s->file_str) {
         s->print = print_file;
     } else {
         s->print = print_log;
     }
 
+    s->avio_context = NULL;
+    if (s->file_str) {
+        printf("Filename: %s", s->file_str);
+        if (!strcmp("-", s->file_str)) {
+            ret = avio_open(&s->avio_context, "pipe:1", AVIO_FLAG_WRITE);
+        } else {
+            ret = avio_open(&s->avio_context, s->file_str, AVIO_FLAG_WRITE);
+        }
+
+        if (ret < 0) {
+            char buf[128];
+            av_strerror(ret, buf, sizeof(buf));
+            av_log(ctx, AV_LOG_ERROR, "Could not open %s: %s\n",
+                   s->file_str, buf);
+            return ret;
+        }
+    }
+
     return 0;
 }
 
@@ -262,9 +271,9 @@ static av_cold void uninit(AVFilterContext *ctx)
 {
     MetadataContext *s = ctx->priv;
 
-    if (s->file && s->file != stdout)
-        fclose(s->file);
-    s->file = NULL;
+    if (s->avio_context) {
+        avio_closep(&s->avio_context);
+    }
 }
 
 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
-- 
2.1.4



More information about the ffmpeg-devel mailing list