8000 add more filters, options · Powercoder64/ffmpeg-python@3cb9523 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3cb9523

Browse files
committed
add more filters, options
* add filters: vflip, zoompan, hue, colorchannelmixer * add options for trim, overlay * filter code made more generic and extendable
1 parent 9a64b5c commit 3cb9523

File tree

1 file changed

+95
-11
lines changed

1 file changed

+95
-11
lines changed

ffmpeg/__init__.py

Lines changed: 95 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,29 +74,60 @@ class _FilterNode(_Node):
7474
def _get_filter(self):
7575
raise NotImplementedError()
7676

77+
def _get_params_from_dict(self, d):
78+
params = ""
79+
for k in self.kwargs:
80+
params += k + "={}:".format(self.kwargs[k])
81+
if len(params) > 0:
82+
params = params[:-1]
83+
return params
84+
85+
def _get_params_from_list(self, l):
86+
return ":".join(["{}".format(i) for i in l])
87+
88+
def _get_filter_from_dict(self, d):
89+
p = self._get_params_from_dict(d)
90+
if len(p) > 0:
91+
return self.NAME + "=" + p
92+
return self.NAME
93+
94+
7795

7896
class _TrimNode(_FilterNode):
7997
NAME = 'trim'
8098

81-
def __init__(self, parent, start_frame, end_frame, setpts='PTS-STARTPTS'):
99+
def __init__(self, parent, **kwargs):
82100
super(_TrimNode, self).__init__([parent])
83-
self.start_frame = start_frame
84-
self.end_frame = end_frame
85-
self.setpts = setpts
101+
# if "setpts" not in kwargs:
102+
# kwargs["setpts"] = 'PTS-STARTPTS'
103+
self.kwargs = kwargs
86104

87105
def _get_filter(self):
88-
return 'trim=start_frame={}:end_frame={},setpts={}'.format(self.start_frame, self.end_frame, self.setpts)
106+
params = ""
107+
for k in self.kwargs:
108+
if k == "setpts":
109+
continue
110+
params += k
111+
params += "={}:".format(self.kwargs[k])
112+
if len(params) > 0:
113+
params = params[:-1]
114+
115+
if "setpts" in self.kwargs:
116+
params += "setpts={}".format(self.kwargs["setpts"])
117+
118+
return self.NAME + '=' + params
89119

90120

91121
class _OverlayNode(_FilterNode):
92122
NAME = 'overlay'
93123

94-
def __init__(self, main_parent, overlay_parent, eof_action='repeat'):
124+
def __init__(self, main_parent, overlay_parent, **kwargs):
95125
super(_OverlayNode, self).__init__([main_parent, overlay_parent])
96126
self.eof_action = eof_action
127+
self.kwargs = kwargs
97128

98129
def _get_filter(self):
99-
return 'overlay=eof_action={}'.format(self.eof_action)
130+
return self._get_filter_from_dict(self.kwargs)
100131

101132

102133
class _HFlipNode(_FilterNode):
@@ -106,23 +137,38 @@ def __init__(self, parent):
106137
super(_HFlipNode, self).__init__([parent])
107138

108139
def _get_filter(self):
109-
return 'hflip'
140+
return self.NAME
141+
142+
143+
class _VFlipNode(_FilterNode):
144+
NAME = 'vflip'
145+
146+
def __init__(self, parent):
147+
super(_VFlipNode, self).__init__([parent])
148+
149+
def _get_filter(self):
150+
return self.NAME
151+
110152

111153

112154
class _DrawBoxNode(_FilterNode):
113155
NAME = 'drawbox'
114156

115-
def __init__(self, parent, x, y, width, height, color, thickness=1):
157+
def __init__(self, parent, x, y, width, height, color, **kwargs):
116158
super(_DrawBoxNode, self).__init__([parent])
117159
self.x = x
118160
self.y = y
119161
self.width = width
120162
self.height = height
121163
self.color = color
122-
self.thickness = thickness
164+
self.kwargs = kwargs
123165

124166
def _get_filter(self):
125-
return 'drawbox={}:{}:{}:{}:{}:t={}'.format(self.x, self.y, self.width, self.height, self.color, self.thickness)
167+
f = 'drawbox={}:{}:{}:{}:{}'.format(self.x, self.y, self.width, self.height, self.color)
168+
p = self._get_params_from_dict(self.kwargs)
169+
if len(p) > 0:
170+
return f + ":" + p
171+
return f
126172

127173

128174
class _ConcatNode(_Node):
@@ -135,7 +181,45 @@ def __init__(self, *parents):
135181
def _get_filter(self):
136182
return 'concat=n={}'.format(len(self.parents))
137183

184+
class _ZoomPanNode(_FilterNode):
185+
NAME = 'zoompan'
186+
187+
def __init__(self, parent, **kwargs):
188+
super(_ZoomPanNode, self).__init__([parent])
189+
self.kwargs = kwargs
190+
191+
def _get_filter(self):
192+
return self._get_filter_from_dict(self.kwargs)
193+
194+
class _HueNode(_FilterNode):
195+
NAME = 'hue'
196+
197+
def __init__(self, parent, **kwargs):
198+
super(_HueNode, self).__init__([parent])
199+
self.kwargs = kwargs
138200

201+
def _get_filter(self):
202+
return self._get_filter_from_dict(self.kwargs)
203+
204+
class _ColorChannelMixerNode(_FilterNode):
205+
NAME = 'colorchannelmixer'
206+
207+
def __init__(self, parent, *args, **kwargs):
208+
super(_ColorChannelMixerNode, self).__init__([parent])
209+
self.args = args
210+
self.kwargs = kwargs
211+
212+
def _get_filter(self):
213+
f = self.NAME + "="
214+
if self.args:
215+
f += self._get_params_from_list(self.args)
216+
if self.kwargs:
217+
f += ":"
218+
if self.kwargs:
219+
f += self._get_params_from_dict(self.kwargs)
220+
return f
221+
222+
139223
class _OutputNode(_Node):
140224
@classmethod
141225
def _get_stream_name(cls, name):

0 commit comments

Comments
 (0)
0