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

Move most imports from editor.py to __init__.py #1340

Merged
merged 12 commits into from
Jan 15, 2021
Merged
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ assignees: ''
You can format code by putting ``` (that's 3 backticks) on a line by itself at the beginning and end of each code block. For example:

```
from moviepy.editor import *
from moviepy import *
clip = ColorClip((600, 400), color=(255, 100, 0), duration=2)
```
-->
Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/question.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Hello! If you think that it is a simple problem, then consider asking instead on
You can format code by putting ``` (that's 3 backticks) on a line by itself at the beginning and end of each code block. For example:

```
from moviepy.editor import *
from moviepy import *
clip = ColorClip((600, 400), color=(255, 100, 0), duration=2)
```
-->
4 changes: 2 additions & 2 deletions .github/workflows/format_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ jobs:
# W503: line break before binary operator
flake8 moviepy --ignore=E203,E302,E402,W291,W293,W503 --max-complexity=10 --max-line-length=127 --statistics --count --exit-zero
# Check examples and tests with slightly relaxed rules to allow 'star' imports
# F403 'from moviepy.editor import *' used; unable to detect undefined names
# F405 'name' may be undefined, or defined from star imports: moviepy.editor
# F403 'from moviepy import *' used; unable to detect undefined names
# F405 'name' may be undefined, or defined from star imports: moviepy
flake8 . --ignore=E203,E302,E402,W291,W293,W503,F403,F405 --exclude moviepy/ --max-complexity=10 --max-line-length=127 --statistics --count --exit-zero
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `VideoFileClip.coreader()` that creates a new clip created from the same source file as the original (to complement the existing `AudioFileClip.coreader()`) [\#1332](https://github.com/Zulko/moviepy/pull/1332)

### Changed <!-- for changes in existing functionality -->
Lots of method and parameter names have been changed. This will be explained better in the documentation soon. See https://github.com/Zulko/moviepy/pull/1170 for more information. [\#1170](https://github.com/Zulko/moviepy/pull/1170)
- Lots of method and parameter names have been changed. This will be explained better in the documentation soon. See https://github.com/Zulko/moviepy/pull/1170 for more information. [\#1170](https://github.com/Zulko/moviepy/pull/1170)
- Changed recommended import from `import moviepy.editor` to `import moviepy`. This change is fully backwards compatible [\#1340](https://github.com/Zulko/moviepy/pull/1340)

### Deprecated <!-- for soon-to-be removed features -->
- `moviepy.video.fx.all` and `moviepy.audio.fx.all`. Use the fx method directly from the clip instance or import the fx function from `moviepy.video.fx` and `moviepy.audio.fx`. [\#1105](https://github.com/Zulko/moviepy/pull/1105)
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ In this example we open a video file, select the subclip between t=50s and t=60s

.. code:: python

from moviepy.editor import *
from moviepy import *

video = VideoFileClip("myHolidays.mp4").subclip(50,60)

Expand Down
4 changes: 2 additions & 2 deletions docs/examples/quick_recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Blurring all frames of a video
::

from skimage.filters import gaussian
from moviepy.editor import VideoFileClip
from moviepy import VideoFileClip

def blur(image):
""" Returns a blurred (radius=2 pixels) version of the image """
Expand Down Expand Up @@ -56,7 +56,7 @@ Getting the average frame of a video
"""""""""""""""""""""""""""""""""""""
::

from moviepy.editor import VideoFileClip, ImageClip
from moviepy import VideoFileClip, ImageClip
clip = VideoFileClip("video.mp4")
fps= 1.0 # take one frame per second
nframes = clip.duration*fps # total number of frames used
Expand Down
2 changes: 1 addition & 1 deletion docs/getting_started/audioclips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Creating a new audio clip

Audio clips can be created from an audio file or from the soundtrack of a video file ::

from moviepy.editor import *
from moviepy import *
audioclip = AudioFileClip("some_audiofile.mp3")
audioclip = AudioFileClip("some_video.avi")

Expand Down
6 changes: 3 additions & 3 deletions docs/getting_started/compositing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Two simple ways of putting clips together is to concatenate them (to play them o

Concatenation is done with the function ``concatenate_videoclips``: ::

from moviepy.editor import VideoFileClip, concatenate_videoclips
from moviepy import VideoFileClip, concatenate_videoclips
clip1 = VideoFileClip("myvideo.mp4")
clip2 = VideoFileClip("myvideo2.mp4").subclip(50,60)
clip3 = VideoFileClip("myvideo3.mp4")
Expand All @@ -34,7 +34,7 @@ The ``final_clip`` is a clip that plays the clips 1, 2, and 3 one after the othe

Stacking is done with ``clip_array``: ::

from moviepy.editor import VideoFileClip, clips_array, vfx
from moviepy import VideoFileClip, clips_array, vfx
clip1 = VideoFileClip("myvideo.mp4").margin(10) # add 10px contour
clip2 = clip1.fx( vfx.mirror_x)
clip3 = clip1.fx( vfx.mirror_y)
Expand Down Expand Up @@ -125,7 +125,7 @@ When you mix video clips together, MoviePy will automatically compose their resp

If you want to make a custom audiotrack from several audio sources: audioc clips can be mixed together with ``CompositeAudioClip`` and ``concatenate_audioclips``: ::

from moviepy.editor import *
from moviepy import *
# ... make some audio clips aclip1, aclip2, aclip3
concat = concatenate_audioclips([aclip1, aclip2, aclip3])
compo = CompositeAudioClip([aclip1.volumex(1.2),
Expand Down
6 changes: 3 additions & 3 deletions docs/getting_started/effects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ but this is not easy to read. To have a clearer syntax you can use ``clip.fx``:
.fx( effect_2, args2)
.fx( effect_3, args3))

Much better! There are already many effects implemented in the modules ``moviepy.video.fx`` and ``moviepy.audio.fx``. The fx methods in these modules are automatically applied to the sound and the mask of the clip if it is relevant, so that you don't have to worry about modifying these. For practicality, when you use ``from moviepy.editor import *``, these two modules are loaded as ``vfx`` and ``afx``, so you may write something like ::
Much better! There are already many effects implemented in the modules ``moviepy.video.fx`` and ``moviepy.audio.fx``. The fx methods in these modules are automatically applied to the sound and the mask of the clip if it is relevant, so that you don't have to worry about modifying these. For practicality, these two modules are loaded as ``vfx`` and ``afx``, so you may write something like ::

from moviepy.editor import *
from moviepy import *
clip = (VideoFileClip("myvideo.avi")
.fx( vfx.resize, width=460) # resize (keep aspect ratio)
.fx( vfx.speedx, 2) # double the speed
.fx( vfx.colorx, 0.5)) # darken the picture

For convenience, when you use ``moviepy.editor``, frequently used methods such as ``resize`` can be called in a simpler way: ``clip.resize(...)`` instead of ``clip.fx( vfx.resize, ...)``
For convenience, fx methods such as ``resize`` can be called in a simpler way: ``clip.resize(...)`` instead of ``clip.fx( vfx.resize, ...)``


Methods to create custom effects
Expand Down
19 changes: 5 additions & 14 deletions docs/getting_started/efficient_moviepy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,11 @@ The best way to start with MoviePy is to use it with the IPython Notebook: it ma
Should I use ``moviepy.editor``?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Most examples in this documentation use the submodule ``moviepy.editor``, but this submodule is not adapted to all needs so should *you* use it? Short answer: if you use MoviePy to edit videos *by hand*, use it, but if you use MoviePy inside a larger library or program or webserver, it is better to avoid it and just load the functions that you need.

The module ``moviepy.editor`` can be loaded using one of the three following methods: ::


from moviepy.editor import * # imports everything, quick and dirty
import moviepy.editor as mpy # Clean. Then use mpy.VideoClip, etc.
from moviepy.editor import VideoFileClip # just import what you need

With any of these lines, the ``moviepy.editor`` module will actually do a lot of work behind the curtain: it will fetch all the most common classes, functions and subpackages of MoviePy, initialize a PyGame session (if PyGame is installed) to be able to preview video clips, and implement some shortcuts, like adding the ``resize`` transformation to the clips. This way you can use ``clip.resize(width=240)`` instead of the longer ``clip.fx( resize, width=240)``. In short, ``moviepy.editor``
provides all you need to play around and edit your videos, but it will take time to load (circa one second). So if all you need is one or two features inside another library, it is better to import directly what you need, as follows: ::

from moviepy.video.io.VideoFileClip import VideoFileClip
from moviepy.video.fx.resize import resize
Older versions of MoviePy always recommended importing from ``moviepy.editor``. In v2.0 and above this is no longer recommended and you should generally import directly from ``moviepy`` (for example, ``from moviepy import VideoFileClip``).
The module ``moviepy.editor`` should now only be loaded if you are using MoviePy to edit videos *by hand*. Importing it will:
- Start a pygame session to enable ``clip.show()`` and ``clip.preview()`` if pygame is installed
- Enable ``clip.ipython_display()`` if in an IPython Notebook
- Enable ``sliders()`` if Matplotlib is installed

.. _previewing:

Expand Down
2 changes: 1 addition & 1 deletion docs/getting_started/quick_presentation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Example code
In a typical MoviePy script, you load video or audio files, modify them, put them together, and write the final result to a new video file. As an example, let us load a video of my last holidays, lower the volume, add a title in the center of the video for the first ten seconds, and write the result in a file: ::

# Import everything needed to edit video clips
from moviepy.editor import *
from moviepy import *

# Load myHolidays.mp4 and select the subclip 00:00:50 - 00:00:60
clip = VideoFileClip("myHolidays.mp4").subclip(50,60)
Expand Down
2 changes: 1 addition & 1 deletion docs/getting_started/videoclips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ VideoClip
``VideoClip`` is the base class for all the other video clips in MoviePy. If all you want is to edit video files, you will never need it. This class is practical when you want to make animations from frames that are generated by another library. All you need is to define a function ``make_frame(t)`` which returns a HxWx3 numpy array (of 8-bits integers) representing the frame at time t. Here is an example with the graphics library Gizeh: ::

import gizeh
import moviepy.editor as mpy
import moviepy as mpy

WIDTH, HEIGHT = (128, 128)

Expand Down
4 changes: 2 additions & 2 deletions docs/getting_started/working_with_matplotlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ MoviePy allows you to produce custom animations by defining a function that retu

An example of this workflow is below: ::

from moviepy.editor import VideoClip
from moviepy import VideoClip

def make_frame(t):
"""Returns an image of the frame for time t."""
Expand All @@ -32,7 +32,7 @@ An example of an animation using `matplotlib` can then be as follows: ::

import matplotlib.pyplot as plt
import numpy as np
from moviepy.editor import VideoClip
from moviepy import VideoClip
from moviepy.video.io.bindings import mplfig_to_npimage

x = np.linspace(-2, 2, 200)
Expand Down
2 changes: 1 addition & 1 deletion docs/ref/audiofx.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Or import everything: ::

When you type ::

from moviepy.editor import *
from moviepy import *

the module ``audio.fx`` is loaded as ``afx`` and you can use ``afx.volumex``, etc.

Expand Down
2 changes: 1 addition & 1 deletion docs/ref/videofx.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Or import everything: ::

When you type: ::

from moviepy.editor import *
from moviepy import *

the module ``video.fx`` is loaded as ``vfx`` and you can use ``vfx.colorx``, ``vfx.resize`` etc.

Expand Down
2 changes: 1 addition & 1 deletion examples/compo_from_image.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from moviepy.editor import *
from moviepy import *
from moviepy.video.tools.segmenting import find_objects

# Load the image specifying the regions.
Expand Down
2 changes: 1 addition & 1 deletion examples/dancing_knights.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import os
import sys

from moviepy.editor import *
from moviepy import *
from moviepy.audio.tools.cuts import find_audio_period
from moviepy.video.tools.cuts import find_video_period

Expand Down
2 changes: 1 addition & 1 deletion examples/example_with_sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
movie put together.
"""

from moviepy.editor import *
from moviepy import *
from moviepy.video.tools.drawing import color_split

duration = 6 # duration of the final clip
Expand Down
2 changes: 1 addition & 1 deletion examples/headblur.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from moviepy.editor import *
from moviepy import *
from moviepy.video.tools.tracking import manual_tracking
from moviepy.video.tools.interpolators import Trajectory

Expand Down
2 changes: 1 addition & 1 deletion examples/logo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from moviepy.editor import *
from moviepy import *

w, h = moviesize = (720, 380)

Expand Down
2 changes: 1 addition & 1 deletion examples/masked_credits.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from moviepy.editor import *
from moviepy import *
from moviepy.video.tools.credits import credits1

# Load the mountains clip, cut it, slow it down, make it look darker
Expand Down
2 changes: 1 addition & 1 deletion examples/moving_letters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from moviepy.editor import *
from moviepy import *
from moviepy.video.tools.segmenting import find_objects

# WE CREATE THE TEXT THAT IS GOING TO MOVE, WE CENTER IT.
Expand Down
2 changes: 1 addition & 1 deletion examples/painting_effect.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" requires scikit-image installed (for vfx.painting) """

from moviepy.editor import *
from moviepy import *

# WE TAKE THE SUBCLIPS WHICH ARE 2 SECONDS BEFORE & AFTER THE FREEZE

Expand Down
2 changes: 1 addition & 1 deletion examples/soundtrack.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
""" A simple test script on how to put a soundtrack to a movie """
from moviepy.editor import *
from moviepy import *

# We load a movie and replace the sound with some music:

Expand Down
2 changes: 1 addition & 1 deletion examples/star_worms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import numpy as np

from moviepy.editor import *
from moviepy import *
from moviepy.video.tools.drawing import color_gradient
from skimage import transform as tf

Expand Down
2 changes: 1 addition & 1 deletion examples/the_end.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from moviepy.editor import *
from moviepy import *
from moviepy.video.tools.drawing import circle

clip = (
Expand Down
2 changes: 1 addition & 1 deletion examples/ukulele_concerto.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from moviepy.editor import *
from moviepy import *

# UKULELE CLIP, OBTAINED BY CUTTING AND CROPPING
# RAW FOOTAGE
Expand Down
2 changes: 1 addition & 1 deletion moviepy/Clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def iter_frames(self, fps=None, with_times=False, logger=None, dtype=None):

>>> # prints the maximum of red that is contained
>>> # on the first line of each frame of the clip.
>>> from moviepy.editor import VideoFileClip
>>> from moviepy import VideoFileClip
>>> myclip = VideoFileClip('myvideo.mp4')
>>> print ( [frame[0,:,0].max()
for frame in myclip.iter_frames()])
Expand Down
83 changes: 82 additions & 1 deletion moviepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,82 @@
from .version import __version__
"""
Imports everything that you need from the MoviePy submodules so that every thing
can be directly imported like `from moviepy import VideoFileClip`.

In particular it loads all effects from the video.fx and audio.fx folders
and turns them into VideoClip and AudioClip methods, so that instead of
``clip.fx(vfx.resize, 2)`` or ``vfx.resize(clip, 2)``
you can write ``clip.resize(2)``.

"""

import inspect

from moviepy.version import __version__


# Clips
from moviepy.video.io.VideoFileClip import VideoFileClip
from moviepy.video.io.ImageSequenceClip import ImageSequenceClip
from moviepy.video.io.downloader import download_webfile
from moviepy.video.VideoClip import (
VideoClip,
ImageClip,
ColorClip,
TextClip,
BitmapClip,
)
from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip, clips_array
from moviepy.video.compositing.concatenate import concatenate_videoclips

from moviepy.audio.AudioClip import (
AudioClip,
CompositeAudioClip,
concatenate_audioclips,
)
from moviepy.audio.io.AudioFileClip import AudioFileClip

# FX
from moviepy.video import fx as vfx
import moviepy.audio.fx as afx
import moviepy.video.compositing.transitions as transfx

# Tools
import moviepy.video.tools as videotools
import moviepy.video.io.ffmpeg_tools as ffmpeg_tools
from moviepy.tools import convert_to_seconds


# Transforms the effects into Clip methods so that
# they can be called with clip.resize(width=500) instead of
# clip.fx(vfx.resize, width=500)
audio_fxs = inspect.getmembers(afx, inspect.isfunction)
video_fxs = (
inspect.getmembers(vfx, inspect.isfunction)
+ inspect.getmembers(transfx, inspect.isfunction)
+ audio_fxs
)

for name, function in video_fxs:
setattr(VideoClip, name, function)

for name, function in audio_fxs:
setattr(AudioClip, name, function)


def preview(self, *args, **kwargs):
"""NOT AVAILABLE: clip.preview requires importing from moviepy.editor"""
raise ImportError("clip.preview requires importing from moviepy.editor")


def show(self, *args, **kwargs):
"""NOT AVAILABLE: clip.show requires importing from moviepy.editor"""
raise ImportError("clip.show requires importing from moviepy.editor")


VideoClip.preview = preview
VideoClip.show = show
AudioClip.preview = preview

tburrows13 marked this conversation as resolved.
Show resolved Hide resolved
# Cleanup namespace
del audio_fxs, video_fxs, name, function, preview, show
del inspect
2 changes: 1 addition & 1 deletion moviepy/audio/fx/audio_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def audio_loop(clip, n_loops=None, duration=None):
Examples
========

>>> from moviepy.editor import *
>>> from moviepy import *
>>> videoclip = VideoFileClip('myvideo.mp4')
>>> music = AudioFileClip('music.ogg')
>>> audio = afx.audio_loop( music, duration=videoclip.duration)
Expand Down
2 changes: 1 addition & 1 deletion moviepy/audio/fx/audio_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def audio_normalize(clip):
Examples
========

>>> from moviepy.editor import *
>>> from moviepy import *
>>> videoclip = VideoFileClip('myvideo.mp4').fx(afx.audio_normalize)

"""
Expand Down
Loading