Provide you with a tool code, using Python, to convert MP4 format video files into GIF images
First install the necessary packages:
pip install imageio pip install imageio[ffmpeg]
Tool code:
import imageio # Video file pathvideo_path = r"" # Output GIF file pathgif_path = "" # Create a video read objectreader = imageio.get_reader(video_path) # Get the frame rate and frame count of the videofps = reader.get_meta_data()['fps'] num_frames = reader.count_frames() # Set the frame rate of the GIF to be saved (can be adjusted as needed)gif_fps = 5 # Calculate the index interval of the frame to be selectedinterval = int(fps / gif_fps) # Create a list to store the frames to be savedgif_frames = [] # traverse the video frames and select some frames to save them in the listfor i, frame in enumerate(reader): if i % interval == 0: # Add frame to the list gif_frames.append(frame) # Save the frame list as a GIF(gif_path, gif_frames, fps=gif_fps, duration=0.1, loop=0) print(f"GIF Saved to {gif_path}")
The variable video_path is the path where the MP4 video file is located. Just modify the address of your MP4 video file here.
The final output file is
If you want to save GIF imagesDon't play it loop, just put the last line of code inloop=0Just remove it.
This is the article about how to convert MP4 files into GIF images in Python. For more related python mp4 files into gif content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!