diff --git a/ffmpeg/_utils.py b/ffmpeg/_utils.py index 20eb1af..06bd3b8 100644 --- a/ffmpeg/_utils.py +++ b/ffmpeg/_utils.py @@ -76,3 +76,8 @@ def escape_chars(text, chars): for ch in chars: text = text.replace(ch, '\\' + ch) return text + + +def sort_ordereddict(d): + for key in sorted(d.keys()): + d[key] = d.pop(key) diff --git a/ffmpeg/dag.py b/ffmpeg/dag.py index 335a060..e4ba116 100644 --- a/ffmpeg/dag.py +++ b/ffmpeg/dag.py @@ -1,8 +1,8 @@ from __future__ import unicode_literals -from ._utils import get_hash, get_hash_int +from ._utils import get_hash, get_hash_int, sort_ordereddict from builtins import object -from collections import namedtuple +from collections import namedtuple, OrderedDict class DagNode(object): @@ -162,7 +162,7 @@ def topo_sort(downstream_nodes): raise RuntimeError('Graph is not a DAG') if downstream_node is not None: - outgoing_edge_map = outgoing_edge_maps.get(upstream_node, {}) + outgoing_edge_map = outgoing_edge_maps.get(upstream_node, OrderedDict()) outgoing_edge_infos = outgoing_edge_map.get(upstream_label, []) outgoing_edge_infos += [(downstream_node, downstream_label, downstream_selector)] outgoing_edge_map[upstream_label] = outgoing_edge_infos @@ -179,4 +179,9 @@ def topo_sort(downstream_nodes): while unmarked_nodes: upstream_node, upstream_label = unmarked_nodes.pop() visit(upstream_node, upstream_label, None, None) + + # Sort outgoing edge maps by upstream label + for map in outgoing_edge_maps.values(): + sort_ordereddict(map) + return sorted_nodes, outgoing_edge_maps diff --git a/ffmpeg/nodes.py b/ffmpeg/nodes.py index e861822..ab775c9 100644 --- a/ffmpeg/nodes.py +++ b/ffmpeg/nodes.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +from collections import OrderedDict + from .dag import KwargReprNode from ._utils import escape_chars, get_hash_int from builtins import object @@ -69,7 +71,7 @@ def get_stream_map(stream_spec): elif isinstance(stream_spec, Stream): stream_map = {None: stream_spec} elif isinstance(stream_spec, (list, tuple)): - stream_map = dict(enumerate(stream_spec)) + stream_map = OrderedDict(enumerate(stream_spec)) elif isinstance(stream_spec, dict): stream_map = stream_spec return stream_map @@ -108,7 +110,7 @@ class Node(KwargReprNode): @classmethod def __get_incoming_edge_map(cls, stream_map): - incoming_edge_map = {} + incoming_edge_map = OrderedDict() for downstream_label, upstream in list(stream_map.items()): incoming_edge_map[downstream_label] = (upstream.node, upstream.label, upstream.selector) return incoming_edge_map