MP4 to.GIF Conveerter (Python Script)
GITHUB CODE:
https://github.com/darnokkeson/mp4-to-gif

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_4adithering 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-60recommended) - 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
- 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- 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/macOSUsage
python mp4_to_gif.py <input.mp4> <output.gif> <start_time> <end_time> [options]Note:
end_timeis an absolute timestamp, not a duration.
Formula:end_time = start_time + desired_duration
Command-Line Arguments
| Argument | Type | Description |
|---|---|---|
input | str | Path to input MP4 video |
output | str | Path for output GIF |
start | str | Start timestamp (e.g., 5.0 or 00:00:05.000) |
end | str | End timestamp (absolute, not duration) |
--fps | int | Target FPS (default: 24) |
--scale | str | Resize 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 15Tips & Best Practices
| Goal | Recommendation |
|---|---|
| Keep file size small | Use --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 fidelity | GIFs are limited to 256 colors. The two-pass method automatically extracts the best palette for your exact clip. |
| Find exact timestamps | Pause in VLC/QuickTime, or run:ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 video.mp4 |
Troubleshooting
| Error / Issue | Solution |
|---|---|
ffmpeg: command not found | Install FFmpeg and ensure it’s in your PATH (which ffmpeg or where ffmpeg) |
Invalid start/end time | Use decimal seconds (5.5) or HH:MM:SS.millis format. Ensure end > start. |
| GIF looks pixelated/blocked | Lower --fps, use --scale, or try --fps 24. High FPS + high res = compression artifacts. |
File size too large | Reduce 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+paletteusetwo-pass method for optimal GIF quality - Inspired by gifsicle and ezgif best practices
Powered by Qwen3.6-27B-MTP-Q6_K
