Skip to content

Video editing with Python

Fix decorator performance.

vermontb9b5ecc61ccb
diff --git a/moviepy/decorators.py b/moviepy/decorators.py
index 621e3585..c33edb00 100644
--- a/moviepy/decorators.py
+++ b/moviepy/decorators.py
@@ -78,22 +78,28 @@ def audio_video_fx(func, clip, *args, **kwargs):
return func(clip, *args, **kwargs)
-def preprocess_args(fun, varnames):
- """Applies fun to variables in varnames before launching the function."""
+def preprocess_args(preprocess_func, varnames):
+ """Applies preprocess_func to variables in varnames before launching
+ the function.
+ """
- def wrapper(func, *args, **kwargs):
- names = inspect.getfullargspec(func).args
- new_args = [
- fun(arg) if (name in varnames) and (arg is not None) else arg
- for (arg, name) in zip(args, names)
- ]
- new_kwargs = {
- kwarg: fun(value) if kwarg in varnames else value
- for (kwarg, value) in kwargs.items()
- }
- return func(*new_args, **new_kwargs)
+ def decor(func):
+ argnames = inspect.getfullargspec(func).args
+
+ def wrapper(func, *args, **kwargs):
+ new_args = [
+ preprocess_func(arg) if (name in varnames) and (arg is not None) else arg
+ for (arg, name) in zip(args, argnames)
+ ]
+ new_kwargs = {
+ kwarg: preprocess_func(value) if kwarg in varnames else value
+ for (kwarg, value) in kwargs.items()
+ }
+ return func(*new_args, **new_kwargs)
+
+ return decorator.decorate(func, wrapper)
- return decorator.decorator(wrapper)
+ return decor
def convert_parameter_to_seconds(varnames):
@@ -114,11 +120,12 @@ def add_mask_if_none(func, clip, *args, **kwargs):
return func(clip, *args, **kwargs)
-def use_clip_fps_by_default(func, clip, *args, **kwargs):
+def use_clip_fps_by_default(func):
"""Will use ``clip.fps`` if no ``fps=...`` is provided in **kwargs**."""
- def find_fps(fps):
+ argnames = inspect.getfullargspec(func).args[1:]
+
+ def find_fps(clip, fps):
if fps is not None:
return fps
elif getattr(clip, "fps", None):
@@ -130,14 +137,16 @@ def use_clip_fps_by_default(func, clip, *args, **kwargs):
" the clip's fps with `clip.fps=24`" % func.__name__
)
- names = inspect.getfullargspec(func).args[1:]
+ def wrapper(func, clip, *args, **kwargs):
+ new_args = [
+ find_fps(clip, arg) if name == "fps" else arg
+ for (arg, name) in zip(args, argnames)
+ ]
+ new_kwargs = {
+ kwarg: find_fps(clip, kwarg) if kwarg == "fps" else value
+ for (kwarg, value) in kwargs.items()
+ }
- new_args = [
- find_fps(arg) if (name == "fps") else arg for (arg, name) in zip(args, names)
- ]
- new_kwargs = {
- kwarg: find_fps(value) if kwarg == "fps" else value
- for (kwarg, value) in kwargs.items()
- }
+ return func(clip, *new_args, **new_kwargs)
- return func(clip, *new_args, **new_kwargs)
+ return decorator.decorate(func, wrapper)