It’s often that I need to convert videos of screen recordings to GIFs, while authoring this blog. Therefore, I’d like to explore how to convert video to gif with Python. Python comes with lots of libraries. To convert video to gif, the MoviePy simply comes in handy, which you can have it done in just few lines of code.
To get started with MoviePy, first of all, you’ll need to install it as follows:
python -m pip install moviepy
To convert a video to gif basically, you just need to import MoviePy and create an instance of MoviePy and supply it with the path to your video clip and call the method write_gif() and provide it a name of the output file:
from moviepy.editor import VideoFileClip videoclip = VideoFileClip("Path to your video clip") videoclip.write_gif("output.gif")
The above code will run as follows:
By simply doing the above, it’ll produce a gif that is sometimes too big for the web. In order to reduce the size, you can do the followings such as :
videoclip = VideoFileClip("Path-to-video-clip") videoclip = videoclip.resize(0.8).subclip(1.0, 1.5)
Let’s build a simple command line tool for video to gif conversion
It’s a good idea to build a simple command line tool for video to gif conversion, so it’ll come in handy when needed.
The simple command line tool will be supplied with various optional arguments for its frame rate, start and end times in seconds and the fraction of its size to reduce to etc. And it shall be run as follows:
python videotogif_cml.py [video_path] [fps] [time_start] [time_end] [resize_to] [output_path]
We need Sys module to read the arguments from the command line
from moviepy.editor import VideoFileClip import sys if len(sys.argv) < 2 : print("Usage : python videotogif_cml.py [video_path] [fps] [time_start] [time_end] [resize_to] [ouput_path]") sys.exit() else: print("Start converting...") videoclip = VideoFileClip( sys.argv) # the following code will handle the optional arguments # and convert video to gif accordingly here...
The above code checks if the user has supplied at least two arguments, will display a usage instruction and exit if not.
Please note that argument zero is the python script itself, argument one is the video path and from two onwards are only the arguments for the options of conversion accordingly.
The rest of the "else" block continues as follows which checks if each optional argument is provided and is given a default value if it's not. And finally, call the write_gif() method for the conversion.
fps = 10 if len(sys.argv) > 2: fps = int(sys.argv) if len(sys.argv) > 4: time_start = float(sys.argv) time_end = float(sys.argv) videoclip = videoclip.subclip(time_start,time_end) elif len(sys.argv) > 3: time_start = float(sys.argv) videoclip = videoclip.subclip(time_start) # if time_end is not provided, # it is assumed to be the duration of the clip if len(sys.argv) > 5: videoclip = videoclip.resize(float(sys.argv)) output_path = "output.gif" if len(sys.argv) > 6: output_path = sys.argv videoclip.write_gif(output_path, fps=fps)
The complete source code can be found on GitHub