[FFmpeg-devel] [PATCH] audio filter 'merge them all'

Gaullier Nicolas nicolas.gaullier at arkena.com
Mon Jun 30 18:37:06 CEST 2014


This patch makes it possible to use 'amerge' filter with 'inputs=-1' to merge 'all' audio input streams.
The 'killer use case' is to batch-convert input files with varying/unknown input audio mappings.

Ex:
ffmpeg (...) -filter_complex "amerge=inputs=-1[a]" -map 0:v -map [a] -d10_channelcount 8 -f mxf_d10 d:\output-naudios.mxf

It is possible with a unique command line to process input files with 1 or several mono tracks, 1 or several stereo tracks, and more.

Nicolas

diff --git a/ffmpeg_filter.c b/ffmpeg_filter.c
index 50ee422..a7c2a1e 100644
--- a/ffmpeg_filter.c
+++ b/ffmpeg_filter.c
@@ -843,6 +843,14 @@ int configure_filtergraph(FilterGraph *fg)
     int ret, i, init = !fg->graph, simple = !fg->graph_desc;
     const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
                                       fg->graph_desc;
+    InputStream *ist;
+
+    avfilter_nb_input_audio_streams = 0;
+    for (i = 0; i < nb_input_streams; i++) {
+        ist = input_streams[i];
+        if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO)
+            avfilter_nb_input_audio_streams++;
+    }
 
     avfilter_graph_free(&fg->graph);
     if (!(fg->graph = avfilter_graph_alloc()))
diff --git a/libavfilter/af_amerge.c b/libavfilter/af_amerge.c
index 591d672..bca0edd 100644
--- a/libavfilter/af_amerge.c
+++ b/libavfilter/af_amerge.c
@@ -51,7 +51,7 @@ typedef struct {
 
 static const AVOption amerge_options[] = {
     { "inputs", "specify the number of inputs", OFFSET(nb_inputs),
-      AV_OPT_TYPE_INT, { .i64 = 2 }, 2, SWR_CH_MAX, FLAGS },
+      AV_OPT_TYPE_INT, { .i64 = 2 }, -1, SWR_CH_MAX, FLAGS },
     { NULL }
 };
 
@@ -308,6 +308,9 @@ static av_cold int init(AVFilterContext *ctx)
     AMergeContext *am = ctx->priv;
     int i;
 
+    if (am->nb_inputs == -1)
+        am->nb_inputs = avfilter_nb_input_audio_streams;
+
     am->in = av_calloc(am->nb_inputs, sizeof(*am->in));
     if (!am->in)
         return AVERROR(ENOMEM);
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index 7e166e0..fc4f29d 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -1166,3 +1166,5 @@ const AVClass *avfilter_get_class(void)
 {
     return &avfilter_class;
 }
+
+int avfilter_nb_input_audio_streams = 0;
diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h
index b5220b9..e1f452f 100644
--- a/libavfilter/avfilter.h
+++ b/libavfilter/avfilter.h
@@ -1525,6 +1525,12 @@ char *avfilter_graph_dump(AVFilterGraph *graph, const char *options);
 int avfilter_graph_request_oldest(AVFilterGraph *graph);
 
 /**
+ * The total number of input audio streams
+ * Enable filters to configure automatically (ex: merge "all" audios)
+ */
+extern int avfilter_nb_input_audio_streams;
+
+/**
  * @}
  */



More information about the ffmpeg-devel mailing list