Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix decorator-related slow performance #2095

Merged
merged 7 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading