Skip to content

Commit

Permalink
Merge pull request #2095 from bzczb/i2094-slow-decorators
Browse files Browse the repository at this point in the history
Fix decorator-related slow performance
  • Loading branch information
OsaAjani authored Jan 10, 2025
2 parents 5ed9c0f + 685137d commit 0ca4c18
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
1 change: 1 addition & 0 deletions moviepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Imports everything that you need from the MoviePy submodules so that every thing
can be directly imported with ``from moviepy import *``.
"""

from moviepy.audio import fx as afx
from moviepy.audio.AudioClip import (
AudioArrayClip,
Expand Down
64 changes: 38 additions & 26 deletions moviepy/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,32 @@ def audio_video_effect(func, effect, clip, *args, **kwargs):
return func(effect, 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.decorator(wrapper)
return decorator.decorate(func, wrapper)

return decor


def convert_parameter_to_seconds(varnames):
Expand All @@ -114,11 +124,11 @@ def add_mask_if_none(func, clip, *args, **kwargs):
return func(clip, *args, **kwargs)


@decorator.decorator
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**."""
argnames = inspect.getfullargspec(func).args[1:]

def find_fps(fps):
def find_fps(clip, fps):
if fps is not None:
return fps
elif getattr(clip, "fps", None):
Expand All @@ -130,14 +140,16 @@ def find_fps(fps):
" 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)
12 changes: 6 additions & 6 deletions moviepy/video/io/ffmpeg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,12 +480,12 @@ def parse(self):
# for default streams, set their numbers globally, so it's
# easy to get without iterating all
if self._current_stream["default"]:
self.result[
f"default_{stream_type_lower}_input_number"
] = input_number
self.result[
f"default_{stream_type_lower}_stream_number"
] = stream_number
self.result[f"default_{stream_type_lower}_input_number"] = (
input_number
)
self.result[f"default_{stream_type_lower}_stream_number"] = (
stream_number
)

# exit chapter
if self._current_chapter:
Expand Down

0 comments on commit 0ca4c18

Please sign in to comment.