mirror of
https://github.com/kkroening/ffmpeg-python.git
synced 2025-04-05 12:48:09 +08:00
#18: add initial graph.py
This commit is contained in:
parent
11a24d0432
commit
241ede2271
@ -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()
|
||||
)
|
||||
```
|
||||
|
47
ffmpeg/graph.py
Normal file
47
ffmpeg/graph.py
Normal file
@ -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')
|
Loading…
x
Reference in New Issue
Block a user