From 241ede2271a05236613561436916dcf38597b102 Mon Sep 17 00:00:00 2001 From: Karl Kroening Date: Wed, 5 Jul 2017 04:23:35 -0600 Subject: [PATCH] #18: add initial `graph.py` --- README.md | 2 +- ffmpeg/graph.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 ffmpeg/graph.py diff --git a/README.md b/README.md index d65b8c3..86976c1 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ overlay_file = ffmpeg.input('overlay.png') ) .overlay(overlay_file.hflip()) .drawbox(50, 50, 120, 120, color='red', thickness=5) - .output(TEST_OUTPUT_FILE) + .output('out.mp4') .run() ) ``` diff --git a/ffmpeg/graph.py b/ffmpeg/graph.py new file mode 100644 index 0000000..9e220df --- /dev/null +++ b/ffmpeg/graph.py @@ -0,0 +1,47 @@ +from __future__ import unicode_literals + +from .dag import get_outgoing_edges +from ._run import topo_sort +import graphviz +import os + +from ffmpeg.nodes import ( + InputNode, + OutputNode, + FilterNode, + operator, +) + + +@operator() +def view(*downstream_nodes, **kwargs): + sorted_nodes, outgoing_edge_maps = topo_sort(downstream_nodes) + graph = graphviz.Digraph() + graph.attr(rankdir='LR') + show_labels = kwargs.pop('show_labels', False) + if len(kwargs.keys()) != 0: + raise ValueError('Invalid kwargs key(s): {}'.format(', '.join(kwargs.keys()))) + + for node in sorted_nodes: + name = node.name + if '_kwargs' in dir(node) and 'filename' in node._kwargs: + name = os.path.basename(node._kwargs['filename']) + if isinstance(node, InputNode): + color = '#99cc00' + elif isinstance(node, OutputNode): + color = '#99ccff' + elif isinstance(node, FilterNode): + color = '#ffcc00' + else: + color = None + graph.node(str(hash(node)), name, shape='box', style='filled', fillcolor=color) + outgoing_edge_map = outgoing_edge_maps.get(node, {}) + for edge in get_outgoing_edges(node, outgoing_edge_map): + kwargs = {} + if show_labels: + kwargs['label'] = '{} -> {}'.format(edge.upstream_label, edge.downstream_label) + upstream_node_id = str(hash(edge.upstream_node)) + downstream_node_id = str(hash(edge.downstream_node)) + graph.edge(upstream_node_id, downstream_node_id, **kwargs) + + graph.view('graph.png')