Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Video conversion Gif with a few simple Lines of python programming

Shulou Source: shulou.com Published: 2022-06-03 07:57:02 09月15日 Update

In this issue, the editor will bring you about python programming simple lines of code how to achieve video conversion Gif, the article is rich in content and professional analysis and description for you, I hope you can get something after reading this article.

I. Preface

Many websites offer the function of converting video to GIF, but either charge or have advertising.

In fact, through python, we can convert video to gif in a few lines of code.

Second, tutorial 1. Install the prerequisite library moviepypip install moviepy-I https://pypi.tuna.tsinghua.edu.cn/simple2. Write the code from moviepy.editor import * clip = (VideoFileClip ("movie.mp4")) # the video file path clip.write_gif ("movie.gif") 3 that needs to be converted to GIF. Conversion effect

The GIF diagram just now is only a few seconds old, but its size is as large as 9m!

(or if the resolution has been scaled)

If the video needs to be converted for dozens of seconds, the file size will have to be 100 megabytes or more!

What should we do about it?

4. GIF is a big solution.

In addition to setting the zoom resolution resize, we can also reduce the size by setting the fps parameter to extract frames.

From moviepy.editor import * clip = (VideoFileClip ("movie.mp4") .resize ((488225)) clip.write_gif ("movie.gif", fps=15) # set to 15 frames per second

After setting to 15 frames per second, the file size is only more than 2m, which is as much as four times smaller!

And there is not much difference in visual effects.

5. Intercept video length conversion

We can also specify the converted video range by setting the subclip parameter:

Subclip: capture video clips from t_start to t_end in the original video

Convert video clips in 1-2 seconds to Gif

From moviepy.editor import * clip = (VideoFileClip ("movie.mp4") .subclip (t_start=1, t_end=2). Resize ((488,225)) clip.write_gif ("movie.gif", fps=15) 6. Specify the size of the converted picture (resolution)

The resize parameter specifies the size of the converted picture.

The accepted parameters are:

Expressed in pixels or floating point (width,height)

Zoom percentage, such as 0.5

Example

1. Set the converted picture to 600 '400.

Clip = (VideoFileClip ("movie.mp4") .resize ((600,400))

two。 The original video is scaled by 50%

Clip = (VideoFileClip ("movie.mp4"). Resize (0.5)) the above is how to convert video into Gif by programming a few simple lines of python for everyone. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Video size code parameters resolution pictures files programming content only can pass effects more fragments or analysis large professional small and medium pixels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Information vpn OPPO Reno Apple