GIF CONVERSION SCRIPT
Converting .gif files to .mp4 can be beneficial when you need to share animated content more widely. MP4 is a widely supported video format compatible with most devices and platforms, including social media platforms, while .gif support can be limited. Additionally, .mp4 allows for higher quality video with features like audio and smoother playback compared to .gif files, which can often have lower resolution and limited animation complexity.
This Python program leverages the imagio module for efficient image and video handling. imagio provides tools for reading the .gif file, which is inherently a sequence of still images. The program processes each frame of the .gif, potentially applying necessary adjustments or enhancements. Finally, imagio assembles these processed frames into a smooth video sequence and exports it as an .mp4 file.
Note: Be sure to install the Python imageio package on your computer in order to utilize this script.
# Python GIF to MP4 Converter Script
# by M4TTBIT
import imageio
imageio.plugins.ffmpeg.download()
def gif_to_mp4(gif_path, mp4_path, loop_count):
"""
Converts a GIF to an MP4 video with specified loop count.
Args:
gif_path: Path to the input GIF file.
mp4_path: Path to the output MP4 file.
loop_count: Number of times to loop the video.
"""
with imageio.get_reader(gif_path) as reader:
# If no metadata, 2 fps is default
fps = reader.get_meta_data().get('fps', 2)
writer = imageio.get_writer(mp4_path, fps=fps)
for frame in reader:
writer.append_data(frame)
# Repeat the frames for the desired number of loops
for _ in range(loop_count - 1):
for frame in reader:
writer.append_data(frame)
writer.close()
# Example usage:
gif_file = '~/Script/MonoChromeCrew.gif'
mp4_file = '~/Script/MonoChromeCrew.mp4'
desired_loops = 4
gif_to_mp4(gif_file, mp4_file, desired_loops)