Last active
August 16, 2018 13:13
-
-
Save marktaiwan/0a160c17e851116d24ee9fc9639481a4 to your computer and use it in GitHub Desktop.
Windows batch script to simplify creation of GIFs 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
@echo off | |
setlocal enabledelayedexpansion | |
:: Windows batch script to simplify creation of GIFs with ffmpeg. | |
:: | |
:: To use, drag and drop one or more files onto the script and type out the desired options when prompted. | |
:: | |
:: The script assumes ffmpeg binary is already in the PATH variable. | |
:: It's important to note that this script will use the input filename and save the converted | |
:: GIF in the same directory as the input, if the file already exist, it will be overwritten. | |
:: | |
:: Letter in [b]rackets signifies the shortcut key for that option, | |
:: leave blank to choose the default or first option. | |
:: | |
:: The code is released under The Unlicense <https://unlicense.org/UNLICENSE> | |
:: Lines prefixed with :: are comments | |
:: If you don't have the directory of ffmpeg binary set in your PATH variable, uncomment and | |
:: add the directory to the line below. | |
::set EXE_PATH=C:\path\to\ffmpeg\here | |
IF DEFINED EXE_PATH ( | |
IF EXIST "%EXE_PATH%" ( | |
set "PATH=%PATH%;%EXE_PATH%" | |
) ELSE ( | |
echo Path to ffmpeg not found | |
pause | |
exit | |
) | |
) | |
:: full: generates the palette from the entire clip | |
:: diff: prioritizes pixels with changing values | |
:: skip: skips the palettegen and dithering options entirely, uses default the palette | |
set /P PALETTEGEN_MODE="palettegen mode (full/diff/skip): " | |
if "%PALETTEGEN_MODE%" == "" (set PALETTEGEN_MODE=full) | |
if "%PALETTEGEN_MODE%" == "skip" (goto skippalette) | |
set /P MAX_COLORS="max colors (default 256): " | |
if "%MAX_COLORS%" == "" (set MAX_COLORS=256) | |
:: 'bayer' offers better compression for animated GIFs then 'floyd_steinberg' | |
set /P DITHER_MODE="dither mode ([b]ayer/[f]loyd_steinberg/[n]one): " | |
if "%DITHER_MODE%" == "" (set DITHER_MODE=bayer) | |
if "%DITHER_MODE%" == "b" (set DITHER_MODE=bayer) | |
if "%DITHER_MODE%" == "f" (set DITHER_MODE=floyd_steinberg) | |
if "%DITHER_MODE%" == "n" (set DITHER_MODE=none) | |
set EXTRA_COMMANDS= | |
if "!DITHER_MODE!" == "floyd_steinberg" ( | |
set /P USER_INPUT="diff_mode (none/[r]ectangle): " | |
if "!USER_INPUT!" == "r" ( | |
set EXTRA_COMMANDS=:diff_mode=rectangle | |
) | |
) | |
:: 0 = most dithering, 5 = least dithering | |
if "!DITHER_MODE!" == "bayer" ( | |
set /P USER_INPUT="bayer_scale (0~5) (default 3): " | |
if "!USER_INPUT!" == "" ( | |
set USER_INPUT=3 | |
) | |
set EXTRA_COMMANDS=:bayer_scale=!USER_INPUT! | |
) | |
:skippalette | |
:: scales the input according to width or height | |
set /P RESIZE_MODE="resize (none/[w]idth/[h]eight): " | |
if "%RESIZE_MODE%" == "w" (set RESIZE_MODE=width) | |
if "%RESIZE_MODE%" == "h" (set RESIZE_MODE=height) | |
set RESIZE_ARG_1=scale=0:0, | |
set RESIZE_ARG_2=scale=0:0 | |
if "!RESIZE_MODE!" == "width" ( | |
set /P RESIZE_VALUE="width: " | |
set RESIZE_ARG_1=scale=!RESIZE_VALUE!:-1:flags=lanczos, | |
set RESIZE_ARG_2=scale=!RESIZE_VALUE!:-1:flags=lanczos | |
) | |
if "!RESIZE_MODE!" == "height" ( | |
set /P RESIZE_VALUE="height: " | |
set RESIZE_ARG_1=scale=-1:!RESIZE_VALUE!:flags=lanczos, | |
set RESIZE_ARG_2=scale=-1:!RESIZE_VALUE!:flags=lanczos | |
) | |
:loop | |
if "%~1" == "" goto done | |
set OUTPUT_NAME=%~dpn1.gif | |
if "%OUTPUT_NAME%" == "%~1" (set OUTPUT_NAME=%~dpn1-ff.gif) | |
if "%PALETTEGEN_MODE%" NEQ "skip" ( | |
ffmpeg -i "%~1" -vf "[0:v] %RESIZE_ARG_1% split [a][b];[a] palettegen=stats_mode=%PALETTEGEN_MODE%:max_colors=%MAX_COLORS% [p];[b][p] paletteuse=dither=%DITHER_MODE%%EXTRA_COMMANDS%" -y "%OUTPUT_NAME%" | |
) else ( | |
ffmpeg -i "%~1" -vf "%RESIZE_ARG_2%" -y "%OUTPUT_NAME%" | |
) | |
shift | |
goto loop | |
:done | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment