ffmpeg: pad filter with transparent background for GIF input/output? - ffmpeg

I'm trying to pad the GIF source to a square thumbnail and the extended area should be transparent.
(image source: https://store.steampowered.com/app/2113680/The_Dot/)
To achieve the goal, I used the following command:
ffmpeg -i ./test/source.gif -filter_complex "[0]scale=255:144[s1];[s1]pad=256:256:0:-56:blue[s2];[s2]split[s3][s4];[s3]palettegen=reserve_transparent=on:transparency_color=blue[p];[s4][p]paletteuse[s5]" -map [s5] -loop 0 ./test/output.gif -y
[0]scale=255:144[s1] First I scale down the image.
[s1]pad=256:256:0:-56:blue[s2] Then pad the image with an uncommon background color: blue
[s2]split[s3][s4] And I split to two streams. One for palettegen and another for paletteuse.
[s3]palettegen=reserve_transparent=on:transparency_color=blue[p] Generate the color palette that indicates blue as the transparency_color
[s4][p]paletteuse[s5] Use the new color palette for output. The padded image with blue background color should be treated as a transparent background.
But the output animated GIF still has a visible blue background color no matter if I put the paletteuse at first or in the end.
Related posts:
Making GIFs with ffmpeg: what does "transparency_color" do?
How do I resize an animated GIF and keep transparency?
Transparency is gone while resizing (scaling) gif using ffmpeg

overlay on transparent background:
ffmpeg -i 74bRi.gif -filter_complex "
color=s=256x256:d=100,format=argb,colorchannelmixer=aa=0.0[b];
[0]scale=255:144[f];
[b][f]overlay=(W-w)/2:(H-h)/2:shortest=1,split[s1][s2];
[s1]palettegen[p];
[s2][p]paletteuse
" -loop 0 output.gif
pad using transparent color ffffff00
ffmpeg -i 74bRi.gif -filter_complex "
[0]scale=255:144,pad=256:256:0:-1:ffffff00,split[s1][s2];
[s1]palettegen[p];
[s2][p]paletteuse
" -loop 0 out.gif

Related

FFMPEG Blending for VP9 format videos

I get a darker background, almost like a placeholder for the asset that is to be blended, whereas the background should be transparent, and should show the same colour as the rest of the background.
I have two webm vp9 files that I am trying to blend using FFMPEG blending functionality.
One of the videos is a zoom animation that starts from 1 pixel in size and then increases in size to 50 x 50 pixels.
The other is a solid red background video 640 px x 360 px
At frame 1 the result looks like this:-
At about 0.5 seconds through, the result looks like this:-
At the end of the sequence, the zoom animation webm fills that darker square you see (50 x 50 pixels).
The code to do the blending of the two webm files looks like this:-
filter_complex.extend([
"[0:v][1:v]overlay=0:0:enable='between(t,0,2)'[out1];",
'[out1]split[out1_1][out1_2];',
'[out1_1]crop=50:50:231:251:exact=1,setsar=1[cropped1];',
'[cropped1][2:v]blend=overlay[blended1];',
"[out1_2][blended1]overlay=231:251:enable='between(t,0,2)'[out2]"
])
This overlays a red background onto a white background, making the red background the new background colour.
It then splits the red background into two, so that there is one output for cropping and another output for overlaying.
It then crops the location and size of the layer to be blended out of the red background. We do this because blending works only on an asset of the same size.
It then performs the blend of the zoom animation onto the cropped background.
It then overlays the blended over the red background
Unfortunately I'm unable to attach videos in stackoverflow, otherwise I would have included them.
The full command looks like this:-
ffmpeg -i v1_background.webm -itsoffset 0 -c:v libvpx-vp9 -i v2_red.webm -itsoffset 0 -c:v libvpx-vp9 -i v3_zoom.webm -filter_complex "[0:v][1:v]overlay=0:0[out1];[out1]split[out1_1][out1_2];[out1_1]crop=50:50:231:251:exact=1,setsar=1[cropped1];[cropped1][2:v]blend=overlay[blended1];[out1_2][blended1]overlay=231:251" output_video_with_blended_overlaid_asset.mp4
I have checked the input vp9 webm zoom video file by extracting the first frame of the video
ffmpeg -vcodec libvpx-vp9 -i zoom.webm first_frame.png
and inspecting the colours in all the channels in GIMP. The colours (apart from the opaque pixel in the middle) are all zero, including the alpha channel.
Note that I tried adding in all_mode, so that the blend command is blend=all_mode=overlay, however this still shows the darker placeholder under the animation asset. In other words, this command
ffmpeg -i v1_background.webm -itsoffset 0 -c:v libvpx-vp9 -i v2_red.webm -itsoffset 0 -c:v libvpx-vp9 -i v3_zoom.webm -filter_complex "[0:v][1:v]overlay=0:0[out1];[out1]split[out1_1][out1_2];[out1_1]crop=50:50:231:251:exact=1,setsar=1[cropped1];[cropped1][2:v]blend=all_mode=overlay[blended1];[out1_2][blended1]overlay=231:251" output_video_with_blended_all_mode_overlay_asset.mp4
also doesn't work
and trying to convert the formats to rgba first doesn't help either, command below is simplified a bit
ffmpeg -c:v libvpx-vp9 -i v2_red.webm -c:v libvpx-vp9 -i v3_zoom.webm -filter_complex "[0:v]format=pix_fmts=rgba[out1];[1:v]format=pix_fmts=rgba[out2];[out1]split[out1_1][out1_2];[out1_1]crop=50:50:0:0:exact=1,setsar=1[cropped1];[cropped1][out2]blend=all_mode=dodge[blended1];[out1_2][blended1]overlay=50:50" output_video_with_blended_all_mode_dodge_rgba_and_alpha_premultiplied_overlay.mp4
adding in an alpha premultiply didn't help either
ffmpeg -c:v libvpx-vp9 -i v2_red.webm -c:v libvpx-vp9 -i v3_zoom.webm -filter_complex "[0:v]format=pix_fmts=rgba[out1];[1:v]setsar=1,format=pix_fmts=rgba,geq=r='r(X,Y)*alpha(X,Y)/255':g='g(X,Y)*alpha(X,Y)/255':b='b(X,Y)*alpha(X,Y)/255'[out2];[out1]split[out1_1][out1_2];[out1_1]crop=50:50:0:0:exact=1,setsar=1[cropped1];[cropped1][out2]blend=all_mode=dodge[blended1];[out1_2][blended1]overlay=50:50" output_video_with_blended_all_mode_dodge_rgba_and_alpha_premultiplied_overlay.mp4
Wondering if there is a workaround I could use so that the background stays transparent?
I was looking for maybe a way of changing the input pixel format in the filter_complex stream to see if that works, but couldn't see anything about this.

ffmpeg hstack png and replace transparent color with white color

Below command works fine to stack two png file with alpha channel left and right, The output png file will keep the alpha channel as well.
ffmpeg -i a.png -i b.png -filter_complex hstack=inputs=2 output.png
I wish replace alpha channel with white color (no need alpha channel), which parameter should I use?
You can try this filter:
color=c=white[b];\
hstack=inputs=2[f];\
[b][f]scale2ref[b1][f1];\
[b1][f1]overlay
also set -pix_fmt rgb24 output option

ffmpeg gif with color instead transparency [duplicate]

I'm converting a PNG to JPG. The transparent background turns black by default. I need it to be white.
What is the FFmpeg command to set the alpha channel to a color?
I think it has something to do with the alphamerge and alphaextract Filters.
ffmpeg -i image.png -qscale:v 2 image.jpg
This replaces white with transparency when converting to png:
-vf chromakey=white
You can use the geq filter.
ffmpeg -i in.png -vf format=yuva444p,geq='if(lte(alpha(X,Y),16),255,p(X,Y))':'if(lte(alpha(X,Y),16),128,p(X,Y))':'if(lte(alpha(X,Y),16),128,p(X,Y))' out.jpg
If your alpha is a pure black and white image, change 16 to 1.

ffmpeg - resize video and merge with image

I've 3 inputs (1st, 2nd and 3rd block)
1st a mp4 video with 600x400 aspect ratio
2nd a png image with 600x400 aspect ratio
3rd a jpeg image with red background
Output (4th block)
I need a mp4 video of 600x400 as output, it should have resized video of 422x282 and merge all three as shown in image.
Can we implement this via ffmpeg command line?
I'm able to resize video and image separately but having issue in creating desire output.
Use
ffmpeg -i 1.mp4 -i red.jpg -i frame.png
-filter_complex "[0]scale=422:-1[vid];[1][vid]overlay=(W-w)/2:(H-h)/2[bg];
[bg][2]overlay=(W-w)/2:(H-h)/2" out.mp4
First, the video is resized. Then that resized video is overlaid on the red background. Then, on top of that result, the PNG frame is overlaid.
With no red frame and white BG,
ffmpeg -i 1.mp4 -i frame.png
-filter_complex "[0]scale=422:-1,pad=600:400:(ow-iw)/2:(oh-ih)/2:color=white[vid];[vid][1]overlay=(W-w)/2:(H-h)/2" out.mp4

How to Convert PNG Transparent Background to JPG White Background?

I'm converting a PNG to JPG. The transparent background turns black by default. I need it to be white.
What is the FFmpeg command to set the alpha channel to a color?
I think it has something to do with the alphamerge and alphaextract Filters.
ffmpeg -i image.png -qscale:v 2 image.jpg
This replaces white with transparency when converting to png:
-vf chromakey=white
You can use the geq filter.
ffmpeg -i in.png -vf format=yuva444p,geq='if(lte(alpha(X,Y),16),255,p(X,Y))':'if(lte(alpha(X,Y),16),128,p(X,Y))':'if(lte(alpha(X,Y),16),128,p(X,Y))' out.jpg
If your alpha is a pure black and white image, change 16 to 1.

Resources