Home About Contact

 

How to convert video to gif with Python

2 min read

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 :

  • Reduce the size of the video by calling resize() and trim it with a specific start and end times by calling subclip() :
  • videoclip = VideoFileClip("Path-to-video-clip")
    videoclip = videoclip.resize(0.8).subclip(1.0, 1.5)

  • Reduce the FPS (frame rate) of the gif, you can specify the frame rate (fps) in the write_gif() method e.g. 10, which is more suitable for web display as follows
  • videoclip.write_gif("output.gif",fps=10)

    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[1])
        # 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[2])
    
    if len(sys.argv) > 4:
       time_start = float(sys.argv[3])
       time_end = float(sys.argv[4])
       videoclip = videoclip.subclip(time_start,time_end)		
    elif len(sys.argv) > 3:
       time_start = float(sys.argv[3])
       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[5]))
    		
    output_path = "output.gif"
    if len(sys.argv) > 6:
       output_path = sys.argv[6]
    
    videoclip.write_gif(output_path, fps=fps)

    The complete source code can be found on GitHub

    Spread the love
    Posted on December 17, 2020 By Christopher Chee

    Please leave us your comments below, if you find any errors or mistakes with this post. Or you have better idea to suggest for better result etc.


    Our FB Twitter Our IG Copyright © 2024