Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report