Skip to content

Instantly share code, notes, and snippets.

@witmin
Last active May 31, 2026 09:26
Show Gist options
  • Select an option

  • Save witmin/1edf926c2886d5c8d9b264d70baf7379 to your computer and use it in GitHub Desktop.

Select an option

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.

@vitaly-zdanevich

Copy link
Copy Markdown

Looks like a typo: fps=fps=20

@witmin

witmin commented Mar 10, 2021

Copy link
Copy Markdown
Author

Looks like a typo: fps=fps=20

It is actually the syntax. Here is the documentation: http://ffmpeg.org/ffmpeg-all.html#Examples-108

@saadhaider78

Copy link
Copy Markdown

Hi @witmin , I want to convert a Animated WebP to mp4, how can I achieve that with FFMPEG?

ghost commented Nov 5, 2021

Copy link
Copy Markdown

Thaanks bro

@dylannirvana

Copy link
Copy Markdown

Converting from MOV to WEBP, is there a special command to preserve transparency?

For example,
ffmpeg -i input.mov -vf "format=yuva444p, scale= 1200:400, crop=1000:400:100:0" -vcodec libwebp -lossless 1 -loop 0 -preset default -an -vsync 0 -strict -1 output.webp

@witmin

witmin commented Nov 7, 2021

Copy link
Copy Markdown
Author

Converting from MOV to WEBP, is there a special command to preserve transparency?

For example, ffmpeg -i input.mov -vf "format=yuva444p, scale= 1200:400, crop=1000:400:100:0" -vcodec libwebp -lossless 1 -loop 0 -preset default -an -vsync 0 -strict -1 output.webp

@dylannirvana does this solution work https://stackoverflow.com/questions/66144707/how-to-get-animated-webp-file-with-transparent-background-padding-after-converti ?

ghost commented Nov 17, 2021 via email

Copy link
Copy Markdown

@ht55ght55

Copy link
Copy Markdown

A little late to the party, but I will just mention that unlike webp and gif, mp4 does not support transparency. Here's an article where Giphy lays out their strategy: https://developers.giphy.com/docs/optional-settings#renditions-on-demand

@Toyirov

Toyirov commented Jun 8, 2022

Copy link
Copy Markdown

Hmm, good

@o-t-w

o-t-w commented Sep 9, 2022

Copy link
Copy Markdown

Unknown encoder 'libwebp'

@aidv

aidv commented Sep 15, 2022

Copy link
Copy Markdown

To convert a series of transparent .png's to a transparent .webp
ffmpeg -r 30 -i %04d.png -vcodec libwebp -loop 0 -q:v 100 -lossless 1 test.webp

@dmitryfry

Copy link
Copy Markdown

What about converting webp to gif?

@SalimF

SalimF commented Feb 23, 2023

Copy link
Copy Markdown

^^ 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?

@witmin

witmin commented Feb 24, 2023

Copy link
Copy Markdown
Author

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?

Unfortunately I didn't have such needed before and didn't get a chance for keyframes. Would this post about keyframe timestamps helps: https://superuser.com/questions/554620/how-to-get-time-stamp-of-closest-keyframe-before-a-given-timestamp-with-ffmpeg?

@lamnhan066

Copy link
Copy Markdown

Just leave it here for anyone who wants to set auto-size for width or height:

ffmpeg -i input.mp4 -loop 1 -an -vf scale=400:-2,fps=fps=20 output.webp

Just replace the width or height in scale=width:height with -2 and it will automatically calculate the size for you. Source here.

More information: You shouldn't use the -preset parameter because it will override all other parameters like -lossless, -compression_level and -s so the output may not be as expected. Source here.

@DanielZanchi

Copy link
Copy Markdown

Converting a GIF with transparency gives a strange result. We still see the old frames in the background :(

@aulerius

aulerius commented Mar 4, 2024

Copy link
Copy Markdown

Even with best settings I get horrible color banding and blockyness. Are there ways to improve this?
image
Comparing to online tools like this one, it seems the codec can indeed achieve way smoother results. It's something to do with ffmpeg then.

@aidv

aidv commented Apr 3, 2024

Copy link
Copy Markdown

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

@thipperz

Copy link
Copy Markdown

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

So.. where?

@thipperz

Copy link
Copy Markdown

^^ 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

aidv commented May 14, 2024

Copy link
Copy Markdown

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
Copy Markdown

Now avif is better?

@abiriadev

abiriadev commented May 31, 2024

Copy link
Copy Markdown

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.

@besworks

besworks commented Mar 16, 2025

Copy link
Copy Markdown

Just leave it here for anyone who wants to set auto-size for width or height:

I used the tip @lamnhan066 posted to make a script that automatically crops black bars and optionally applies scaling to the result.

$ [SCALE=1] [FPS=20] vid2webp <input_file> [start_time] [end_time] [out_name]

No flags, just positional parameters for brevity. To set [end_time], [start_time] must be provided first. Set scale and framerate by ENV vars. Don't add .webp to [out_name], it will be added automatically. If not provided, out_name defaults to the original filename with .webp extension instead. If that file exists, it will add -1, -2, etc. to filename incrementally.

So, basic usage looks like this:

$ vid2webp vidfile.mp4 5 15
$ vid2webp vidfile.mp4 30 45
$ vid2webp vidfile.mp4 55 65 bonus
$ vid2webp vidfile.mp4 "00:03:22" "00:03:30.5"

Which outputs:

vidfile.webp
vidfile-1.webp
bonus.webp
vidfile-2.webp

To scale and convert entire video :

$ SCALE=0.5 vid2webp vidfile.mp4

More details and usage examples in the readme.

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