Skip to content

Commit f75ca3c

Browse files
committed
filtergraph-other_index support by rshift
1 parent 28c766f commit f75ca3c

3 files changed

Lines changed: 98 additions & 18 deletions

File tree

src/ffmpegio/filtergraph.py

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -757,9 +757,15 @@ def __rshift__(self, other):
757757

758758
# resolve the index
759759
if type(other) == tuple:
760-
other, index = other
760+
if len(other) > 2:
761+
index, other_index, other = other
762+
else:
763+
index, other = other
764+
other_index = None
761765
else:
762766
index = None
767+
other_index = None
768+
763769
index = self._resolve_index(False, index)
764770

765771
# if label
@@ -771,10 +777,14 @@ def __rshift__(self, other):
771777

772778
# if other is Filter object, do add operation
773779
try:
774-
other = as_filter(other)
780+
other = as_filtergraph_object(other)
775781
except:
776782
return NotImplemented
777783

784+
# if not Chain or Graph, use other's >> operator
785+
if not isinstance(other, Filter):
786+
return other.__rrshift__((self, index, other_index))
787+
778788
if other.get_num_inputs() == 0:
779789
raise FiltergraphMismatchError(self.get_num_outputs(), 0)
780790

@@ -790,9 +800,15 @@ def __rrshift__(self, other):
790800

791801
# resolve the index
792802
if type(other) == tuple:
793-
index, other = other
803+
if len(other) > 2:
804+
other, other_index, index = other
805+
else:
806+
other, index = other
807+
other_index = None
794808
else:
795809
index = None
810+
other_index = None
811+
796812
index = self._resolve_index(True, index)
797813

798814
# if label
@@ -804,10 +820,14 @@ def __rrshift__(self, other):
804820

805821
# if other is Filter object, do add operation
806822
try:
807-
other = as_filter(other)
823+
other = as_filtergraph_object(other)
808824
except:
809825
return NotImplemented
810826

827+
# if not Chain or Graph, use other's >> operator
828+
if not isinstance(other, Filter):
829+
return other.__rshift__((other_index, index, self))
830+
811831
if other.get_num_outputs() == 0:
812832
raise FiltergraphMismatchError(0, self.get_num_inputs())
813833

@@ -960,11 +980,20 @@ def __ror__(self, other):
960980
return Graph([other, self]) if n and m else self if n else other
961981

962982
def __rshift__(self, other):
963-
"""self >> other | self >> (index, other)"""
983+
984+
if type(other) == tuple:
985+
if len(other) > 2:
986+
index, other_index, other = other
987+
else:
988+
index, other = other
989+
other_index = None
990+
else:
991+
index = None
992+
other_index = None
964993

965994
# resolve the index
966995
if type(other) == tuple:
967-
other, index = other
996+
index, other = other
968997
else:
969998
index = None
970999

@@ -992,10 +1021,13 @@ def __rshift__(self, other):
9921021

9931022
# if other is Filter object, do add operation
9941023
try:
995-
other = as_filterchain(other)
1024+
other = as_filtergraph_object(other)
9961025
except:
9971026
return NotImplemented
9981027

1028+
if isinstance(other, Graph):
1029+
return other.__rrshift__((self, index, other_index))
1030+
9991031
if other.get_num_inputs() == 0:
10001032
raise FiltergraphMismatchError(self.get_num_outputs(), 0)
10011033

@@ -1011,9 +1043,14 @@ def __rrshift__(self, other):
10111043

10121044
# resolve the index
10131045
if type(other) == tuple:
1014-
index, other = other
1046+
if len(other) > 2:
1047+
other, other_index, index = other
1048+
else:
1049+
other, index = other
1050+
other_index = None
10151051
else:
10161052
index = None
1053+
other_index = None
10171054

10181055
if not len(self):
10191056
if index is not None:
@@ -1039,10 +1076,13 @@ def __rrshift__(self, other):
10391076

10401077
# if other is Filter object, do add operation
10411078
try:
1042-
other = as_filterchain(other)
1079+
other = as_filtergraph_object(other)
10431080
except:
10441081
return NotImplemented
10451082

1083+
if isinstance(other, Graph):
1084+
return other.__rshift__((other_index, index, self))
1085+
10461086
if other.get_num_outputs() == 0:
10471087
raise FiltergraphMismatchError(0, self.get_num_inputs())
10481088

tests/test_filtergraph_chain.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from ffmpegio.caps import filters
2+
import operator
33

44
logging.basicConfig(level=logging.INFO)
55

@@ -52,7 +52,7 @@ def test_iter_pads():
5252
# if __name__ == "__main__":
5353
def test_resolve_index():
5454
with pytest.raises(fg_lib.FiltergraphPadNotFoundError):
55-
fg_lib.Chain('color')._resolve_index(True,None)
55+
fg_lib.Chain("color")._resolve_index(True, None)
5656

5757
fchain = fg_lib.Chain("fps,scale2ref,overlay,split=3,concat=3")
5858

@@ -61,8 +61,32 @@ def test_resolve_index():
6161

6262
assert fchain._resolve_index(True, None) == (0, 0)
6363
assert fchain._resolve_index(False, None) == (4, 0)
64-
assert fchain._resolve_index(True, 1)==(4,1)
65-
assert fchain._resolve_index(False, 1)==(3,1)
64+
assert fchain._resolve_index(True, 1) == (4, 1)
65+
assert fchain._resolve_index(False, 1) == (3, 1)
6666
assert fchain._resolve_index(True, (1, 0)) == (1, 0)
6767
assert fchain._resolve_index(True, (4, None)) == (4, 0)
6868
assert fchain._resolve_index(True, (4, 1)) == (4, 1)
69+
70+
71+
@pytest.mark.parametrize(
72+
"op, lhs,rhs,expected",
73+
[
74+
# fmt:off
75+
(operator.__add__, fg_lib.Chain("scale"), "overlay", "scale[L0];[L0]overlay"),
76+
(operator.__add__, "scale", fg_lib.Chain("overlay"), "scale[L0];[L0]overlay"),
77+
(operator.__rshift__, fg_lib.Chain("split"), "hflip", "split[L0];[L0]hflip"),
78+
(operator.__rshift__, fg_lib.Chain("split"), (1, "overlay"), "split[L0];[L0]overlay"),
79+
(operator.__rshift__, fg_lib.Chain("split"), (1, "[in]overlay"), "split[L0];[in][L0]overlay"),
80+
(operator.__rshift__, fg_lib.Chain("split"), (1, 1, "overlay"), "split[L0];[L0]overlay"),
81+
(operator.__rshift__, fg_lib.Chain("split"), (None, '[over]', "[base][over]overlay"), "split[over];[base][over]overlay"),
82+
(operator.__rshift__, "hflip", fg_lib.Chain("overlay"), "hflip[L0];[L0]overlay"),
83+
(operator.__rshift__, ("split",1), fg_lib.Chain("overlay"), "split[L0];[L0]overlay"),
84+
(operator.__rshift__, ("split",(0,1)), fg_lib.Chain("overlay"), "split[L0];[L0]overlay"),
85+
(operator.__rshift__, ("split[out]",1), fg_lib.Chain("overlay"), "split[out][L0];[L0]overlay"),
86+
(operator.__rshift__, ("split[out]", '[out]',None), fg_lib.Chain("overlay"), "split[out];[out]overlay"),
87+
# (operator.__rshift__, fg_lib.Graph("split[out1][out2]"), ('[out1]', '[over]', "[base][over]overlay"), "split[out1][out2];[base][out1]overlay"),
88+
# fmt:on
89+
],
90+
)
91+
def test_ops(op, lhs, rhs, expected):
92+
assert str(op(lhs, rhs)) == expected

tests/test_filtergraph_filter.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import logging
2-
from ffmpegio.caps import filters
32

43
logging.basicConfig(level=logging.INFO)
54

65
from ffmpegio import filtergraph as fg_lib
7-
from pprint import pprint
86
import pytest
7+
import operator
98

109

1110
def test_Filter():
@@ -106,9 +105,26 @@ def test_apply():
106105
print(str(f1))
107106

108107

109-
def test_ops():
110-
assert str(fg_lib.Filter("scale") + "overlay") == "scale[L0];[L0]overlay"
111-
assert str("scale" + fg_lib.Filter("overlay")) == "scale[L0];[L0]overlay"
108+
@pytest.mark.parametrize(
109+
"op, lhs,rhs,expected",
110+
[
111+
(operator.__add__, fg_lib.Filter("scale"), "overlay", "scale[L0];[L0]overlay"),
112+
(operator.__add__, "scale", fg_lib.Filter("overlay"), "scale[L0];[L0]overlay"),
113+
(operator.__rshift__, fg_lib.Filter("split"), "hflip", "split[L0];[L0]hflip"),
114+
(operator.__rshift__, fg_lib.Filter("split"), (1, "overlay"), "split[L0];[L0]overlay"),
115+
(operator.__rshift__, fg_lib.Filter("split"), (1, "[in]overlay"), "split[L0];[in][L0]overlay"),
116+
(operator.__rshift__, fg_lib.Filter("split"), (1, 1, "overlay"), "split[L0];[L0]overlay"),
117+
(operator.__rshift__, fg_lib.Filter("split"), (None, '[over]', "[base][over]overlay"), "split[over];[base][over]overlay"),
118+
(operator.__rshift__, "hflip", fg_lib.Filter("overlay"), "hflip[L0];[L0]overlay"),
119+
(operator.__rshift__, ("split",1), fg_lib.Filter("overlay"), "split[L0];[L0]overlay"),
120+
(operator.__rshift__, ("split",(0,1)), fg_lib.Filter("overlay"), "split[L0];[L0]overlay"),
121+
(operator.__rshift__, ("split[out]",1), fg_lib.Filter("overlay"), "split[out][L0];[L0]overlay"),
122+
(operator.__rshift__, ("split[out]", '[out]',None), fg_lib.Filter("overlay"), "split[out];[out]overlay"),
123+
# (operator.__rshift__, fg_lib.Graph("split[out1][out2]"), ('[out1]', '[over]', "[base][over]overlay"), "split[out1][out2];[base][out1]overlay"),
124+
],
125+
)
126+
def test_ops(op, lhs, rhs, expected):
127+
assert str(op(lhs, rhs)) == expected
112128

113129

114130
if __name__ == "__name__":

0 commit comments

Comments
 (0)