You will need:
- mpv or mpv.net
- ffmpeg[^1] executable on the command line[^2]
- mpv script encode.lua (modified)
Download modified encode.lua and input.conf.
Set up your file structure1 as follows:
input.conf
- script-opts
- scripts
encode.lua
Create a new file in script-opts titled encode_gif.conf:
gif=yes
only_active_tracks=yes
preserve_filters=yes
append_filter=scale=420:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse
codec=-loop 0
output_format=$f_$n.gif
output_directory=
detached=yes
ffmpeg_command=ffmpeg
In input.conf replace
e script-message-to encode set-timestamp
with
e script-message-to encode set-timestamp encode_gif
Now you can press e within mpv to indicate the starting frame of a gif, and Enter to set the final frame. Your gif will be output to the same directory as the currently playing file.
Options:
In input.conf, the letter e can be changed to your keybind of choice. eg. E (shift e), alt-e, ctrl-e.
In encode_gif.conf, -loop 0 specifies that the gif should loop indefinitely, -1 -> no loops, n>1 -> n loops.
scale=420:-1 scales the output to 420 pixels wide and the height is scaled automatically to match the original aspect ratio. To remove scaling entirely, remove scale=420:-1:flags=lanczos, from that line in the config file.
For more info, see the stack exchange answer which I took the ffmpeg filter from.
Enjoy your gifs!

-
mpv documentation on scripts folder location. On windows, mpv scripts are located in
C:\Users\<User>\AppData\Roaming\mpv\scriptsorC:\Users\<User>\AppData\Roaming\mpv.net\scripts
