-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_filtergraph.py
More file actions
363 lines (317 loc) · 11 KB
/
test_filtergraph.py
File metadata and controls
363 lines (317 loc) · 11 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
from os import path
from tempfile import TemporaryDirectory
from ffmpegio import ffmpegprocess, filtergraph as fgb
from ffmpegio.filtergraph import Chain
from pprint import pprint
import pytest
def test_iter_io_pads():
fg = fgb.Graph(
"color;scale,pad[l1]; crop,pad[l2]; overlay; [l1]overlay; pad,overlay; trim,[l2]overlay"
)
print(fg)
pprint(tuple(fg._iter_io_pads(True, "all")))
pprint(tuple(fg._iter_io_pads(True, "chainable")))
pprint(tuple(fg._iter_io_pads(True, "per_chain")))
def test_resolve_index():
fg = fgb.Graph(
"color;scale,pad[l1]; crop,pad[l2]; overlay; [l1]overlay; pad,overlay[l3]; trim,[l2]overlay,split=3[l4];[l3][l4]overlay"
)
fg.add_label("in", dst=(2, 0, 0))
print(fg)
pprint(tuple(fg._iter_io_pads(True, "all")))
# pprint(tuple(fg._iter_io_pads(False, "all")))
assert fg._resolve_index(True, 0) == (1, 0, 0)
assert fg._resolve_index(True, None) == (1, 0, 0)
assert fg._resolve_index(True, "in") == (2, 0, 0)
assert fg._resolve_index(True, "[in]") == (2, 0, 0)
assert fg._resolve_index(True, 1) == (3, 0, 1)
assert fg._resolve_index(True, (1, None)) == (5, 1, 0)
# pprint(fg._resolve_index(False, 0))
# pprint(fg._resolve_index(False, 1))
@pytest.mark.parametrize(
"fg,fc,left_on,right_on,out",
[
("fps;crop", "trim", None, None, "fps,trim;crop"),
("fps[out];crop", "trim", None, None, "fps,trim;crop"),
("fps;crop", "trim", (1, 0, 0), None, "fps;crop,trim"),
("fps;crop[out]", "trim", "out", None, "fps;crop,trim"),
(
fgb.Graph(["fps", "crop"], {"out": ((None, (1, 0, 0)), (0, 0, 0))}),
"trim",
"out",
None,
None,
),
(
fgb.Graph(["fps", "crop"], {"out": ((None, None), (0, 0, 0))}),
"trim",
"out",
None,
"fps,trim;crop",
),
("fps[L];[L]crop", "trim", None, None, "fps[L];[L]crop,trim"),
("split=2[C];[C]crop", "trim", None, None, "split=2[C][L0];[C]crop;[L0]trim"),
(
"split=2[C][out];[C]crop",
"trim",
"out",
None,
"split=2[C][out];[C]crop;[out]trim",
),
],
)
def test_attach(fg, fc, left_on, right_on, out):
fg = fgb.Graph(fg)
if out is None:
with pytest.raises(fgb.Graph.Error):
fg = fg.attach(fc, left_on, right_on)
else:
fg = fg.attach(fc, left_on, right_on)
assert str(fg) == out
@pytest.mark.parametrize(
"fg,fc,left_on,skip_named,out",
[
("fps;crop", "trim", None, None, "trim,fps;crop"),
("[in]fps;crop", "trim", None, None, "trim,fps;crop"),
("fps;crop", "trim", (1, 0, 0), None, "trim,crop;fps"),
("fps;[in]crop", "trim", "in", None, "trim,crop;fps"),
("[L]fps;crop[L]", "trim", None, None, "trim,crop[L];[L]fps"),
("[C]overlay;crop[C]", "trim", None, None, "trim[L0];[C][L0]overlay;crop[C]"),
(
"[C][in]overlay;crop[C]",
"trim",
"in",
None,
"trim[in];[C][in]overlay;crop[C]",
),
],
)
def test_rattach(fg, fc, left_on, skip_named, out):
fg = fgb.Graph(fg)
if out is None:
with pytest.raises(fgb.Graph.Error):
fg = fg.rattach(fc, left_on, skip_named)
else:
fg = fg.rattach(fc, left_on, skip_named)
assert str(fg) == out
@pytest.mark.parametrize(
"fg, other, auto_link, replace_sws_flags, out",
[
("fps;crop", "trim;scale", False, None, "fps;crop;trim;scale"),
("fps;crop", "trim,scale", False, None, "fps;crop;trim,scale"),
(
"[la]fps;crop[lb]",
"[lb]trim;scale[la]",
False,
None,
"[la1]fps;crop[lb1];[lb2]trim;scale[la2]",
),
(
"[la]fps;crop[lb]",
"[lb]trim;scale[la]",
True,
None,
"[la]fps;crop[lb];[lb]trim;scale[la]",
),
("sws_flags=w=200;fps;crop", "sws_flags=h=400;trim;scale", False, None, None),
(
"sws_flags=w=200;fps;crop",
"sws_flags=h=400;trim;scale",
False,
False,
"sws_flags=w=200;fps;crop;trim;scale",
),
(
"sws_flags=w=200;fps;crop",
"sws_flags=h=400;trim;scale",
False,
True,
"sws_flags=h=400;fps;crop;trim;scale",
),
],
)
def test_stack(fg, other, auto_link, replace_sws_flags, out):
# other, auto_link=False, replace_sws_flags=None,
fg = fgb.Graph(fg)
if out is None:
with pytest.raises(fgb.Graph.Error):
fg = fg.stack(other, auto_link, replace_sws_flags)
else:
fg = fg.stack(other, auto_link, replace_sws_flags)
assert str(fg) == out
@pytest.mark.parametrize(
"fg, id, out",
[
# fmt: off
("fps;crop", (0,0,0), ((0,0,0),None)),
("fps;crop", (1,0,0), ((1,0,0),None)),
("fps;crop", (0,1,0), None),
("fps;crop", 'fake', None),
("[la]fps;crop[lb]", 'la', ((0,0,0),'la')),
("[la]fps;crop[lb]", 'lb', None),
("[a]fps;[a]crop", 'a', None),
("[0:v]fps;[0:v]crop", (0,0,0), None),
# fmt: on
],
)
def test_get_input_pad(fg, id, out):
# other, auto_link=False, replace_sws_flags=None,
fg = fgb.Graph(fg)
if out is None:
with pytest.raises(fgb.Graph.Error):
fg.get_input_pad(id)
else:
assert fg.get_input_pad(id) == out
@pytest.mark.parametrize(
"fg, id, out",
[
# fmt: off
("fps;crop", (0,0,0), ((0,0,0),None)),
("fps;crop", (1,0,0), ((1,0,0),None)),
("fps;crop", (0,1,0), None),
("fps;crop", 'fake', None),
("[la]fps;crop[lb]", 'lb', ((1,0,0),'lb')),
("[la]fps;crop[lb]", 'la', None),
# TODO: test split output case
# fmt: on
],
)
def test_get_output_pad(fg, id, out):
# other, auto_link=False, replace_sws_flags=None,
fg = fgb.Graph(fg)
if out is None:
with pytest.raises(fgb.Graph.Error):
fg.get_output_pad(id)
else:
assert fg.get_output_pad(id) == out
@pytest.mark.parametrize(
"fg, r, to_l,to_r,chain, out",
[
# fmt: off
("[a]fps;crop[b]", "[c]trim;scale[d]", ['b'], ['c'], None, "[a]fps;crop[b];[b]trim;scale[d]"),
("[la]fps;crop[lb]", "[lb]trim;scale[la]", ['lb'], ['lb'], None, "[la1]fps;crop[lb];[lb]trim;scale[la2]"),
("[a]fps;crop[b]", "[c]trim;scale[d]", ['b'], ['c'], True, "[a]fps;crop,trim;scale[d]"),
# fmt: on
],
)
def test_connect(fg, r, to_l, to_r, chain, out):
# other, auto_link=False, replace_sws_flags=None,
fg = fgb.Graph(fg)
if out is None:
with pytest.raises(fgb.Graph.Error):
fg = fg.connect(r, to_l, to_r, chain)
else:
fg = fg.connect(r, to_l, to_r, chain)
assert str(fg) == out
@pytest.mark.parametrize(
"fg, r, how, match_scalar, ignore_labels, out",
[
# fmt: off
("fps;crop", "trim;scale", None, False, False, "fps,trim;crop,scale"),
("[a]fps;crop[b]", "[c]trim;scale[d]", None, False, True, "[a]fps,trim;crop,scale[d]"),
("fps;crop", "trim", None, True, False, "fps,trim;crop,trim"),
("fps", "trim;crop", None, True, False, "fps,trim;fps,crop"),
("fps", "overlay", 'per_chain', False, False, "fps[L0];[L0]overlay"),
("fps", "overlay", 'all', True, False, "fps[L0];fps[L1];[L0][L1]overlay"),
("fps", "overlay", 'chainable', True, False, "fps[L0];[L0]overlay"),
# fmt: on
],
)
def test_join(fg, r, how, match_scalar, ignore_labels, out):
# other, auto_link=False, replace_sws_flags=None,
fg = fgb.Graph(fg)
if out is None:
with pytest.raises(fgb.Graph.Error):
fg = fg.join(r, how, match_scalar, ignore_labels)
else:
fg = fg.join(r, how, match_scalar, ignore_labels)
assert str(fg) == out
# @pytest.mark.parametrize(
# "f1e,f2",
# [
# ("fps", "trim"),
# ("fps", "trim,crop"),
# ("fps", "trim;crop"),
# ("fps,scale", "trim"),
# ("fps,scale", "trim,crop"),
# ("fps,scale", "trim;crop"),
# ("fps;scale", "trim"),
# ("fps;scale", "trim,crop"),
# ("fps;scale", "trim;crop"),
# ],
# )
def test_filter_arithmetics():
fg1 = fgb.trim() + fgb.crop()
assert isinstance(fg1, fgb.Chain)
assert str(fg1) == "trim,crop"
fg2 = fgb.fps() | fgb.scale()
assert isinstance(fg2, fgb.Graph)
assert str(fg2) == "fps;scale"
fg3 = fgb.setpts() * 3
assert isinstance(fg3, fgb.Graph)
assert str(fg3) == "setpts;setpts;setpts"
assert str(("[in]" >> fgb.geq()) >> "[out]") == "[in]geq[out]"
assert str("[in]" >> (fgb.geq() >> "[out]")) == "[in]geq[out]"
fg1 = fgb.trim() >> fgb.crop()
assert str(fg1) == "trim,crop"
fg1 = "trim" >> fgb.crop()
assert str(fg1) == "trim,crop"
def test_filter_empty_handling():
fg1 = fgb.trim() + fgb.crop()
fg2 = fgb.fps() | fgb.scale()
fg3 = fgb.Chain()
fg4 = fgb.Graph()
assert str(fg3 * 2) == ""
assert str(fg4 * 2) == ""
assert str(fg1 + fg3) == "trim,crop"
assert str(fg1 | fg3) == "trim,crop"
assert str(fg2 + fg3) == "fps;scale"
assert str(fg2 | fg3) == "fps;scale"
def test_script():
fg = fgb.Graph("trim=duration=1")
with fg.as_script_file() as script, TemporaryDirectory() as dir:
out = ffmpegprocess.run(
{
"inputs": [(path.join("tests", "assets", "sample.mp4"), None)],
"outputs": [
(path.join(dir, "output.mp4"), {"filter_script:v": script})
],
},
)
assert not out.returncode
def test_ops():
assert str(Chain("scale") + "overlay") == "scale[L0];[L0]overlay"
assert str("scale" + Chain("overlay")) == "scale[L0];[L0]overlay"
if __name__ == "__main__":
from pprint import pprint
from ffmpegio.filtergraph import Graph, filter_info, FFmpegioError, list_filters
for k, v in list_filters().items():
if v.num_inputs is None or v.num_inputs:
continue
info = filter_info(k)
# print(info.options)
d = None
for o in info.options:
if o.name == "duration" or "duration" in o.aliases:
d = o.default
print(f"{k} found {o.name} option with default {d} ")
break
if d is None:
print(
f"{k} has no apparent duration option:\n{[o.name for o in info.options]}"
)
pprint(srcs)
exit()
type = "audio"
expr = "aevalsrc"
fg = Graph(expr)
# if len(fg) != 1 or len(fg[0]) != 1:
# # multi-filter input filtergraph, cannot take arguments
# return expr, args, kwargs
f = fg[0][0]
info = filter_info(f.name)
print(info)
if info.inputs is not None:
raise FFmpegioError(f"{f.name} filter is not a source filter")
opts = info.options
print(len(opts))