Implement SourceNode

This commit is contained in:
Davide Depau 2018-01-25 12:25:20 +01:00 committed by Silvio Tomatis
parent 4cb7d26f55
commit 63d660f129
3 changed files with 71 additions and 22 deletions

View File

@ -10,7 +10,7 @@ from .nodes import (
MergeOutputsNode,
OutputNode,
output_operator,
)
SourceNode)
def input(filename, **kwargs):
@ -32,6 +32,30 @@ def input(filename, **kwargs):
return InputNode(input.__name__, kwargs=kwargs).stream()
def source_multi_output(filter_name, *args, **kwargs):
"""Apply custom filter with one or more outputs.
This is the same as ``filter_`` except that the filter can produce more than one output.
To reference an output stream, use either the ``.stream`` operator or bracket shorthand:
Example:
```
split = ffmpeg.input('in.mp4').filter_multi_output('split')
split0 = split.stream(0)
split1 = split[1]
ffmpeg.concat(split0, split1).output('out.mp4').run()
```
"""
return SourceNode(filter_name, args=args, kwargs=kwargs)
def source(filter_name, *args, **kwargs):
return source_multi_output(filter_name, *args, **kwargs).stream()
@output_operator()
def global_args(stream, *args):
"""Add extra global command-line argument(s), e.g. ``-progress``.
@ -94,4 +118,11 @@ def output(*streams_and_filename, **kwargs):
return OutputNode(streams, output.__name__, kwargs=kwargs).stream()
__all__ = ['input', 'merge_outputs', 'output', 'overwrite_output']
__all__ = [
'input',
'source_multi_output',
'source',
'merge_outputs',
'output',
'overwrite_output',
]

View File

@ -16,7 +16,7 @@ from .nodes import (
InputNode,
OutputNode,
output_operator,
)
SourceNode)
class Error(Exception):
@ -156,9 +156,10 @@ def get_args(stream_spec, overwrite_output=False):
input_nodes = [node for node in sorted_nodes if isinstance(node, InputNode)]
output_nodes = [node for node in sorted_nodes if isinstance(node, OutputNode)]
global_nodes = [node for node in sorted_nodes if isinstance(node, GlobalNode)]
filter_nodes = [node for node in sorted_nodes if isinstance(node, FilterNode)]
filter_nodes = [node for node in sorted_nodes if isinstance(node, (FilterNode, SourceNode))]
stream_name_map = {(node, None): str(i) for i, node in enumerate(input_nodes)}
filter_arg = _get_filter_arg(filter_nodes, outgoing_edge_maps, stream_name_map)
if len(input_nodes) > 0:
args += reduce(operator.add, [_get_input_args(node) for node in input_nodes])
if filter_arg:
args += ['-filter_complex', filter_arg]

View File

@ -234,9 +234,7 @@ class Node(KwargReprNode):
class FilterableStream(Stream):
def __init__(self, upstream_node, upstream_label, upstream_selector=None):
super(FilterableStream, self).__init__(
upstream_node, upstream_label, {InputNode, FilterNode}, upstream_selector
)
super(FilterableStream, self).__init__(upstream_node, upstream_label, {InputNode, FilterNode, SourceNode}, upstream_selector)
# noinspection PyMethodOverriding
@ -261,20 +259,8 @@ class InputNode(Node):
# noinspection PyMethodOverriding
class FilterNode(Node):
def __init__(self, stream_spec, name, max_inputs=1, args=[], kwargs={}):
super(FilterNode, self).__init__(
stream_spec=stream_spec,
name=name,
incoming_stream_types={FilterableStream},
outgoing_stream_type=FilterableStream,
min_inputs=1,
max_inputs=max_inputs,
args=args,
kwargs=kwargs,
)
"""FilterNode"""
class FilterableNode(Node):
"""FilterableNode"""
def _get_filter(self, outgoing_edges):
args = self.args
@ -300,6 +286,37 @@ class FilterNode(Node):
return escape_chars(params_text, '\\\'[],;')
# noinspection PyMethodOverriding
class FilterNode(FilterableNode):
"""FilterNode"""
def __init__(self, stream_spec, name, max_inputs=1, args=[], kwargs={}):
super(FilterNode, self).__init__(
stream_spec=stream_spec,
name=name,
incoming_stream_types={FilterableStream},
outgoing_stream_type=FilterableStream,
min_inputs=1,
max_inputs=max_inputs,
args=args,
kwargs=kwargs,
)
# noinspection PyMethodOverriding
class SourceNode(FilterableNode):
def __init__(self, name, args=[], kwargs={}):
super(SourceNode, self).__init__(
stream_spec=None,
name=name,
incoming_stream_types={},
outgoing_stream_type=FilterableStream,
min_inputs=0,
max_inputs=0,
args=args,
kwargs=kwargs
)
# noinspection PyMethodOverriding
class OutputNode(Node):
def __init__(self, stream, name, args=[], kwargs={}):