Created
February 24, 2025 17:10
-
-
Save idcesares/933943e300258faa4802eb7fe67b4621 to your computer and use it in GitHub Desktop.
π¬ A Python script to convert a VIDEO to GIF using the moviepy library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# video_to_gif.py | |
# ------------------------------------------------- | |
# π This script converts any video file to an HD GIF. | |
# π It uses the moviepy library for video processing. | |
# π¨ Includes options for resizing, trimming, and optimizing. | |
# ------------------------------------------------- | |
# Step 1: Install dependencies (run these in your terminal if needed) | |
# pip install moviepy | |
from moviepy.editor import VideoFileClip # Handles video processing | |
import os | |
def video_to_gif(video_path, output_gif="output.gif", fps=15, start_time=None, end_time=None, height=720): | |
""" | |
Converts a video file to a high-definition GIF. | |
Parameters: | |
- video_path (str): Path to the video file. | |
- output_gif (str): Filename for the output GIF. | |
- fps (int): Frames per second for the GIF (higher means smoother animation). | |
- start_time (float): Start time in seconds for trimming (optional). | |
- end_time (float): End time in seconds for trimming (optional). | |
- height (int): Desired height of the output GIF (maintains aspect ratio). | |
Returns: | |
- None (Outputs a GIF file in the same directory) | |
""" | |
# Step 2: Load the video file | |
print(f"π Loading video file: {video_path}") | |
clip = VideoFileClip(video_path) | |
# Step 3: (Optional) Trim the video | |
if start_time is not None and end_time is not None: | |
print(f"βοΈ Trimming video from {start_time}s to {end_time}s.") | |
clip = clip.subclip(start_time, end_time) | |
# Step 4: Resize for HD output (while keeping aspect ratio) | |
if height is not None: | |
print(f"πΌοΈ Resizing video to height {height}px (HD).") | |
clip = clip.resize(height=height) | |
# Step 5: Convert video to GIF | |
print(f"π¬ Converting video to GIF: {output_gif}") | |
clip.write_gif(output_gif, fps=fps, program='ffmpeg', opt='nq') | |
print(f"β GIF saved as {output_gif}") | |
if __name__ == "__main__": | |
# π§ CONFIGURATION: Customize these values if needed | |
video_file = input("π₯ Enter the path to the video file (e.g., video.mp4): ") | |
output_gif_file = input("πΎ Enter the output GIF filename (default: output.gif): ") or "output.gif" | |
fps = int(input("ποΈ Enter desired FPS (default 15 for smoother playback): ") or 15) | |
height = int(input("πΌοΈ Enter desired GIF height in pixels (default 720 for HD): ") or 720) | |
# (Optional) Trim settings | |
trim = input("βοΈ Do you want to trim the video? (y/n): ").lower() | |
start = end = None | |
if trim == 'y': | |
start = float(input("β° Enter start time in seconds: ")) | |
end = float(input("β³ Enter end time in seconds: ")) | |
# Run the conversion | |
video_to_gif(video_file, output_gif=output_gif_file, fps=fps, start_time=start, end_time=end, height=height) | |
# π Final success message | |
print(f"\nπ All done! Your GIF is saved as '{output_gif_file}' in {os.getcwd()}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment