Fail to Import Moviepy. Need Only for Video Upload.
Creating and exporting video clips¶
Video and audio clips are the cardinal objects of MoviePy. In this section we present the different sorts of clips, how to create them, and how to write them to a file. For informations on modifying a clip (cuts, furnishings, etc.), meet Clips transformations and furnishings. For how to put clips together see Mixing clips and to see how to preview clips before writing a file, refer to How to exist efficient with MoviePy.
The following code summarizes the base clips that you tin can create with moviepy:
# VIDEO CLIPS clip = VideoClip ( make_frame , elapsing = 4 ) # for custom animations (run into beneath) 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 = lxx , colour = "blackness" ) clip = ColorClip ( size = ( 460 , 380 ), color = [ R , One thousand , B ]) # Audio CLIPS prune = AudioFileClip ( "my_audiofile.mp3" ) # or .ogg, .wav... or a video ! prune = AudioArrayClip ( numpy_array , fps = 44100 ) # from a numerical array clip = AudioClip ( make_frame , elapsing = iii ) # uses a function make_frame(t)
The best to sympathise these clips is to read the total documentation for each in the Reference Manual. The adjacent sections In this section we see how to create clips, (for case from video or sound files), how to mix them together, and how to write them to a file.
Categories of video clips¶
Video clips are the building blocks of longer videos. Technically, they are clips with a prune.get_frame(t)
method which outputs a HxWx3 numpy array representing the frame of the clip at fourth dimension t. There are two main categories: animated clips (made with VideoFileClip
and VideoClip
) and unanimated clips which show the same picture for an a-priori infinite duration ( ImageClip
, TextClip
,``ColorClip``). There are also special video clips call masks, which belong to the categories in a higher place but output greyscale frames indicating which parts of another clip are visible or non. A video clip tin can carry effectually an sound prune ( clip.audio
) which is its soundtrack, and a mask clip.
VideoClip¶
VideoClip
is the base course for all the other video clips in MoviePy. If all you want is to edit video files, you lot will never need it. This class is applied when you desire to make animations from frames that are generated by another library. All you need is to define a office make_frame(t)
which returns a HxWx3 numpy assortment (of eight-bits integers) representing the frame at fourth dimension t. Here is an example with the graphics library Gizeh:
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 fourth dimension circle = gizeh . circle ( radius , xy = ( 64 , 64 ), fill = ( 1 , 0 , 0 )) circle . describe ( surface ) render surface . get_npimage () # returns a 8-fleck RGB array prune = mpy . VideoClip ( make_frame , duration = 2 ) # two seconds clip . write_gif ( "circle.gif" , fps = 15 )
Note that clips brand with a make_frame do not have an explicit frame rate, so you must provide a frame charge per unit ( fps
, frames er second) for write_gif
and write_videofile
, and more than generally for any methods that requires iterating through the frames.
VideoFileClip¶
A VideoFileClip is a clip read from a video file (most formats are supported) or a GIF file. You load the video as follows:
myclip = VideoFileClip ( "some_video.avi" ) myclip = VideoFileClip ( "some_animation.gif" )
Note that these clips will have an fps
(frame per second) aspect, which will be transmitted if you do small modifications of the clip, and volition be used by default in write_videofile
, write_gif
, etc. For case:
myclip = VideoFileClip ( "some_video.avi" ) print ( myclip . fps ) # prints for instance '30' # Now cutting the clip between t=10 and 25 secs. This conserves the fps. myclip2 = myclip . subclip ( 10 , 25 ) myclip2 . write_gif ( "test.gif" ) # the gif will have 30 fps
For more, run into VideoFileClip
.
ImageSequenceClip¶
This is a clip fabricated from a serial of images, you call it with
clip = ImageSequenceClip ( images_list , fps = 25 )
where images_list
can be either a listing of image names (that will be played) in that order, a folder proper name (at which instance all the image files in the binder will be played in alphanumerical lodge), or a listing of frames (Numpy arrays), obtained for instance from other clips.
When yous provide a folder name or listing of file names, you can choose load_images=Truthful
to specify that all images should be loaded into the RAM. This is only interesting if you have a pocket-size number of images that volition be each used more than once (eastward.g. if the images grade a looping animation).
ImageClip¶
An ImageClip is a video prune that always displays the aforementioned image. You can create one every bit follows:
myclip = ImageClip ( "some_picture.jpeg" ) myclip = ImageClip ( somme_array ) # a (summit 10 width x 3) RGB numpy assortment myclip = some_video_clip . to_ImageClip ( t = '01:00:00' ) # frame at t=1 hr.
For more, run across ImageClip
.
Ii examples of ImageClip shown below are the TextClip and ColorClip
TextClip¶
Generating a TextClip requires to take ImageMagick installed and (for windows users) linked to MoviePy, see the installation instructions.
Here is how you make a textclip (you won't need all these options all the fourth dimension):
myclip = TextClip ( "Hello" , font = 'Amiri-Bold' )
The font can be any font installed on your computer, but ImageMagick will accept specific names for it. For instance the normal Amiri font will be chosen Amiri-Regular
while the Impact font volition be chosen Impact-Normal
. To get a list of the possible fonts, blazon TextClip.list('font')
. To find all the font names related to a given font, apply for instance
TextClip . search ( 'Amiri' , 'font' ) # Returns all font names containing Amiri
Note also that the use of a stroke (or contour) will non work well on small letters, so if you need a small text with a profile, it is meliorate to generate a big text, and then downsize it:
myclip = TextClip ( "Hello" , fontsize = 70 , stroke_width = 5 ) . resize ( height = 15 )
TextClips have many, many options: alignment, kerning (altitude between the letters), stroke size, background, word wrapping, etc. run across TextClip
for more.
Mask clips¶
A mask is a special video clip which indicates which pixels will be visible when a video clip conveying this mask will exist composed with other video clips (encounter Mixing clips). Masks are also used to define transparency when yous export the prune every bit GIF file or as a PNG.
The central difference between masks and standard clips is that standard clips output frames with iii components (R-G-B) per pixel, comprised betwixt 0 and 255, while a mask has just one composant per pixel, between 0 and 1 (1 indicating a fully visible pixel and 0 a transparent pixel). Seen otherwise, a mask is always in greyscale.
When you create or load a clip that yous will employ as a mask you need to declare information technology:
maskclip = VideoClip ( makeframe , elapsing = 4 , ismask = Truthful ) maskclip = ImageClip ( "my_mask.jpeg" , ismask = True ) maskclip = VideoFileClip ( "myvideo.mp4" , ismask = Truthful )
In the case of video and image files, if these are not already black and white they volition be converted automatically.
And then yous attach this mask to a prune (which must have the same dimensions) with myclip.set_mask(maskclip)
.
Some epitome formats like PNG support transparency with an alpha layer, which MoviePy volition utilize equally a mask:
myclip = ImageClip ( "image.png" , transparent = Truthful ) # True is the default myclip . mask # <- the alpha layer of the movie.
Whatsoever video clip can be turned into a mask with clip.to_mask()
, and a mask tin be turned to a standard RGB video clip with my_mask_clip.to_RGB()
.
Masks are treated differently past many methods (because their frames are dissimilar) but you tin do with a mask pretty much everything you lot can do with a standard clip: you tin cutting it, edit it, preview it, write it to a video file, brand snapshots, etc.
Exporting video clips¶
Video files (.mp4, .webm, .ogv…)¶
To write a clip as a video file, use
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 ( "pic.webm" , sound = Fake ) # don't return audio.
MoviePy has default codec names for the about common file extensions. If you desire to use exotic formats or if y'all are not happy with the defaults y'all tin provide the codec with codec='mpeg4'
for case. There are many many options when y'all are writing a video (bitrate, parameters of the audio writing, file size optimization, number of processors to use, etc.). Please refer to write_videofile()
for more than.
Sometimes it is impossible for MoviePy to judge the duration
attribute of the clip (keep in mind that some clips, similar ImageClips displaying a picture, have a priori an infinite duration). So, the duration
must be gear up manually with prune.set_duration
:
# Make a video showing a bloom for 5 seconds my_clip = Image ( "bloom.jpeg" ) # has infinite duration my_clip . write_videofile ( "flower.mp4" ) # Will neglect ! NO DURATION ! my_clip . set_duration ( 5 ) . write_videofile ( "flower.mp4" ) # works !
Animated GIFs¶
To write your video as an animated GIF, use
my_clip . write_gif ( 'test.gif' , fps = 12 )
Note that this requires ImageMagick installed. Otherwise you can also create the GIF with ffmpeg by adding the option program='ffmpeg'
, information technology will be much faster only won't look as nice and won't exist optimized.
There are many options to optimize the quality and size of a gif. Delight refer to write_gif()
.
Note that for editing gifs the all-time way is to preview them in the notebook as explained here: ipython_display
For examples of use, see this blog postal service for informations on making GIFs from video files, and this other post for GIF animations with vector graphics.
Consign images¶
Y'all can write a frame to an epitome file with
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
If the prune has a mask information technology will exist exported every bit the alpha layer of the image unless you specify withmask=Simulated
.
colmenerocoor1989.blogspot.com
Source: https://zulko.github.io/moviepy/getting_started/videoclips.html
Post a Comment for "Fail to Import Moviepy. Need Only for Video Upload."