-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.go
296 lines (255 loc) · 8.12 KB
/
executor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package ffmpegtree
import (
"fmt"
"strings"
)
// Chain is a singly linked chain of IFilterNode's which is written seperated by ',' in ffmpeg commands
type Chain []IFilterNode
// ToString returns string representation of the chain of filters seperated by ','
func (c Chain) ToString(includeOutStream bool) string {
filterStrings := make([]string, 0)
for i := len(c) - 1; i >= 0; i-- {
filterStrings = append(filterStrings, FilterNodeToStr(c[i]))
}
// if ends with split
res := ""
if split, ok := c[0].(*SplitNode); ok {
outs := ""
for i := 0; i < split.fanOut; i++ {
outs += fmt.Sprintf("[%v]", split.GetOutStreamName())
}
res = strings.Join(filterStrings, ",") + outs
} else {
res = strings.Join(filterStrings, ",")
if includeOutStream {
res += fmt.Sprintf("[%v]", c[0].GetOutStreamName())
}
}
return res
}
type FFmpegExecutor struct {
acc []string
inputs []IInputNode
visited map[string]bool
dependents *DependentsMap
q []INode
maps []IMap
outName string
outOptions []string
}
func (e *FFmpegExecutor) ToFfmpeg(nodes ...INode) FfmpegCommand {
for _, node := range nodes {
// find all IInputNode and assign their indexes. it will affect the order they show up in the output
e.setInputIdx(node)
// first preprocess tree
// insert select stream nodes where it is missing
e.insertSelectStream(node)
// insert split nodes if a stream is input to more than one node
e.insertSplit(node)
}
// each node can access its inputs but cannot access to nodes which depends on itself. traverse tree
// and save dependencies in a hashmap structure. it will be useful while executing tree.
e.dependents = GetDependents(nodes...)
// start traversal from root node
e.q = nodes
r := e.toFfmpeg()
// generate input options which are in the form of "-i ***.mp4"
inputs := make([]string, 0, len(e.inputs))
for _, input := range e.inputs {
inputs = append(inputs, input.ToString()...)
}
// generate map options which are in the form of "-map '0:0'" or "-map '[var_1:0]'"
maps := make([]string, 0, len(e.maps))
for _, iMap := range e.maps {
maps = append(maps, iMap.ToString()...)
}
// put it all together
res := make([]string, 0)
res = append(res, inputs...)
res = append(res, "-filter_complex", r)
res = append(res, maps...)
res = append(res, e.outOptions...)
res = append(res, e.outName)
return res
}
func (e *FFmpegExecutor) toFfmpeg() string {
// NOTE: we are traversing tree in bfs manner and as lons as each node only has 1 dependents, it normally guarentees that a filter or a stream variable
// shows up before all of its dependents in the output, avoiding 'forward declarations, which ffmpeg script does not support'.
// It is not guaranteed for split nodes because they could have more than one dependents. A split node may have a child node
// which still has a dependent unprocessed node. To guarantee that the node which is splitted comes before all of its
// dependents, we need to process its split node after all of its dependents are queued.
// nodeEncounters is a mapping from nodes to their fan out number(dependent count). Each time a node is popped
// up from queue, its corresponding mapping is decreased by one and when it hits 0, it means the last dependent
// is processed hence we can proceed to process the current node and add it to output
nodeEncounters := make(map[string]int)
for len(e.q) > 0 {
tree := e.q[0]
e.q = e.q[1:]
if e.visited[tree.GetID()] {
continue
}
switch tree.(type) {
case IFilterNode:
if _, ok := nodeEncounters[tree.GetID()]; !ok {
nodeEncounters[tree.GetID()] = len(e.dependents.Get(tree))
}
if nodeEncounters[tree.GetID()] = nodeEncounters[tree.GetID()] - 1; nodeEncounters[tree.GetID()] > 0 {
// if not 0 delay processing of the node since it might still have unprocessed dependents
continue
}
// every IFilterNode can be treated as a chain of IFilterNode's even if it consists of only one node
c, newTree := e.toChain(tree)
e.visited[tree.GetID()] = true
tree = newTree
f := ""
for _, node := range tree.GetInputs() {
parentFilterNode, ok := node.(IFilterNode)
if !ok {
ssn := node.(ISelectStreamNode)
f += ssn.GetOutStreamName()
continue
}
f += fmt.Sprintf("[%v]", parentFilterNode.GetOutStreamName())
}
f += c.ToString(len(e.dependents.Get(c[0])) > 0 || e.isMapped(c[0]))
e.acc = append([]string{f}, e.acc...)
case IInputNode:
e.visited[tree.GetID()] = true
}
e.q = append(e.q, tree.GetInputs()...)
}
return strings.Join(e.acc, ";")
}
// toChain creates a chain of IFilterNode's by adding given node then, starts to follow every node's inputs and adds it to chain
// if the node has only one child and one dependency
func (e *FFmpegExecutor) toChain(tree INode) (c Chain, ret INode) {
stack := make(Chain, 0)
stack = append(stack, tree.(IFilterNode))
for ; len(tree.GetInputs()) == 1; tree = tree.GetInputs()[0] {
curr := tree.GetInputs()[0]
if e.visited[curr.GetID()] || len(e.dependents.Get(curr)) > 1 {
break
}
fn, ok := curr.(IFilterNode)
if !ok {
break
}
stack = append(stack, fn)
}
return stack, stack[len(stack)-1]
}
// insertSelectStream traverses the graph and inserts an ISelectStreamNode when an IInputNode is directly fed into a
// IFilterNode. Default selected stream is '0'
func (e *FFmpegExecutor) insertSelectStream(t INode) {
d := GetDependents(t)
for _, s := range d.Keys() {
nodes := d.Get(s)
in, ok := s.(IInputNode)
if !ok {
continue
}
ssn := NewSelectStreamNode(in, 0)
for _, node := range nodes {
_, ok := node.(IFilterNode)
if ok {
// if input is directly connected to an IFilterNode insert ssn in between
inps := node.GetInputs()
i := 0
for ; i < len(inps); i++ {
if inps[i].GetID() == s.GetID() {
break
}
}
inps[i] = ssn
node.SetInputs(inps)
}
}
}
}
// insertSplit traverses the graph and inserts an ISplitNode if a stream is used as input for more than one times.
// This is required by ffmpeg syntax.
func (e *FFmpegExecutor) insertSplit(t INode) {
d := GetDependents(t)
for _, currNode := range d.Keys() {
if _, ok := currNode.(ISplitNode); ok {
continue
}
dependents := d.Get(currNode)
_, ok := currNode.(IInputNode) // input node can be input to more than once without splitting
if len(dependents) > 1 && !ok {
split := NewSplitNode(currNode, len(dependents))
for _, node := range dependents {
inps := node.GetInputs()
i := 0
for ; i < len(inps); i++ {
if inps[i].GetID() == currNode.GetID() {
break
}
}
inps[i] = split
node.SetInputs(inps)
}
}
}
}
// setInputIdx traverses the graph from a given node and discovers all input nodes and assign them an index.
func (e *FFmpegExecutor) setInputIdx(t INode) {
if i, ok := t.(IInputNode); ok && !e.isInInputs(i) {
e.inputs = append(e.inputs, i)
return
}
d := GetDependents(t)
for _, s := range d.Keys() {
in, ok := s.(IInputNode)
if ok && !e.isInInputs(in) {
in.SetInputIdx(len(e.inputs))
e.inputs = append(e.inputs, in)
}
}
// add input nodes which are mapped but is not used in graph at all
for _, iMap := range e.maps {
n := iMap.GetStreamNode()
in, ok := n.(IInputNode)
if ok && d.Get(in) == nil && !e.isInInputs(in) {
in.SetInputIdx(len(e.inputs))
e.inputs = append(e.inputs, in)
}
}
}
func (e *FFmpegExecutor) isMapped(n INode) bool {
for _, m := range e.maps {
if m.GetStreamNode().GetID() == n.GetID() {
return true
}
}
return false
}
func (e *FFmpegExecutor) isInInputs(n IInputNode) bool {
for _, inp := range e.inputs {
if inp.GetID() == n.GetID() {
return true
}
}
return false
}
func NewFfmpegExecutor(maps []IMap, outName string, outOptions []string) *FFmpegExecutor {
return &FFmpegExecutor{
acc: nil,
inputs: nil,
visited: make(map[string]bool),
dependents: NewDependentsMap(),
q: nil,
maps: maps,
outName: outName,
outOptions: outOptions,
}
}
type FfmpegCommand []string
func (cmd *FfmpegCommand) FilterComplex() string {
for i, arg := range *cmd {
if strings.Contains(arg, "filter_complex") {
return []string(*cmd)[i+1]
}
}
return ""
}