"Creating gifs on the fly in mpv"

Date published: 2024-02-11 19:27 UTC+0

You will need:

Download modified encode.lua and input.conf. Set up your file structure[^3] 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. Enjoy your gifs!

yuno hidamari sketch

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 prevents looping, and any other number will set the gif to loop for that fixed number of times.
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 that I took the ffmpeg filter from.

[^1]: ffmpeg installation guide, I'd personally recommend scoop over Chocolatey as a package manager
[^2]: it is possible without but you would need to edit the absolute path to your ffmpeg executable into ffmpeg_command in your encode config
[^3]: mpv documentation on scripts folder location. On windows, mpv scripts are located in C:\Users\<User>\AppData\Roaming\mpv\scripts or C:\Users\<User>\AppData\Roaming\mpv.net\scripts