-1

I have two transparent pictures, one is in completely transparent png format, its name is C0.png, the other is the main picture, and its name is C1.png. I want to superimpose two pictures with xfade and transparency effects.

I used the command below:

ffmpeg -loop 1 -t 12 -i c0.png -t 12 -loop 1 -i c1.png -filter_complex "[0]format=rgb24,drawbox=thickness=fill,split[black][black2];[black2]negate[white];[black][white]xfade=transition=wipedown:duration=1:offset=0[alf];[0][1]overlay=format=auto[ovr];[ovr][alf]alphamerge[fg];[0][fg]overlay=format=auto:alpha=1,format=yuv420p" -t 12 -pix_fmt yuv420p 1p.webm

But after executing this way, the final webm video will have a black background, and what I want is a video with a transparent background。

How to optimize it, look forward to your help, thank you

C0.png https://i.stack.imgur.com/fPuQH.png

C1.png https://i.stack.imgur.com/W9RA6.png

Xiang Chen
  • 31
  • 12
  • Video transparency is defined by alpha channel. What you see depends on how a player handles alpha channel. – Rajib Oct 17 '21 at 10:09
  • i used iina app and chrome to view the video, same black background will display – Xiang Chen Oct 17 '21 at 12:50
  • In Chrome when I drag any film onto the blank page, the background always turns black, so alpha/transparency will show as black. Having said that, you should change both instances of `yuv420p` to `yuva420p` to accommodate alpha channel. – Rajib Oct 17 '21 at 14:20
  • I overlay this webm video to a mp4 , i can see black background – Xiang Chen Oct 17 '21 at 14:31
  • It is not clear what you want to do by looking at C0 because there seems to be no image at all in it. So why use it at all? Also there are 2 instances of `-t 12` in the input side which are not necessary. Moreover, the result is the same even if we drop `format=rgb24`. Please edit the question to show what is the final result you want. By the pics attached, the overlay does not seem to do anything at all. – Rajib Oct 17 '21 at 14:31
  • Did you use `yuva420p`? And where are you overlaying on mp4? Which application? Again, please clarify what you actually want to achieve with the 2 images C0 and C1. C0 seems blank. – Rajib Oct 17 '21 at 14:33
  • C0 is a blank png image , I used as tranparent background. because xfade need two image file – Xiang Chen Oct 17 '21 at 14:42
  • I want the output webm file have no black background , when 1p.webm overlay to a mp4 file , there will no black background , transparent 1p.webm on a mp4 video. – Xiang Chen Oct 17 '21 at 14:47
  • I try to use yuva420p, but after 1p.webm overlay to a video, black background will appear, this is not I want. – Xiang Chen Oct 17 '21 at 14:47
  • It is not clear what you want to achieve. Is this simply a wipedown transition so that an image C1, which has some transparency, will appear in the video, along with transparency? (Wipedown transition from blank screen)? – Rajib Oct 17 '21 at 17:27
  • yes ,you totally got it – Xiang Chen Oct 17 '21 at 23:37

1 Answers1

2

Try this:

ffmpeg -loop 1 -i c1.png -filter_complex "nullsrc=size=800x800[bg]; \
[bg][0]xfade=transition=wipedown:duration=1:offset=1,format=yuva420p" \
-c:v libvpx-vp9 -pix_fmt yuva420p -metadata:s:v:0 alpha_mode="1" -t 12 1p.webm

You do not need the drawbox filter at all because xfade itself can do the transition.

Also, you do not need a blank image. Instead, use a null source for the other image, here achieved by the nullsrc filter. Note that the size of the nullsrc has to adjusted by you so that it matches your image c1.png. In my sample I have used a sample/arbitrary size of 800x800 pixels. I have used an offset of 1 second. So the wipedown starts 1 second after the video starts.

You can choose anything you prefer.

You do need the format to be yuva420p because the a represents the alpha channel. Also for webm it seems you need to state -metadata:s:v:0 alpha_mode="1" for the result to have alpha.

Note that in Chrome as soon as I drag a video, the entire canvas turns black, so even a transparent background will show as black. If you query through ffprobe or ffmpeg your video will report "Alphamode : 1".

The drawbox filter, which draws a solid black color with fill will certainly make it non-transparent because it will cover the alpha channel. Anyway it is not required at all for what you want to do.

The above code is tested on bash 4.4 and ffmpeg version 4.4, and works perfectly.

Rajib
  • 453
  • 7
  • 10
  • Now I upgrade ffmpeg to 5.0.1, but this bash command not working any more? can u help me more? thank u so much . @Rajib – Xiang Chen Jun 22 '22 at 15:04