Post

Tip #15: Convert Video to GIF

The Snipping Tool in Windows now includes a video recording feature, which I have been eagerly anticipating. The videos are saved in MP4 format by default. For my blog, I convert these screen capture videos into Animated GIFs using this great method I discovered.

Manual

The all powerful ffmpeg does a great job and there are win64 binaries too.

I begun with these two lines:

1
2
3
4
REM Generate the palette from the input file
ffmpeg -i "your.mp4" -filter_complex "[0:v] palettegen" temp_palette.png
REM Create the GIF using the palette
ffmpeg -i "your.mp4" -i temp_palette.png -filter_complex "[0:v][1:v] paletteuse" "your.gif"

Converting videos to animated gif’s works best with small videos with little color changes

Batch file

While that approach works for single use, I wanted a more automated solution. I recalled that dropping a file onto a batch file would pass the file path as a parameter. My idea was to create a batch file that could accept dropped mp4 files and generate an animated gif using the above commands in the same directory.

This is what I came up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@echo off

REM Set input file name and output file name
set "input_file=%~1"
set "output_file=%~n1.gif"

echo.
echo "InputFile: %input_file%"
echo "OutputFile: %output_file%"

REM Generate the palette from the input file
%~dp0ffmpeg -i "%input_file%" -filter_complex "[0:v] palettegen" palette.png

REM Create the GIF using the palette
%~dp0ffmpeg -i "%input_file%" -i palette.png -filter_complex "[0:v][1:v] paletteuse" "%output_file%"

REM Cleanup intermediate files
del palette.png
echo Completed.

ffmpeg.exe must be in the same directory as the batch file

Drag and drop mp4 to batchdrag and drop mp4

Automated

I wanted to go beyond just dragging and dropping a file onto a batch file. I aimed to add this functionality to the explorer’s context menu, allowing me to run it on any mp4 file, regardless of its location.

Explorer context menuConvert Video to GIF

The steps required for this are:

  • download ffmpeg.exe
    • curl -o "%temp%\ffmpeg.zip" -L "https://github.com/GyanD/codexffmpeg/releases/download/6.0/ffmpeg-6.0-essentials_build.zip"
  • copy the batch file, ffmpeg and an icon to a common location, I chose %ProgramData%
  • Register context menu in the shell (registry)
    • reg add "HKEY_CLASSES_ROOT\*\shell\Convert Video to GIF" /ve /d "Convert Video to GIF" /f
    • reg add "HKEY_CLASSES_ROOT\*\shell\Convert Video to GIF" /v "Icon" /d "\"C:\\ProgramData\\VideoToGif\\VideoToGIF.ico\"" /f
    • reg add "HKEY_CLASSES_ROOT\*\shell\Convert Video to GIF\command" /ve /d "\"C:\\ProgramData\\VideoToGIF\\VideoToGIF.bat\" \"%%1\"" /f

I created an install.bat file that must be run as an administrator to handle all of these steps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@echo off
NET FILE >NUL 2>&1
IF %ERRORLEVEL% NEQ 0 (
    ECHO This script must be run as Administrator!
    PAUSE
    EXIT /B
)

setlocal

REM Create VideoToGIF folder in ProgramData
echo Creating VideoToGIF folder...
if not exist "%ProgramData%\VideoToGIF" mkdir "%ProgramData%\VideoToGIF"

REM Download ffmpeg to temporary directory and copy to VideoToGIF folder
echo Downloading ffmpeg and copying to VideoToGIF folder... 
curl -o "%temp%\ffmpeg.zip" -L "https://github.com/GyanD/codexffmpeg/releases/download/6.0/ffmpeg-6.0-essentials_build.zip"
powershell -Command "$tempDir = [System.IO.Path]::GetTempPath(); Expand-Archive "$tempDir\ffmpeg.zip" -DestinationPath "%temp%\ffmpeg" -Force"
del %temp%\ffmpeg.zip
copy "%temp%\ffmpeg\ffmpeg-6.0-essentials_build\bin\ffmpeg.exe" "%ProgramData%\VideoToGIF"
rmdir /s /q %temp%\ffmpeg
copy "%~dp0\VideoToGIF.bat" "%ProgramData%\VideoToGIF"
copy "%~dp0\VideoToGIF.ico" "%ProgramData%\VideoToGIF"

REM Adding context menu Convert to GIF
echo Importing registry keys...
reg add "HKEY_CLASSES_ROOT\*\shell\Convert Video to GIF" /ve /d "Convert Video to GIF" /f
reg add "HKEY_CLASSES_ROOT\*\shell\Convert Video to GIF" /v "Icon" /d "\"C:\\ProgramData\\VideoToGif\\VideoToGIF.ico\"" /f
reg add "HKEY_CLASSES_ROOT\*\shell\Convert Video to GIF\command" /ve /d "\"C:\\ProgramData\\VideoToGIF\\VideoToGIF.bat\" \"%%1\"" /f

echo Installation complete.
pause

Find all the required files in this VideoToGIF.zip

  • install.bat
  • VideoToGIF.bat
  • VideoToGIF.ico
This post is licensed under CC BY 4.0 by the author.