From 8e9fc34242f62860f998b5e59f1eeec74ffdd87f Mon Sep 17 00:00:00 2001 From: Artem Eltyshev Date: Mon, 3 Aug 2020 23:52:41 +0300 Subject: [PATCH] add explicit maps option --- ffmpeg/_run.py | 12 ++++++------ ffmpeg/tests/test_ffmpeg.py | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/ffmpeg/_run.py b/ffmpeg/_run.py index c9cbb7c..49d4416 100644 --- a/ffmpeg/_run.py +++ b/ffmpeg/_run.py @@ -110,7 +110,7 @@ def _get_global_args(node): return list(node.args) -def _get_output_args(node, stream_name_map): +def _get_output_args(node, stream_name_map, explicit_maps): if node.name != output.__name__: raise ValueError('Unsupported output node: {}'.format(node)) args = [] @@ -123,7 +123,7 @@ def _get_output_args(node, stream_name_map): stream_name = _format_input_stream_name( stream_name_map, edge, is_final_arg=True ) - if stream_name != '0' or len(node.incoming_edges) > 1: + if explicit_maps or stream_name != '0' or len(node.incoming_edges) > 1: args += ['-map', stream_name] kwargs = copy.copy(node.kwargs) @@ -147,7 +147,7 @@ def _get_output_args(node, stream_name_map): @output_operator() -def get_args(stream_spec, overwrite_output=False): +def get_args(stream_spec, overwrite_output=False, explicit_maps=False): """Build command-line arguments to be passed to ffmpeg.""" nodes = get_stream_spec_nodes(stream_spec) args = [] @@ -163,7 +163,7 @@ def get_args(stream_spec, overwrite_output=False): if filter_arg: args += ['-filter_complex', filter_arg] args += reduce( - operator.add, [_get_output_args(node, stream_name_map) for node in output_nodes] + operator.add, [_get_output_args(node, stream_name_map, explicit_maps) for node in output_nodes] ) args += reduce(operator.add, [_get_global_args(node) for node in global_nodes], []) if overwrite_output: @@ -172,7 +172,7 @@ def get_args(stream_spec, overwrite_output=False): @output_operator() -def compile(stream_spec, cmd='ffmpeg', overwrite_output=False): +def compile(stream_spec, cmd='ffmpeg', overwrite_output=False, explicit_maps=False): """Build command-line for invoking ffmpeg. The :meth:`run` function uses this to build the command line @@ -187,7 +187,7 @@ def compile(stream_spec, cmd='ffmpeg', overwrite_output=False): cmd = [cmd] elif type(cmd) != list: cmd = list(cmd) - return cmd + get_args(stream_spec, overwrite_output=overwrite_output) + return cmd + get_args(stream_spec, overwrite_output=overwrite_output, explicit_maps=explicit_maps) @output_operator() diff --git a/ffmpeg/tests/test_ffmpeg.py b/ffmpeg/tests/test_ffmpeg.py index 51ee258..e60bcc7 100644 --- a/ffmpeg/tests/test_ffmpeg.py +++ b/ffmpeg/tests/test_ffmpeg.py @@ -577,6 +577,32 @@ def test__merge_outputs(): assert ffmpeg.get_args([out1, out2]) == ['-i', 'in.mp4', 'out2.mp4', 'out1.mp4'] +def test__explicit_maps(): + in_ = ffmpeg.input('in.mp4') + out1 = in_.output('out1.mp4') + out2 = in_.output('out2.mp4') + assert ffmpeg.merge_outputs(out1, out2).get_args(explicit_maps=True) == [ + '-i', + 'in.mp4', + '-map', + '0', + 'out1.mp4', + '-map', + '0', + 'out2.mp4', + ] + assert ffmpeg.get_args([out1, out2], explicit_maps=True) == [ + '-i', + 'in.mp4', + '-map', + '0', + 'out2.mp4', + '-map', + '0', + 'out1.mp4' + ] + + def test__input__start_time(): assert ffmpeg.input('in', ss=10.5).output('out').get_args() == [ '-ss',