Create an animated gif from a video
animated gif

Hello, in this article we will continue to develop the video editing application which we have started a while ago. In this article, we will create an animated gif from a video, due to the size of the gif file which can get very large we will trim the duration of the video in this example by providing the starting time of that video which is very near to the end time of that video. We will hard code the video’s starting time in this example but we will retrieve the duration of the video in the next chapter so we do not need to worry about the overflow of the video’s starting time issue from any video. Just like before we will use both python and FFmpeg tool to create the animated gif from a video based on the video’s starting time to the video’s end time.

We will use two steps process to create the animated gif, first, we will resize the original video to the size which has been selected by the user from the drop-down combo box of the tkinter user-interface and then saved the resize video in a new file. Next, we will create the animated gif from the new video starting from a certain time period. Below is the entire program.

from tkinter import *
from tkinter import filedialog
import os
import subprocess
import tkinter.ttk as tk
win = Tk() # Create instance
win.title("Manipulate Video") # Add a title
win.resizable(0, 0) # Disable resizing the GUI
win.configure(background='white') # change background color
#  Create a label
aLabel = Label(win, text="Select video size and video", anchor="center", padx=13, pady=10, relief=RAISED)
aLabel.grid(column=0, row=0, sticky=W+E)
# Create a combo box
vid_size = StringVar() # create a string variable
preferSize = tk.Combobox(win, textvariable=vid_size) 
preferSize['values'] = (1920, 1280, 854, 640) # video width in pixels
preferSize.grid(column=0, row=1) # the position of combo box
preferSize.current(0) # select item one 
# Open a video file
def openVideo():
        fullfilename = filedialog.askopenfilename(initialdir="/", title="Select a file", filetypes=[("Video file", "*.mp4; *.avi ")]) # select a video file from the hard drive
        if(fullfilename != ''):
                scale_vid = preferSize.get() # retrieve value in the comno box
                new_size = str(scale_vid)
                dir_path = os.path.dirname(os.path.realpath(fullfilename))
                f = new_size  + '.mp4' # the new output file name/format
                f2 = f + '.gif' # animated gif
      ['ffmpeg', '-stream_loop', '2', '-i', fullfilename, '-vf', 'scale=' + new_size + ':-1', '-y', f]) # resize and loop the video with ffmpeg
      ['ffmpeg', '-i', fullfilename, '-vf', 'scale=' + new_size + ':-1', '-y', '-r', '24', f]) # resize and speed up the video with ffmpeg
      ['ffmpeg', '-i', fullfilename, '-vf', 'scale=' + new_size + ':-1', '-y', f]) # resize the video with ffmpeg
      ['ffmpeg', '-i', f, '-ss', '00:02:30', '-y', f2]) # create animated gif starting from 2 minutes and 30 seconds to the end
action_vid = Button(win, text="Open Video", command=openVideo, padx=2)
action_vid.grid(column=0, row=2, sticky=E+W)

As you can see the program has trimmed the length of the video by providing the starting point of the video file when creating the animated file. Below is the outcome.

There are lots of ant in the backyard

In the next chapter I will tidy up the application’s user interface and extract the video duration so we do not need to hard code the video’s starting time anymore.