MP4 to.GIF Conveerter (Python Script)

GITHUB CODE:
https://github.com/darnokkeson/mp4-to-gif

Convert captured MP4 videos into high-quality, optimized GIFs with precise in/out cuts.

MP4 to GIF Converter

A lightweight, zero-dependency Python script that converts video segments into high-quality, optimized GIFs using FFmpeg’s industry-standard two-pass palette generation. Supports precise in/out cuts, custom FPS, and smart scaling.


Features

  • High Quality: Two-pass palette generation + sierra2_4a dithering for minimal color banding
  • Precise Cutting: Frame-accurate in/out timestamps (accepts seconds or HH:MM:SS.millis)
  • Custom FPS: Set any target frame rate (24-60 recommended)
  • Smart Scaling: Optional resolution control to manage file size
  • Clean Execution: Auto-cleans temp files, validates inputs, and shows progress
  • CLI Ready: Simple command-line interface for scripting & automation
  • Zero Python Dependencies: Uses only Python’s standard library + FFmpeg

Prerequisites

  • Python 3.7+
  • FFmpeg installed and available in your system PATH

Installation

  1. Install FFmpeg (if not already installed):
   # macOS
   brew install ffmpeg

   # Ubuntu/Debian
   sudo apt update && sudo apt install ffmpeg

   # Windows
   winget install ffmpeg
   # Or download from https://ffmpeg.org/download.html
  1. Download the script:
   curl -O https://raw.githubusercontent.com/yourusername/mp4-to-gif/main/mp4_to_gif.py
   chmod +x mp4_to_gif.py  # Optional: makes it executable on Unix/macOS

Usage

python mp4_to_gif.py <input.mp4> <output.gif> <start_time> <end_time> [options]

Note: end_time is an absolute timestamp, not a duration.
Formula: end_time = start_time + desired_duration


Command-Line Arguments

ArgumentTypeDescription
inputstrPath to input MP4 video
outputstrPath for output GIF
startstrStart timestamp (e.g., 5.0 or 00:00:05.000)
endstrEnd timestamp (absolute, not duration)
--fpsintTarget FPS (default: 24)
--scalestrResize filter (e.g., 320:-1 for 320px width, keeps aspect ratio)

Examples

# Extract 3-second clip at 30 FPS, original resolution
python mp4_to_gif.py video.mp4 clip.gif 10.0 13.0 --fps 30

# 2-second clip, scaled to 480px width, 24 FPS
python mp4_to_gif.py input.mp4 demo.gif 00:00:05.500 00:00:07.500 --fps 24 --scale 480:-1

# Full video (0 to 15.5s) at 15 FPS
python mp4_to_gif.py full.mp4 full.gif 0 15.5 --fps 15

Tips & Best Practices

GoalRecommendation
Keep file size smallUse --scale 320:-1 or --scale 480:-1. GIFs grow exponentially with resolution & FPS.
Optimal playback--fps 24 to --fps 30 works best. Most browsers/platforms cap GIF playback at ~30-40 FPS.
Color fidelityGIFs are limited to 256 colors. The two-pass method automatically extracts the best palette for your exact clip.
Find exact timestampsPause in VLC/QuickTime, or run:
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 video.mp4

Troubleshooting

Error / IssueSolution
ffmpeg: command not foundInstall FFmpeg and ensure it’s in your PATH (which ffmpeg or where ffmpeg)
Invalid start/end timeUse decimal seconds (5.5) or HH:MM:SS.millis format. Ensure end > start.
GIF looks pixelated/blockedLower --fps, use --scale, or try --fps 24. High FPS + high res = compression artifacts.
File size too largeReduce duration, lower FPS, or add --scale 320:-1
Permission denied (Unix/macOS)Run chmod +x mp4_to_gif.py

Credits

  • Built on FFmpeg
  • Uses FFmpeg’s palettegen + paletteuse two-pass method for optimal GIF quality
  • Inspired by gifsicle and ezgif best practices

Powered by Qwen3.6-27B-MTP-Q6_K

Leave a Reply

Your email address will not be published. Required fields are marked *.