4

I need to convert mp4 video(1280×720) to webp file(512x512) such that the resulting webp file maintains aspect ratio and also gets entirely contained in 512x512 and the uncovered areas at the top and bottom should be transparent.

I tried the following ffmpeg command:

ffmpeg -i sample.mp4 
-vcodec libwebp -filter:v fps=fps=20 -lossless 1 -loop 0 -preset default -an -vsync 0 -vf 
scale=512:512:force_original_aspect_ratio=decrease,pad=512:512:-1:-1:color=#00000000 sample.webp

In the above command please note pad=512:512:-1:-1:color=#00000000 I have given the alpha values 00 but it outputs black color only.

I also referred to this site https://ffmpeg.org/ffmpeg-utils.html#color-syntax It says

It can be the name of a color as defined below (case insensitive match) or a [0x|#]RRGGBB[AA] sequence, possibly followed by @ and a string representing the alpha component. The alpha component may be a string composed of "0x" followed by a hexadecimal number or a decimal number between 0.0 and 1.0, which represents the opacity value (‘0x00’ or ‘0.0’ means completely transparent, ‘0xff’ or ‘1.0’ completely opaque). If the alpha component is not specified then ‘0xff’ is assumed.

I tried both color=0x000000@0x00 and color=0x000000@0.0 but the result was opaque black.

isharth rastogi
  • 101
  • 2
  • 6

3 Answers3

3

For conversion of files with alpha channel also change codec to libwebp_anim to make previous frames be cleared when a new is drawn. This is what the final command will look like:

ffmpeg -i input.mov -c:v libwebp_anim -filter:v fps=fps=20 -lossless 1 \
 -loop 0 -preset default -an -vsync 0 -vf \
 "scale=512:512:force_original_aspect_ratio=decrease,format=rgba,pad=512:512:-1:-1:color=#00000000" \
 output.webp
  • Thank god for your answer, especially about frame clearing. I searched docs for hours and found absolutely no mention of `libwebp_anim` - why is it so obscure? It should be the default behaviour – 1owk3y Jun 03 '23 at 16:13
2

I was just missing format filter before pad format=rgba,pad

isharth rastogi
  • 101
  • 2
  • 6
  • `ffmpeg -h encoder=libwebp` shows `Supported pixel formats: bgra yuv420p yuva420p`, so you can choose `bgra` or `yuva420p` depending on which colorspace you prefer (the input MP4 is almost certainly YUV). `rgba` as in your answer works too, but it will get autoconverted to `bgra` (not that it really matters). – llogan Feb 10 '21 at 22:31
0
ffmpeg -r 30 -i %04d.png -vcodec libwebp -loop 0 -q:v 100 -lossless 1 test.webp
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
vaid
  • 1,390
  • 12
  • 33