Video to GIF Coverter

Goal: Converting video to GIF.

Process:

  1. Installing and import prerequsite packages.
  2. Defining a variable with path to video that we wna't convert.
  3. Defining and calling the function to convert video.

I hope you enjoy this.

Installing prerequisite packages

In [1]:
!pip install imageio imageio -ffmpeg
!pip install tqdm
Looking in links: fmpeg
Requirement already satisfied: imageio in c:\users\saluj\appdata\local\continuum\anaconda3\lib\site-packages (2.5.0)
Requirement already satisfied: tqdm in c:\users\saluj\appdata\local\continuum\anaconda3\lib\site-packages (4.31.1)

Importing necessary packages

In [2]:
import imageio
import os   #This module provides a portable way of using operating system dependent functionality. 
from tqdm import tqdm_notebook as tqdm #used for progress bar in loops

Defining a variable via path locater using os

In [3]:
#mention you video file name below
clip = os.path.abspath('Video.mp4')

Defining function to make GIF

In [4]:
def gifMaker(inputPath, targetFormat):
    
    #defining the output file name
    outputPath = os.path.splitext(inputPath)[0] + targetFormat
    
    print(f'Converting {inputPath} \n to {outputPath}')
    
    reader = imageio.get_reader(inputPath)
    fps = reader.get_meta_data()['fps']
    
    writer = imageio.get_writer(outputPath, fps=fps)
    
    #for loop to append the frames in the video
    for frames in tqdm(reader):
        writer.append_data(frames)
    
    print ('Video Converted to GIF!')
    writer.close

Calling out the function we just created

In [5]:
gifMaker(clip, '.gif')
Converting C:\Users\saluj\Desktop\My Website Data\Python\Done\Video to GIF Converter\Video.mp4 
 to C:\Users\saluj\Desktop\My Website Data\Python\Done\Video to GIF Converter\Video.gif
Video Converted to GIF!

Author: Amandeep Saluja