How to get animated webp file with transparent background padding after converting from mp4 file with ffmpeg?
Asked Answered
B

4

7

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 [email protected] but the result was opaque black.

Binary answered 10/2, 2021 at 20:42 Comment(0)
B
3

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

Binary answered 10/2, 2021 at 21:12 Comment(1)
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).Burlingame
P
6

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
Phosphorescence answered 24/9, 2021 at 5:45 Comment(1)
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 behaviourNylanylghau
B
3

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

Binary answered 10/2, 2021 at 21:12 Comment(1)
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).Burlingame
F
0
ffmpeg -r 30 -i %04d.png -vcodec libwebp -loop 0 -q:v 100 -lossless 1 test.webp
Floro answered 15/9, 2022 at 15:34 Comment(0)
S
0

Timofey’s answer was helpful, but did not work for me. The libewebp_anim is essential, but I didn't get it to work until I added this colorkey:

ffmpeg -i input.mp4 -vf "colorkey=0x000000:0.06:0.06" -c:v libwebp_anim -lossless 0 -compression_level 6 -q:v 50 -loop 0 -an -preset picture -metadata:s:v:0 alpha_mode="1" output.webp
Subirrigate answered 2/3 at 20:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.