Skip to content

Instantly share code, notes, and snippets.

@witmin
Last active June 2, 2024 11:43
Show Gist options
  • Save witmin/1edf926c2886d5c8d9b264d70baf7379 to your computer and use it in GitHub Desktop.
Save witmin/1edf926c2886d5c8d9b264d70baf7379 to your computer and use it in GitHub Desktop.
Convert MP4 file to animated WebP in ffmpeg

Convert MP4 file to animated WEBP file in ffmpeg CLI

1. Install ffmpeg CLI through homebrew

In terminal.app, install ffmpeg through homebrew

brew install ffmpeg

Validate the installation:

which ffmpeg

Expect to see terminal returns the directory path of ffmpeg such as /usr/local/bin/ffmpeg

2. Set webp properties and convert

Example command which would convert an mp4 file to a lossless loop playing webp file in 20FPS with resolution 800px(width) * h600px(height):

ffmpeg -i input_filename.mp4 -vcodec libwebp -filter:v fps=fps=20 -lossless 1 -loop 0 -preset default -an -vsync 0 -s 800:600 output_filename.webp

Export an animated lossy WebP with preset mode picture:

ffmpeg -i input.mp4 -vcodec libwebp -filter:v fps=fps=20 -lossless 0  -compression_level 3 -q:v 70 -loop 1 -preset picture -an -vsync 0 -s 800:600 output.webp

primary options:

  • set frame per second as 20: -filter:v fps=fps=20
  • set output file lossless: -lossless 1
  • set output webp file loop play: -loop 0. For non-loop, use -loop 1
  • set rendering mode -preset default , can set as picture, photo, text, icon, drawing and none as needed. It would effect output file size. http://ffmpeg.org/ffmpeg-all.html#Options-28
  • set output webp resolution as w800px * h600px -s 800:600

For more option details, please visit the the ffmpeg libwebp documentation

This method should applicable for majority video formats including .mov, .avi, .flv, etc. as input files as well as .gif format as output file.

@thipperz
Copy link

^^ Damn that image toke whole my screen view, remove it and put link instead.

Anyway to OP, how can I set sort of key-frame or control frames consistence, e.g. increased fps when the scene have movement and make still image when the scene stops or let it on lower fps?

How did you handle this?

@aidv
Copy link

aidv commented May 14, 2024

I wrote a tool to stitch PNG’s into a n animated WebP video with transparency and no gradient banding.

So.. where?

I'm thinking of making it into an app. Would this be interesting?

@vitaly-zdanevich
Copy link

Now avif is better?

@abiriadev
Copy link

abiriadev commented May 31, 2024

Note of 2024:

-vsync is deprecated. Use -fps_mode
Passing a number to -vsync is deprecated, use a string argument as described in the manual.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment