Last active
December 22, 2025 09:17
-
-
Save cumulus13/86b8138ca9567fb2f8d5fb774939d594 to your computer and use it in GitHub Desktop.
MP4 to GIF with FFMPEG
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
| :: File: mp4togif.bat | |
| :: Author: Hadi Cahyadi <cumulus13@gmail.com> | |
| :: Date: 2025-12-22 | |
| :: Description: | |
| :: License: MIT | |
| @echo off | |
| setlocal | |
| :: --- Mandatory Input Checking --- | |
| if "%~1" == "" ( | |
| echo. | |
| echo β ERROR: You must provide an input file. | |
| echo. | |
| goto :eof | |
| ) | |
| :: --- Input Variable Settings --- | |
| set "INPUT_FILE=%~1" | |
| :: Output default: Input Name without extention (e.g., color-notify) | |
| set "OUTPUT_NAME=%~n1" | |
| :: --- Simplified Output Logic --- | |
| :: IF argument %2 EXISTS (NOT EMPTY), OVERRIDE OUTPUT_NAME | |
| if not "%~2" == "" set "OUTPUT_NAME=%~n2" | |
| :: Add .gif extension to output name (always) | |
| set "OUTPUT_FILE=%OUTPUT_NAME%.gif" | |
| :: --- GIF Parameter Settings --- | |
| set "START_TIME=5" | |
| set "DURATION=15" | |
| set "FPS=12" | |
| set "WIDTH=1280" | |
| :: --- FFmpeg command --- | |
| echo. | |
| echo π¬ Processing Input: "%INPUT_FILE%" | |
| echo πΌοΈ The output will be saved as: "%OUTPUT_FILE%" | |
| echo π Duration: %DURATION% seconds (Starting from %START_TIME% second) | |
| echo βοΈ Generate GIFs... | |
| echo. | |
| ffmpeg ^ | |
| -ss %START_TIME% ^ | |
| -t %DURATION% ^ | |
| -i "%INPUT_FILE%" ^ | |
| -vf "fps=%FPS%,scale=%WIDTH%:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" ^ | |
| -loop 0 ^ | |
| "%OUTPUT_FILE%" | |
| :: --- Checking Results --- | |
| if exist "%OUTPUT_FILE%" ( | |
| echo. | |
| echo β Done! The GIF file has been created. | |
| ) else ( | |
| echo. | |
| echo β ERROR: FFmpeg failed to create GIF file. | |
| echo. | |
| ) | |
| endlocal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment