moviepy video processing library uses Detailed

moviepy Profile

moviepy can cut, merge, insert title and other processes on the audio, video, pictures and git, and supports a variety of formats.

moviepy also based on ffmpeg, ffmpeg installed if not, moviepy will automatically download and install ffmpeg use moviepy the first time, if the machine is installed ffmpeg, proposed to amend the config_defaults.pyfile to configureFFMPEG_BINARY = 'auto-detect'

As for the other tools, it is the corresponding appropriate tools to decide whether or not to install, such as adding text requires ImageMagick , preview audio and video needs PyGame

moviepy use

core object moviepy that clipscan be AudioClipsorVideoClips

create clips

      
      
1
2
3
4
5
6
7
8
9
10
11
12
      
      
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 (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 al 大专栏  moviepy视频处理库使用详解l 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

Reference Documents

Guess you like

Origin www.cnblogs.com/dajunjun/p/11717825.html