How to get millions of likes? It's so easy to make shake video with Python

2022-11-22

Source: About data analysis and visualization

In the last tutorial, we talked about the application of Python in image processing by calling the opencv module, so today I'll share with you that Python can also be used to create videos, that is, by calling the moviepy module.

About Moviepy module
Moviepy is a Python module for video editing, which can be used to do some basic video editing operations, such as video splicing, audio-video compositing, adding some basic transitions, etc. It can read most formats of video files, including MP4 and GIF.

So let's first install the module with the pip command

pip install moviepy
There are differences in the use of different versions of moviepy, here I'm using version 1.0.1

Making a video
After installing the module, let's simply create a video, the steps are also very simple, we read a number of photos, combine them into a video, and add a simple transition effect. The code is as follows

import os from moviepy.editor import * from moviepy.video.compositing.transitions import crossfadein

filelist = os.listdir(". /images/")
clips_list = [] for item in filelist: if item.endswith('.png') or item.endswith('.jpg'): # determine if the image suffix is .png photo_path = ". /images/{}".format(item)
        clips1 = ImageClip(photo_path).set_duration(0.5).fx(crossfadein, 1)
        clips_list.append(clips1)

video_clip = concatenate_videoclips(clips_list, method="compose")
video_clip.write_videofile("test.mp4", fps=24, remove_temp=True)
output

Since it involves the addition of transitions, here we refer to the fade in/fade out transitions in moviepy.video.compositing.transitions, and from the overall code logic, we add the transitions after reading the image The duration of the transition is 0.5 seconds, and finally we combine the images with the transition effect into one video and save it to the specified path.

Add background music
After the above practice, our composite video is without background music, the moviepy module can also add BGM to the video, the code is as follows

videoclip = VideoFileClip("video.mp4")
audioclip = AudioFileClip("audio.mp3")
finalclip = videoclip.set_audio(audioclip)
finalclip.write_videofile("final_result.mp4", fps=60, remove_temp=True, codec="libx264")
output

For the version of moviepy I'm using, I need to modify some of the source code to successfully add audio to the video, the specific location is in site-packagesmoviepyvideoioffmpeg_writer.py in line 86 to remove the -an parameter

Secondary editing of video
We can also create a second edit to an existing video, such as scaling, flipping, etc. For example, to flip the video vertically or horizontally, the code is as follows

clip1 = VideoFileClip("video.mp4") clip2 = clip1.fx(vfx.mirror_x) clip3 = clip1.fx(vfx.mirror_y)
Of course we can also scale the video, for example by 60%, with the following code

clip4 = clip1.resize(0.60)
All the code is shown below

from moviepy.editor import VideoFileClip, clips_array, vfx
clip1 = VideoFileClip("out.mp4").margin(10) # add gap clip2 = clip1.fx(vfx.mirror_x)
clip3 = clip1.fx(vfx.mirror_y)
clip4 = clip1.resize(0.60) # shrink by 60% final_clip = clips_array([[clip1, clip2],
                          [clip3, clip4]])
final_clip.resize(width=480).write_videofile("my_stack.mp4")
output

If we want to take a screenshot of a part of the video, we call the subclip() method with the following code

clip = VideoFileClip("video.mp4")
clip_2 = clip.subclip(5, 10) # capture a 5-10 second section clip_2.write_videofile("video_2.mp4")
Creating a motion picture
Finally the moviepy module can also be combined with the matplotlib visualization module to create a motion picture, by customizing a function to generate a frame by frame picture, and finally a gif motion picture, with the following code

x = np.linspace(-5, 5, 200) duration = 5 fig, ax = plt.subplots() def build_frame(t): ax.clear() ax.plot(x, np.sin(x**2) + np.sinc(x + 2*np.pi/ duration * t), lw=5) ax.set_ylim(-2.5, 2.5) return mplfig_to_npimage(fig) animation = VideoClip(build_frame, duration=duration) animation. write_gif('movie_matplotlib.gif', fps=60) 
output

Thanks for watching

Join Us

Company/Organization Name:

Company/Organization Site:

Candidate Name:

Candidate Job:

Tel:

Email:

Admission Remarks: (cause and appeal of admission)

Submit application