moviepy视频处理库使用详解

moviepy简介

moviepy能够对音频,视频,以及git图片进行剪切,合并,标题插入等处理,并支持多种格式。

moviepy也是基于ffmpeg,如果没有安装ffmpeg,moviepy会在第一次使用moviepy的时候自动下载安装ffmpeg,如果本机安装有ffmpeg,建议修改config_defaults.py文件中的配置为FFMPEG_BINARY = 'auto-detect'

至于其他工具,则是对应相应的工具自行决定要不要安装,比如增加文字需要ImageMagick,预览音频和视频需要PyGame

moviepy使用

moviepy的核心对象是clips,可以是AudioClipsVideoClips

create clips

1
2
3
4
5
6
7
8
9
10
11
12
# VIDEO CLIPS
clip = VideoClip(make_frame, duration=4) # for custom animations (see below)
clip = VideoFileClip("my_video_file.mp4") # or .avi, .webm, .gif ...
clip = ImageSequenceClip(['image_file1.jpeg', ...], fps=24)
clip = ImageClip("my_picture.png") # or .jpeg, .tiff, ...
clip = TextClip("Hello !", font="Amiri-Bold", fontsize=70, color="black")
clip = ColorClip(size=(460,380), color=[R,G,B])
# AUDIO CLIPS
clip = AudioFileClip("my_audiofile.mp3") # or .ogg, .wav... or a video !
clip = AudioArrayClip(numpy_array, fps=44100) # from a numerical array
clip = AudioClip(make_frame, duration=3) # uses a function make_frame(t)

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:

1
2
3
4
5
6
7
8
9
10
11
12
import gizeh
import moviepy.editor as mpy
def make_frame(t):
surface = gizeh.Surface(128,128) # width, height
radius = W*(1+ (t*(2-t))**2 )/6 # the radius varies over time
circle = gizeh.circle(radius, xy = (64,64), fill=(1,0,0))
circle.draw(surface)
return surface.get_npimage() # returns a 8-bit RGB array
clip = mpy.VideoClip(make_frame, duration=2) # 2 seconds
clip.write_gif("circle.gif",fps=15)

ImageSequenceClip

This is a clip made from a series of images, you call it with:

1
clip = ImageSequenceClip(images_list, fps=25)

where images_list can be either a list of image names (that will be played) in that order, a folder name (at which case all the image files in the folder will be played in alphanumerical order), or a list of frames (Numpy arrays), obtained for instance from other clips.

TextClip

Generating a TextClip requires to have ImageMagick installed and (for windows users) linked to MoviePy

Exporting video clips

1
2
3
4
my_clip.write_videofile("movie.mp4") # default codec: 'libx264', 24 fps
my_clip.write_videofile("movie.mp4",fps=15)
my_clip.write_videofile("movie.webm") # webm format
my_clip.write_videofile("movie.webm",audio=False) # don't render audio.

Sometimes it is impossible for MoviePy to guess the duration attribute of the clip (keep in mind that some clips, like ImageClips displaying a picture, have a priori an infinite duration). Then, the durationmust be set manually with clip.set_duration:

1
2
3
4
# Make a video showing a flower for 5 seconds
my_clip = Image("flower.jpeg") # has infinite duration
my_clip.write_videofile("flower.mp4") # Will fail ! NO DURATION !
my_clip.set_duration(5).write_videofile("flower.mp4") # works !

To write your video as an animated GIF, use

1
my_clip.write_gif('test.gif', fps=12)

You can write a frame to an image file with

1
2
myclip.save_frame("frame.png") # by default the first frame is extracted
myclip.save_frame("frame.jpeg", t='01:00:00') # frame at time t=1h

concatenating clips

1
2
3
4
5
6
from moviepy.editor import VideoFileClip, concatenate_videoclips
clip1 = VideoFileClip("myvideo.mp4")
clip2 = VideoFileClip("myvideo2.mp4").subclip(50,60)
clip3 = VideoFileClip("myvideo3.mp4")
final_clip = concatenate_videoclips([clip1,clip2,clip3])
final_clip.write_videofile("my_concatenation.mp4")

CompositeVideoClips也能合并clips

1
video = CompositeVideoClip([clip1,clip2,clip3], size=(720,460))

Clips transformations and effects

1
2
3
4
5
from moviepy.editor 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

Example Scripts

https://zulko.github.io/moviepy/examples/examples.html

参考文档

坚持原创技术分享,您的支持将鼓励我继续创作!