Conversion of video to gif using Python
Hello, in this tutorial we will learn how to convert video to gif in Python. For that, we use python.
Gifs are basically the compressed format of the video and they are used in places where very few colors are used, they are mostly used in logos as such. These gifs are compressed using lossless data compression in-order not to degrade the video quality.
Necessary libraries to convert video to gif in Python:
$ pip install MoviePy
Once the package is installed, the further process is simple.
Here is theĀ code to convert any videos into gifs:
from moviepy.editor import * clip = (VideoFileClip("ENTER THE FILE PATH HERE")) clip.write_gif("output.gif")
The above code works for any videos.
If you want to select a particular part of the video to make the gif then we use the method .subclip(), wherein you can select the start and the end of the video.
And this is the code for the above problem
from moviepy.editor import * clip = (VideoFileClip("PATH NAME").subclip((START TIME),(END TIME)) .resize(ACCORDING TO THE USER WISH)) clip.write_gif("output.gif")
from moviepy.editor import * clip = (VideoFileClip("/Users/nikhilgovind/Documents/input.mp4").subclip((22.65),(25.2)) .resize(0.3)) clip.write_gif("output.gif")
There are other methods to convert video to gifs here we use moviepy.
We can also try ffmpy method which is pretty simple as the one shown above.
Here is the actual video before converting:
Here is the gif:
This is how the conversion is done. As you can observe the quality of the gif is reduced compared to that of the video as well as the gif runs in an infinite loop and there is no control over it. The sound is also removed.
This is all about the conversion of a video into a gif. Hope this tutorial has helped.
Also, read:
I convert many wmv files to gif to share them through email body.
But, I generally use online conversion.
Today I tried this code.
On my case, the video is of 6 MB.And the converted gif from online is 15 MB.And, the converted gif from this code is 246 MB.
Both looks same quality.
Please suggest how to fix this.
you can specify the amount of fps.
clip.write_gif(“output.gif”, fps=15)
Same issue, the video size was 46 MB lol
But thanks very much for the intro, I had no idea how to do that and you made it super simple.
I tried resize but it was too small and the file still too big
Why does the clip var have the form of a tuple with a function inside?