This project involves the development of a Python function called convert_to_gif. It takes a list of frames from a video and converts them into a GIF file.
Input Video link: -
https://drive.google.com/file/d/1KMOYmeU6HIW8IbnG4k1a4KBnJtIi2CgY/view?usp=sharing
Output Gif link: -
https://drive.google.com/file/d/1TyJu5iGMCZdyAQntUm9qRbN-uAkZCjST/view?usp=sharing
Screenshot: -
PRE-PREP:
1) .MP4 VIDEO FILE
2) .GIF EMPTY FILE
METHODOLOGY:
convert_mp4_to_gif
This function converts an MP4 video file to a GIF file.
Arguments:
mp4_path
(str): The path to the MP4 video file.gif_path
(str): The path to save the resulting GIF file.duration
(float, optional): The duration of the GIF in seconds. Defaults to None.frame_delay
(float, optional): The delay between frames in the GIF in seconds. Defaults to 0.1.resize_width
(int, optional): The desired width of the resized frames. Defaults to None.resize_height
(int, optional): The desired height of the resized frames. Defaults to None.The function first opens the MP4 video file specified by mp4_path
. It then reads each frame from the video, resizes the frame if resize_width
and resize_height
are provided, and converts the frame to the RGB color space.
The frames are stored in a list, frames_video
. The frame rate is calculated based on the frame_delay
. If the duration
is provided, the number of frames is calculated based on the duration and frame rate, and frames_video
is truncated accordingly.
Finally, the frames are saved as a GIF file using the imageio.mimsave()
function, with the specified gif_path
, frame delay, and format.
The function prints "GIF conversion complete!" when the conversion is finished.
DISCLAIMER:
Please note that the code assumes the video file is in the MP4 format and that the necessary dependencies are installed to run the code successfully.
Also, the store path of the .gif and .mp4 file should be same i.e same drive and folder.
for example,
VIDEO FILE PATH: "C:\Users\achau\input.mp4"
GIF PATH FILE: "C:\Users\achau\output.gif"
Submitted by Amarjeet Singh Chauhan (AJchauhan04)
Download packets of source code on Coders Packet
Comments