One skill a day: easily complete video to gif moving picture in Python

I don't know if you have had a similar experience. When watching the video, you think it's very interesting. You want to make a moving picture, but you can't start! Or you can find some online tools on the Internet, but you need to pay more or less or have watermarks, then!  ?

Yes, today we'll learn to use Python to solve this demand!

Moving picture effect

catalog:

  • 1. Preparation

  • 2. Preliminary discussion

  • 3. Truncation area rotation diagram

  • 4. Fixed area rotation diagram

  • 5. Add custom text

1. Preparation

You need to prepare a video file for generating gif. I use the last Make a video download gadget for station B with Python (complete code is attached at the end of the text) >The video in the case. In addition, you need to use the movie library. For a more detailed introduction to this library, you can view its official documents.

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

Type pip from the command line to install it

pip install moviepy

That's step 1!

2. Preliminary discussion

This step is also the script for making the first moving picture at the beginning.

from moviepy.editor import *

video_path = "F:\PythonCool\video\[[spell back to war] Episode 20 Article 5 Wushuai is a little too much.mp4"
video = VideoFileClip(video_path)
clip = (video.subclip((2,4.5),(2,6.0))
        .resize(0.2))
clip.write_gif("The motion map is done.gif",fps=8)

Our original video material is 1920 * 1080 in size and 30 in frame rate.

Video properties

The parameters of this rotation diagram are set as follows:

  • subclip((2,4.5),(2,6.0) selects the entire video from 2.45 seconds to 2.60 seconds

  • The resize(0.2) size is changed to 0.2 of the original size, that is, 1 / 5 of the original size (after all, the file size of the moving picture size is too large)

  • write_gif("the motion picture is done. gif",fps=8) adjust the frame rate to 8 when saving gif

Based on the above parameter settings, this dynamic diagram is actually composed of (2.60-2.45) * 8 = 12 diagrams!

Dynamic graph attribute

3. Truncation area rotation diagram

Sometimes we only need to capture the specified rectangular area in the screen to make the moving picture, which requires the screenshot area of the {crop} function.

video.crop(
    x1=None,
    y1=None,
    x2=None,
    y2=None,
    width=None,
    height=None,
    x_center=None,
    y_center=None,
)

Parameters:

x1, y1: coordinates of the upper left corner of the rectangular area

x2, y2: coordinates of the lower right corner of the rectangular area

Width and height: width and height

x_center , y_center: indicates that the coordinate of x1 is x_center width / 2, the coordinate of x2 is x_center+width/2,y_center similar processing

For example, the code of the motion picture produced in the area where the screenshot character is located is as follows:

crop = (video.subclip((2,4.5),(2,6.0))
        .resize(0.2)
        .crop(x1=70,y1=0, x2=310,y2=216)) 
crop.write_gif("Cutting.gif",fps=8)

Cutting

Here, for the determination of the coordinates of the upper left corner and the lower right corner, you can preview the video by using the "Preview" method, and then click the "Preview" corresponding position to obtain it.

video.resize(0.2).preview()

4. Fixed area rotation diagram

After the above moving picture is completed, we want it to be resident. Then we can get the content of this moment through the screenshot, and then overwrite it to the original location.

snapshot = (crop
            # Screenshot area
            .crop(x1=100,y1=190, x2=140,y2=216) 
            # Intercepted time period (at 1 second)
            .to_ImageClip(1)
            # Setting position (upper left corner, consistent with the upper left corner of the interception area)
            .set_position((100, 190))
            .set_duration(crop.duration))
# merge
composition = CompositeVideoClip([crop, snapshot])
composition.write_gif('cover.gif', fps=8)

cover

5. Add custom text

For the above operation, we still have many methods to complete, such as covering with a completed picture (similar to the above case, but we don't need a separate screenshot, but read a ready-made one); In addition, we can also play by adding masks and custom text.

from moviepy.video.VideoClip import TextClip

# Mask
mask = (# Black mask with length and width of 40 * 20
     ColorClip((40, 20), (0, 0, 0))
     # Setting position (same as section 4)
        .set_pos((100, 190))
        .set_duration(crop.duration)
        )
# written words
text = (TextClip("It's done",
                 fontsize=30, color='white',
                 font='SimHei', interline=-25)
        .set_pos((80, 160))
        .set_duration(crop.duration))

composition = CompositeVideoClip([crop, mask, text])
composition.write_gif('cover.gif', fps=8)

Custom text

Keywords: Python

Added by cmzone on Mon, 24 Jan 2022 19:20:09 +0200