Introduction
MoviePy is a Python module for video editing. It can be used for basic operations (like cuts, concatenations, title insertions), video compositing (a.k.a. non-linear editing), video processing, or to create advanced effects. It can read and write the most common video formats, including GIF.
MoviePy is open source software originally written by Zulko and released under the MIT licence. It works on Windows, Mac, and Linux, with Python 2 or Python 3. The code is hosted on Github, where you can push improvements, report bugs and ask for help.
To use MoviePy, you first need to install it. You can do this using pip:
pip install moviepy
Once MoviePy is installed, you can start using it. The basic building block of MoviePy is the Clip
class. A Clip represents a piece of video or audio. You can create a Clip from a file, or from a sequence of frames.
Once you have a Clip, you can do things like cut it, concatenate it with other Clips, add effects to it, and export it to a file.
Here is an example of how to use MoviePy to create a simple video:
import moviepy.editor as mpy
# Create a Clip from a file
clip1 = mpy.VideoFileClip("video1.mp4")
# Create a second Clip from a sequence of frames
frames = [mpy.ImageClip("frame%02d.jpg" % i) for i in range(10)]
clip2 = mpy.concatenate(frames)
# Concatenate the two Clips
final_clip = clip1.concatenate(clip2)
# Export the final Clip to a file
final_clip.write_videofile("output.mp4")
This code will create a new video file called output.mp4
that is the concatenation of the two input videos.
For more information on how to use MoviePy, please refer to the documentation: https://zulko.github.io/moviepy/
Here are some of the important classes in MoviePy:
Clip
: The basic building block of MoviePy. Represents a piece of video or audio.VideoFileClip
: A Clip that is loaded from a file.ImageClip
: A Clip that is created from a sequence of images.CompositeVideoClip
: A Clip that is composed of multiple other Clips.TextClip
: A Clip that contains text.Effect
: A class that can be used to apply an effect to a Clip.
GIF
you can use MoviePy to produce GIFs. To do this, you can use the write_gif()
method of the Clip
class. The write_gif()
method takes a few arguments, including the name of the output file and the frame rate of the GIF.
Here is an example of how to use write_gif()
to create a GIF from a video file:
import moviepy.editor as mpy
# Create a Clip from a file
clip = mpy.VideoFileClip("video.mp4")
# Set the frame rate of the GIF
frame_rate = 10
# Write the GIF to a file
clip.write_gif("output.gif", frame_rate=frame_rate)
This code will create a new GIF file called output.gif
with a frame rate of 10 frames per second.
You can also use the write_gif()
method to create a GIF from a sequence of images. To do this, you can pass a list of images to the images
argument of the write_gif()
method.
Here is an example of how to use write_gif()
to create a GIF from a sequence of images:
import moviepy.editor as mpy
# Create a list of images
images = [mpy.ImageClip("image%02d.jpg" % i) for i in range(10)]
# Write the GIF to a file
mpy.concatenate(images).write_gif("output.gif")
This code will create a new GIF file called output.gif
from the sequence of images image00.jpg
, image01.jpg
, …, image09.jpg
.
audio, subtitle and watermark
MoviePy can be used to manipulate audio, subtitle, and watermark in videos. Here are some examples:
- To add a watermark to a video, you can use the
CompositeVideoClip()
class. TheCompositeVideoClip()
class allows you to combine multiple Clips into a single Clip. You can use this to overlay a watermark image or text on top of a video.
import moviepy.editor as mpy
# Create a Clip from a file
video_clip = mpy.VideoFileClip("video.mp4")
# Create a watermark image
watermark_image = mpy.ImageClip("watermark.png")
# Overlay the watermark image on the video clip
watermarked_clip = mpy.CompositeVideoClip([video_clip, watermark_image])
# Write the watermarked clip to a file
watermarked_clip.write_videofile("output.mp4")
- To add subtitles to a video, you can use the
TextClip()
class. TheTextClip()
class allows you to create a Clip that contains text. You can then use theCompositeVideoClip()
class to overlay the text Clip on top of the video Clip.
import moviepy.editor as mpy
# Create a Clip from a file
video_clip = mpy.VideoFileClip("video.mp4")
# Create a subtitle text
subtitle_text = "This is a subtitle"
# Create a TextClip with the subtitle text
subtitle_clip = mpy.TextClip(subtitle_text, font="Arial", size=30)
# Overlay the subtitle clip on the video clip
watermarked_clip = mpy.CompositeVideoClip([video_clip, subtitle_clip])
# Write the watermarked clip to a file
watermarked_clip.write_videofile("output.mp4")
- To manipulate audio in a video, you can use the
AudioClip()
class. TheAudioClip()
class allows you to extract the audio from a video Clip and manipulate it. For example, you can change the volume of the audio, or add effects to it.
import moviepy.editor as mpy
# Create a Clip from a file
video_clip = mpy.VideoFileClip("video.mp4")
# Extract the audio from the video clip
audio_clip = video_clip.audio
# Change the volume of the audio
audio_clip.set_volume(0.5)
# Add an echo effect to the audio
audio_clip = audio_clip.apply_effect("echo")
# Write the audio clip to a file
audio_clip.write_audiofile("output.mp3")
Audio
the example for AudioClip does not change the original mp4’s audio. It only extracts the audio from the video clip and manipulates the extracted audio. The original mp4’s audio is not affected.
To change the original mp4’s audio, you can use the set_audio()
method of the VideoFileClip()
class. The set_audio()
method takes an AudioClip
object as an argument. The AudioClip
object that you pass to the set_audio()
method will replace the original audio in the video clip.
Here is an example of how to use set_audio()
to change the original mp4’s audio:
import moviepy.editor as mpy
# Create a Clip from a file
video_clip = mpy.VideoFileClip("video.mp4")
# Load a new audio file
new_audio_clip = mpy.AudioFileClip("new_audio.mp3")
# Set the audio of the video clip to the new audio clip
video_clip.set_audio(new_audio_clip)
# Write the video clip to a file
video_clip.write_videofile("output.mp4")
This code will create a new video file called output.mp4
with the audio from the new_audio.mp3
file replacing the original audio in the video.mp4
file.
Channel
To make a video have multiple audio channels with MoviePy, you can use the CompositeAudioClip()
class. The CompositeAudioClip()
class allows you to combine multiple audio Clips into a single Clip. This means that you can add multiple audio tracks to a video.
Here is an example of how to use CompositeAudioClip()
to make a video have two audio channels:
import moviepy.editor as mpy
# Load two audio files
audio_clip_1 = mpy.AudioFileClip("audio1.mp3")
audio_clip_2 = mpy.AudioFileClip("audio2.mp3")
# Create a CompositeAudioClip with the two audio clips
composite_audio_clip = mpy.CompositeAudioClip([audio_clip_1, audio_clip_2])
# Write the composite audio clip to a file
composite_audio_clip.write_audiofile("output.mp3")
This code will create a new audio file called output.mp3
with two audio tracks. The first audio track will be the audio from the audio1.mp3
file, and the second audio track will be the audio from the audio2.mp3
file.
You can also use the CompositeAudioClip()
class to add an audio track to an existing video. To do this, you can pass the video Clip to the CompositeAudioClip()
constructor as the first argument.
Here is an example of how to use CompositeAudioClip()
to add an audio track to a video:
import moviepy.editor as mpy
# Load a video file
video_clip = mpy.VideoFileClip("video.mp4")
# Load an audio file
audio_clip = mpy.AudioFileClip("audio.mp3")
# Create a CompositeAudioClip with the video clip and the audio clip
composite_audio_clip = mpy.CompositeAudioClip([video_clip.audio, audio_clip])
# Write the composite audio clip to a file
composite_audio_clip.write_videofile("output.mp4")
This code will create a new video file called output.mp4
with the audio from the audio.mp3
file added to the audio from the video.mp4
file.
Mix
To mix multiple audio into one channel with MoviePy, you can use the concatenate_audioclips()
function. The concatenate_audioclips()
function takes a list of audio Clips as input and outputs a single audio Clip with the audio from all the input Clips concatenated together.
Here is an example of how to use concatenate_audioclips()
to mix multiple audio into one channel:
import moviepy.editor as mpy
# Load multiple audio files
audio_clip_1 = mpy.AudioFileClip("audio1.mp3")
audio_clip_2 = mpy.AudioFileClip("audio2.mp3")
audio_clip_3 = mpy.AudioFileClip("audio3.mp3")
# Concatenate the audio clips
mixed_audio_clip = mpy.concatenate_audioclips([audio_clip_1, audio_clip_2, audio_clip_3])
# Write the mixed audio clip to a file
mixed_audio_clip.write_audiofile("output.mp3")
This code will create a new audio file called output.mp3
with the audio from the audio1.mp3
, audio2.mp3
, and audio3.mp3
files concatenated together.
The concatenate_audioclips()
function will mix the audio from the input Clips by default. However, you can also specify the way you want to mix the audio by passing a keyword argument to the concatenate_audioclips()
function. For example, the following code will mix the audio from the input Clips by averaging them:
mixed_audio_clip = mpy.concatenate_audioclips([audio_clip_1, audio_clip_2, audio_clip_3], mix_mode="avg")
The mix_mode
keyword argument can be one of the following values:
avg
: Average the audio from the input Clips.max
: Take the maximum value of the audio from the input Clips.min
: Take the minimum value of the audio from the input Clips.sum
: Sum the audio from the input Clips.
GUI
there are a few GUIs that have been developed for MoviePy.
- Moveditor is a graphical user interface for MoviePy. It allows you to edit videos and audio files without having to write any Python code. Moveditor is available for Windows, Mac, and Linux. [Image of Moveditor GUI for MoviePy]
- Tkinter MoviePy is a Tkinter-based GUI for MoviePy. It allows you to edit videos and audio files with Python code. Tkinter MoviePy is available for Windows, Mac, and Linux. [Image of Tkinter MoviePy GUI for MoviePy]
- PyQt5 MoviePy is a PyQt5-based GUI for MoviePy. It allows you to edit videos and audio files with Python code. PyQt5 MoviePy is available for Windows, Mac, and Linux. [Image of PyQt5 MoviePy GUI for MoviePy]
You can find more information about these GUIs on the MoviePy website: https://zulko.github.io/moviepy/