Coders Packet

video to gif converter using python

By Bokkasam Eswarsai

Python Video to GIF Converter: Transform Videos into Animated GIFs Effortlessly with Customizable FPS, Width, and Height - moviepy Library Magic!

VIDEO TO GIF CONVERTER USING PYTHON

Step-1 : import moviepy

pip install moviepy
Requirement already satisfied: moviepy in c:\users\eswarsai\anaconda3\lib\site-packages (1.0.3)
Requirement already satisfied: proglog<=1.0.0 in c:\users\eswarsai\anaconda3\lib\site-packages (from moviepy) (0.1.10)
Requirement already satisfied: numpy>=1.17.3 in c:\users\eswarsai\anaconda3\lib\site-packages (from moviepy) (1.21.5)
Requirement already satisfied: requests<3.0,>=2.8.1 in c:\users\eswarsai\anaconda3\lib\site-packages (from moviepy) (2.28.1)
Requirement already satisfied: tqdm<5.0,>=4.11.2 in c:\users\eswarsai\anaconda3\lib\site-packages (from moviepy) (4.64.1)
Requirement already satisfied: imageio<3.0,>=2.5 in c:\users\eswarsai\anaconda3\lib\site-packages (from moviepy) (2.19.3)
Requirement already satisfied: imageio-ffmpeg>=0.2.0 in c:\users\eswarsai\anaconda3\lib\site-packages (from moviepy) (0.4.8)
Requirement already satisfied: decorator<5.0,>=4.0.2 in c:\users\eswarsai\anaconda3\lib\site-packages (from moviepy) (4.4.2)
Requirement already satisfied: pillow>=8.3.2 in c:\users\eswarsai\anaconda3\lib\site-packages (from imageio<3.0,>=2.5->moviepy) (9.2.0)
Requirement already satisfied: charset-normalizer<3,>=2 in c:\users\eswarsai\anaconda3\lib\site-packages (from requests<3.0,>=2.8.1->moviepy) (2.0.4)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\eswarsai\anaconda3\lib\site-packages (from requests<3.0,>=2.8.1->moviepy) (2022.9.14)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\eswarsai\anaconda3\lib\site-packages (from requests<3.0,>=2.8.1->moviepy) (1.26.11)
Requirement already satisfied: idna<4,>=2.5 in c:\users\eswarsai\anaconda3\lib\site-packages (from requests<3.0,>=2.8.1->moviepy) (3.3)
Requirement already satisfied: colorama in c:\users\eswarsai\anaconda3\lib\site-packages (from tqdm<5.0,>=4.11.2->moviepy) (0.4.5)
Note: you may need to restart the kernel to use updated packages.

import moviepy library
from moviepy.editor import *
def video_to_gif_converter(input_video_file, output_gif_file, frames_per_second=20,new_width=None,new_height=None):
    video = VideoFileClip(input_video_file)
    video = video.set_fps(frames_per_second)
    video_width, video_height = video.size
    if new_width is not None and new_height is not None:
        video = video.resize(width=new_width, height=new_height)
    elif new_width is not None:
        aspect_ratio = video.width / video.height
        new_height = int(new_width / aspect_ratio)
        video = video.resize(width=new_width, height=new_height)
    elif new_height is not None:
        aspect_ratio = video.width / video.height
        new_width = int(new_height * aspect_ratio)
        video = video.resize(width=new_width, height=new_height)
    video.write_gif(output_gif_file)

code

RESULT

Output

---------------------------------------------------------------------------------------------------------------------------------------------------------------Detail Explanation of Code:

The video to GIF converter using Python is a script that utilizes the `moviepy` library to convert a video file into an animated GIF. The script allows users to customize the frames per second (FPS), width, and height of the resulting GIF.

Here's a brief description of how the converter works:

1. Importing Required Libraries: The script starts by importing the necessary modules from the `moviepy.editor` package, which enables video editing capabilities.

2. Function Definition: The core functionality of the converter is encapsulated within the `video_to_gif_converter` function. This function takes several parameters, including the input video file, output GIF file, desired FPS, width, and height.

3. Video Loading and FPS Setting: The function loads the input video file using `VideoFileClip`. It then sets the frames per second (FPS) for the resulting GIF using the `set_fps` method.

4. Dimension Adjustment: Users can optionally customize the width and height of the GIF. If either `new_width` or `new_height` is provided, the script resizes the video while maintaining the original aspect ratio. If both `new_width` and `new_height` are provided, the script performs the resizing directly.

5. GIF Generation: After the adjustments, the script writes the modified video as an animated GIF using the `write_gif` method, saving it to the specified output file path.

6. Main Execution: In the `if __name__ == "__main__":` block, the user can specify the input video file, output GIF file, desired FPS, width, and height. The script then calls the `video_to_gif_converter` function with the provided parameters to perform the conversion.

By using this Python-based video to GIF converter, users can easily tailor the resulting GIF to meet their preferences, such as changing its frame rate, dimensions, and even positioning elements in the middle or center of the GIF. The `moviepy` library simplifies the process of video manipulation and GIF creation, making it accessible and convenient for developers and users alike.

 

INPUT VIDEO LINK:

https://drive.google.com/file/d/1lAM5XXoZY0R2smVrnXuPYd5OcWOQjeLH/view?usp=sharing

 

OUTPUT GIF LINK:

https://drive.google.com/file/d/12vUcT4mwovuZlFWvLZxIjSYeXdtqsYQW/view?usp=sharing

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Bokkasam Eswarsai (Eswarsai26)

Download packets of source code on Coders Packet