ffmpeg options: Enable trailing ? for map_channel
authorpkviet <pkv.stream@gmail.com>
Tue, 22 Aug 2017 09:30:45 +0000 (11:30 +0200)
committerMichael Niedermayer <michael@niedermayer.cc>
Fri, 25 Aug 2017 21:41:11 +0000 (23:41 +0200)
The -map option allows for a trailing ? so that an error is not thrown if
the input stream does not exist.
This capability is extended to the map_channel option.
This allows a ffmpeg command not to break if an input channel does not
exist, which can be of use (for instance, scripts processing audio
channels with sources having unset number of audio channels).

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
doc/ffmpeg.texi
ffmpeg_opt.c
tests/fate/ffmpeg.mak
tests/ref/fate/mapchan-2ch-extract-ch0-ch2-trailing [new file with mode: 0644]
tests/ref/fate/mapchan-3ch-extract-ch0-ch2-trailing [new file with mode: 0644]

index 4616a42..de6d3f1 100644 (file)
@@ -996,7 +996,7 @@ such streams is attempted.
 Allow input streams with unknown type to be copied instead of failing if copying
 such streams is attempted.
 
-@item -map_channel [@var{input_file_id}.@var{stream_specifier}.@var{channel_id}|-1][:@var{output_file_id}.@var{stream_specifier}]
+@item -map_channel [@var{input_file_id}.@var{stream_specifier}.@var{channel_id}|-1][?][:@var{output_file_id}.@var{stream_specifier}]
 Map an audio channel from a given input to an output. If
 @var{output_file_id}.@var{stream_specifier} is not set, the audio channel will
 be mapped on all the audio streams.
@@ -1005,6 +1005,10 @@ Using "-1" instead of
 @var{input_file_id}.@var{stream_specifier}.@var{channel_id} will map a muted
 channel.
 
+A trailing @code{?} will allow the map_channel to be
+optional: if the map_channel matches no channel the map_channel will be ignored instead
+of failing.
+
 For example, assuming @var{INPUT} is a stereo audio file, you can switch the
 two audio channels with the following command:
 @example
@@ -1052,6 +1056,13 @@ video stream), you can use the following command:
 ffmpeg -i input.mkv -filter_complex "[0:1] [0:2] amerge" -c:a pcm_s16le -c:v copy output.mkv
 @end example
 
+To map the first two audio channels from the first input, and using the
+trailing @code{?}, ignore the audio channel mapping if the first input is
+mono instead of stereo:
+@example
+ffmpeg -i INPUT -map_channel 0.0.0 -map_channel 0.0.1? OUTPUT
+@end example
+
 @item -map_metadata[:@var{metadata_spec_out}] @var{infile}[:@var{metadata_spec_in}] (@emph{output,per-metadata})
 Set metadata information of the next output file from @var{infile}. Note that
 those are file indices (zero-based), not filenames.
index 1c4a11e..f275f71 100644 (file)
@@ -405,6 +405,11 @@ static int opt_map_channel(void *optctx, const char *opt, const char *arg)
     int n;
     AVStream *st;
     AudioChannelMap *m;
+    char *allow_unused;
+    char *mapchan;
+    mapchan = av_strdup(arg);
+    if (!mapchan)
+        return AVERROR(ENOMEM);
 
     GROW_ARRAY(o->audio_channel_maps, o->nb_audio_channel_maps);
     m = &o->audio_channel_maps[o->nb_audio_channel_maps - 1];
@@ -415,6 +420,7 @@ static int opt_map_channel(void *optctx, const char *opt, const char *arg)
         m->file_idx = m->stream_idx = -1;
         if (n == 1)
             m->ofile_idx = m->ostream_idx = -1;
+        av_free(mapchan);
         return 0;
     }
 
@@ -450,11 +456,22 @@ static int opt_map_channel(void *optctx, const char *opt, const char *arg)
                m->file_idx, m->stream_idx);
         exit_program(1);
     }
+    /* allow trailing ? to map_channel */
+    if (allow_unused = strchr(mapchan, '?'))
+        *allow_unused = 0;
     if (m->channel_idx < 0 || m->channel_idx >= st->codecpar->channels) {
-        av_log(NULL, AV_LOG_FATAL, "mapchan: invalid audio channel #%d.%d.%d\n",
-               m->file_idx, m->stream_idx, m->channel_idx);
-        exit_program(1);
+        if (allow_unused) {
+            av_log(NULL, AV_LOG_VERBOSE, "mapchan: invalid audio channel #%d.%d.%d\n",
+                    m->file_idx, m->stream_idx, m->channel_idx);
+        } else {
+            av_log(NULL, AV_LOG_FATAL,  "mapchan: invalid audio channel #%d.%d.%d\n"
+                    "To ignore this, add a trailing '?' to the map_channel.\n",
+                    m->file_idx, m->stream_idx, m->channel_idx);
+            exit_program(1);
+        }
+
     }
+    av_free(mapchan);
     return 0;
 }
 
index 0ec5a34..f3fbc84 100644 (file)
@@ -10,6 +10,14 @@ FATE_MAPCHAN-$(CONFIG_CHANNELMAP_FILTER) += fate-mapchan-silent-mono
 fate-mapchan-silent-mono: tests/data/asynth-22050-1.wav
 fate-mapchan-silent-mono: CMD = md5 -i $(TARGET_PATH)/tests/data/asynth-22050-1.wav -map_channel -1 -map_channel 0.0.0 -fflags +bitexact -f wav
 
+FATE_MAPCHAN-$(CONFIG_CHANNELMAP_FILTER) += fate-mapchan-2ch-extract-ch0-ch2-trailing
+fate-mapchan-2ch-extract-ch0-ch2-trailing: tests/data/asynth-44100-2.wav
+fate-mapchan-2ch-extract-ch0-ch2-trailing: CMD = md5 -i $(TARGET_PATH)/tests/data/asynth-44100-2.wav -map_channel 0.0.0 -map_channel 0.0.2? -fflags +bitexact -f wav
+
+FATE_MAPCHAN-$(CONFIG_CHANNELMAP_FILTER) += fate-mapchan-3ch-extract-ch0-ch2-trailing
+fate-mapchan-3ch-extract-ch0-ch2-trailing: tests/data/asynth-44100-3.wav
+fate-mapchan-3ch-extract-ch0-ch2-trailing: CMD = md5 -i $(TARGET_PATH)/tests/data/asynth-44100-3.wav -map_channel 0.0.0 -map_channel 0.0.2? -fflags +bitexact -f wav
+
 FATE_MAPCHAN = $(FATE_MAPCHAN-yes)
 
 FATE_FFMPEG += $(FATE_MAPCHAN)
diff --git a/tests/ref/fate/mapchan-2ch-extract-ch0-ch2-trailing b/tests/ref/fate/mapchan-2ch-extract-ch0-ch2-trailing
new file mode 100644 (file)
index 0000000..a540037
--- /dev/null
@@ -0,0 +1 @@
+b6e1d142b4e484221562e7b66b9bbbdc
diff --git a/tests/ref/fate/mapchan-3ch-extract-ch0-ch2-trailing b/tests/ref/fate/mapchan-3ch-extract-ch0-ch2-trailing
new file mode 100644 (file)
index 0000000..6237a98
--- /dev/null
@@ -0,0 +1 @@
+ae533985186cab287309c04f6b3e866c