Skip to content

Commit e70e587

Browse files
committed
filtergraph - fixed label str detection
1 parent c9c569a commit e70e587

1 file changed

Lines changed: 50 additions & 24 deletions

File tree

src/ffmpegio/filtergraph.py

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ def __init__(self, n, m) -> None:
125125
)
126126

127127

128+
class FiltergraphInvalidIndex(TypeError, FFmpegioError):
129+
pass
130+
131+
128132
def _check_joinable(src, dst):
129133
n = src.get_num_outputs()
130134
m = dst.get_num_inputs()
@@ -133,6 +137,10 @@ def _check_joinable(src, dst):
133137
return n == 1 and m == 1
134138

135139

140+
def _is_label(expr):
141+
return re.match(r"\[[^\[\]]+\]$", expr)
142+
143+
136144
class FiltergraphPadNotFoundError(FFmpegioError):
137145
def __init__(self, type, index) -> None:
138146
target = (
@@ -745,8 +753,11 @@ def __rshift__(self, other):
745753
index = self._resolve_index(False, index)
746754

747755
# if label
748-
if isinstance(other, str) and other[0] == "[":
749-
return self.add_labels(output_labels={index: other})
756+
if isinstance(other, str) and _is_label(other):
757+
if other_index is None:
758+
return self.add_labels(output_labels={index: other})
759+
else:
760+
raise FiltergraphInvalidIndex("index cannot be assigned to a label")
750761

751762
# if other is Filter object, do add operation
752763
try:
@@ -775,8 +786,11 @@ def __rrshift__(self, other):
775786
index = self._resolve_index(True, index)
776787

777788
# if label
778-
if isinstance(other, str) and other[0] == "[":
779-
return self.add_labels(input_labels={index: other})
789+
if isinstance(other, str) and _is_label(other):
790+
if other_index is None:
791+
return self.add_labels(input_labels={index: other})
792+
else:
793+
raise FiltergraphInvalidIndex("index cannot be assigned to a label")
780794

781795
# if other is Filter object, do add operation
782796
try:
@@ -949,7 +963,7 @@ def __rshift__(self, other):
949963
raise Chain.Error(
950964
"attempting to specify a pad index of an empty chain."
951965
)
952-
if isinstance(other, str) and other[0] == "[" and other[-1] == "]":
966+
if isinstance(other, str) and _is_label(other):
953967
raise Chain.Error(
954968
"attempting to set a pad label specified to an empty chain."
955969
)
@@ -958,10 +972,13 @@ def __rshift__(self, other):
958972
index = self._resolve_index(False, index)
959973

960974
# if label
961-
if isinstance(other, str) and other[0] == "[" and other[-1] == "]":
962-
fg = Graph([self])
963-
fg.add_label(other[1:-1], src=(0, *index))
964-
return fg
975+
if isinstance(other, str) and _is_label(other):
976+
if other_index is None:
977+
fg = Graph([self])
978+
fg.add_label(other[1:-1], src=(0, *index))
979+
return fg
980+
else:
981+
raise FiltergraphInvalidIndex("index cannot be assigned to a label")
965982

966983
# if other is Filter object, do add operation
967984
try:
@@ -993,7 +1010,7 @@ def __rrshift__(self, other):
9931010
raise Chain.Error(
9941011
"attempting to specify a pad index of an empty chain."
9951012
)
996-
if isinstance(other, str) and other[0] == "[" and other[-1] == "]":
1013+
if isinstance(other, str) and _is_label(other):
9971014
raise Chain.Error(
9981015
"attempting to set a pad label specified to an empty chain."
9991016
)
@@ -1002,10 +1019,13 @@ def __rrshift__(self, other):
10021019
index = self._resolve_index(True, index)
10031020

10041021
# if label
1005-
if isinstance(other, str) and other[0] == "[" and other[-1] == "]":
1006-
fg = Graph([self])
1007-
fg.add_label(other[1:-1], dst=(0, *index))
1008-
return fg
1022+
if isinstance(other, str) and _is_label(other):
1023+
if other_index is None:
1024+
fg = Graph([self])
1025+
fg.add_label(other[1:-1], dst=(0, *index))
1026+
return fg
1027+
else:
1028+
raise FiltergraphInvalidIndex("index cannot be assigned to a label")
10091029

10101030
# if other is Filter object, do add operation
10111031
try:
@@ -1441,7 +1461,7 @@ def __rshift__(self, other):
14411461
raise Chain.Error(
14421462
"attempting to specify a filter pad index of an empty chain."
14431463
)
1444-
if isinstance(other, str) and other[0] == "[" and other[-1] == "]":
1464+
if isinstance(other, str) and _is_label(other):
14451465
raise Chain.Error(
14461466
"attempting to set a filter pad label specified to an empty chain."
14471467
)
@@ -1450,10 +1470,13 @@ def __rshift__(self, other):
14501470
index = self._resolve_index(False, index)
14511471

14521472
# if label
1453-
if isinstance(other, str) and other[0] == "[" and other[-1] == "]":
1454-
fg = Graph(self) # copy
1455-
fg.add_label(other[1:-1], src=index)
1456-
return fg
1473+
if isinstance(other, str) and _is_label(other):
1474+
if other_index is None:
1475+
fg = Graph(self) # copy
1476+
fg.add_label(other[1:-1], src=index)
1477+
return fg
1478+
else:
1479+
raise FiltergraphInvalidIndex("index cannot be assigned to a label")
14571480

14581481
# if other is Filter object, do add operation
14591482
try:
@@ -1482,7 +1505,7 @@ def __rrshift__(self, other):
14821505
raise Chain.Error(
14831506
"attempting to specify a filter pad index of an empty chain."
14841507
)
1485-
if isinstance(other, str) and other[0] == "[" and other[-1] == "]":
1508+
if isinstance(other, str) and _is_label(other):
14861509
raise Chain.Error(
14871510
"attempting to set a filter pad label specified to an empty chain."
14881511
)
@@ -1491,10 +1514,13 @@ def __rrshift__(self, other):
14911514
index = self._resolve_index(True, index)
14921515

14931516
# if label
1494-
if isinstance(other, str) and other[0] == "[" and other[-1] == "]":
1495-
fg = Graph(self) # copy
1496-
fg.add_label(other[1:-1], dst=index)
1497-
return fg
1517+
if isinstance(other, str) and _is_label(other):
1518+
if other_index is None:
1519+
fg = Graph(self) # copy
1520+
fg.add_label(other[1:-1], dst=index)
1521+
return fg
1522+
else:
1523+
raise FiltergraphInvalidIndex("index cannot be assigned to a label")
14981524

14991525
# if other is Filter object, do add operation
15001526
try:

0 commit comments

Comments
 (0)