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")
依次播放,视频的大小以第一个视频为准,视频的大小为最大的剪辑的大小,小于该大小的剪辑可以选择填充颜色,也可以设置过度效果transaction=myclip
就是使用clips_array
将剪辑堆叠到窗口中一起进行播放
from moviepy.editor 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)
clip4 = clip1.resize(0.60) # downsize 60%
final_clip = clips_array([[clip1, clip2],
[clip3, clip4]])
final_clip.resize(width=480).write_videofile("my_stack.mp4")
将剪辑以图层的形式一起进行播放
video = CompositeVideoClip([clip1,clip2,clip3])
clip3在最上层,依次clip2,clip1。 重叠的部分会被遮挡,除非顶层会有mask遮罩层。组合的剪辑的大小默认为clip1的大小,即最底层的剪辑的大小。也可以自己指定大小
video = CompositeVideoClip([clip1,clip2,clip3], size=(720,460))
video = CompositeVideoClip([clip1, # starts at t=0
clip2.set_start(5).crossfadein(1),
clip3.set_start(9).crossfadein(1.5)])
video = CompositeVideoClip([clip1,
clip2.set_pos((45,150)),
clip3.set_pos((90,100))])
clip2.set_pos((45,150)) # x=45, y=150 , in pixels
clip2.set_pos("center") # automatically centered
# clip2 is horizontally centered, and at the top of the picture
clip2.set_pos(("center","top"))
# clip2 is vertically centered, at the left of the picture
clip2.set_pos(("left","center"))
# clip2 is at 40% of the width, 70% of the height of the screen:
clip2.set_pos((0.4,0.7), relative=True)
# clip2's position is horizontally centered, and moving down !
clip2.set_pos(lambda t: ('center', 50+t) )
MoviePy会自动把你组合的音轨加到最终的 video clip 中,不需要手动组合
from moviepy.editor import *
# ... make some audio clips aclip1, aclip2, aclip3
concat = concatenate_audioclips([aclip1, aclip2, aclip3])
compo = CompositeAudioClip([aclip1.volumex(1.2),
aclip2.set_start(5), # start at t=5s
aclip3.set_start(9)])
moviepy.video.fx 和 moviepy.audio.fx 中有很多已经实现的效果,但是这些方法都不会在原来的剪辑上进行修改,而是生成一个新的剪辑
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
常用的特效可以使用一些一般化的方法而不是使用.fx()
,比如resize()
,使用.fx( vfx.resize, width)
就显得比较复杂,可以使用clip.resize()
设置起始时间的方式
clip.subclip(t_start,t_end)
t_start=230.54
t_start=(3,50.54) #(minutes, seconds)
t_start=(0,3,50.54) #(hour, min, sec)
t_start='00:03:50.54'
clip.subclip(-20, -10) #倒数20秒和倒数10秒
modifiedClip1 = my_clip.fl_time(lambda t: 3*t)
modifiedClip2 = my_clip.fl_time(lambda t: 1+sin(t))
modifiedClip1:以3倍的速度进行播放
modifiedClip2:在0到2秒之间进行摆动,这是一个不会停止的剪辑
def invert_green_blue(image):
return image[:,:,[0,2,1]]
modifiedClip = my_clip.fl_image( invert_green_blue )
def scroll(get_frame, t):
"""
This function returns a 'region' of the current frame.
The position of this region depends on the time.
"""
frame = get_frame(t)
frame_region = frame[int(t):int(t)+360,:]
return frame_region
modifiedClip = my_clip.fl( scroll )
能使用fl_time、fl_image实现效果就用fl_time或者fl_image,这样渲染的速度会很快
三种方式
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
这样可以导入所有的类,但是要花费大约1秒的时间,如果你想更快可以直接导入你需要的东西
比如
from moviepy.video.io.VideoFileClip import VideoFileClip
from moviepy.video.fx.resize import resize
my_clip.save_frame("frame.jpeg") # saves the first frame
my_clip.save_frame("frame.png", t=2) # saves the frame a t=2s
需要先安装pygame
显示某一帧
my_clip.show() # shows the first frame of the clip
my_clip.show(10.5) # shows the frame of the clip at t=10.5s
my_clip.show(10.5, interactive = True)
interactive=True
代表你可以在画面上点击以显示位置和像素的颜色,按esc可以退出
预览
my_clip.preview() # preview with default fps=15
my_clip.preview(fps=25)
my_clip.preview(fps=15, audio=False) # don't generate/play the audio.
my_audio_clip.preview(fps=22000)
通过ipython_display
方法来预览一个clip或者图片,或者MP4
ipython_display(my_video_clip) # embeds a video
ipython_display(my_imageclip) # embeds an image
ipython_display(my_audio_clip) # embeds a sound
ipython_display("my_picture.jpeg") # embeds an image
ipython_display("my_video.mp4") # embeds a video
ipython_display("my_sound.mp3") # embeds a sound
直接调用clip的方法
my_video_clip.ipython_display()
调整预览窗口的大小
ipython_display(my_clip, width=400) # HTML5 will resize to 400 pixels
从文件中获取
from moviepy.editor import *
audioclip = AudioFileClip("some_audiofile.mp3")
audioclip = AudioFileClip("some_video.avi")
从clip中获取
videoclip = VideoFileClip("some_video.avi")
audioclip = videoclip.audio
videoclip2 = videoclip.set_audio(my_audioclip)