ffmpeg overlay video on image and remove video black background - ffmpeg

Im using this ffmpeg command to overlay a video on image (with remove black background):
ffmpeg -loop 1 -i image.png -i video.mp4 -filter_complex [1:v]colorkey=0x000000:0.1:0.1[ckout];[0:v][ckout]overlay[out] -map [out] -t 5 -c:a copy -c:v libx264 -y result.mp4
But as you can see in the picture, the black parts of the ball have also disappeared. How can I solve this problem?

Not possible with colorkey/chromakey alone. The background is too similar to the color you want to remove. You have two options.
Mask
Use a mask. If the video comes with an alpha mask you can use it to cut out the background using the alphamerge filter:
ffmpeg -i bg.jpg -i video.mp4 -i alpha.mp4 -filter_complex "[1][2]alphamerge[alf];[0][alf]overlay" output.mp4
Use a different color
Replace the video that has a color that is different than the color you want to remove.

Related

FFMpeg add drop shadow to a video

I have this example video, recorded by Kazam:
https://user-images.githubusercontent.com/1997316/178513325-98513d4c-49d4-4a45-bcb2-196e8a76fa5f.mp4
It's a 1022x728 video.
I need to add a drop shadow identical to the one generated by the "Drop shadow (legacy)" filter of Gimp with the default settings. So, I generated with Gimp a PNG containing only the drop shadow. It's a 1052x758 image:
Now I want to put the video over the image to get a new video with the drop shadow. The wanted effect for the first frame is:
So, the video must be placed over the image. The top-left corner of the video must be in the position 11x11 of the background image.
How can I achieve this result?
I tried without success the following command. What's wrong?
ffmpeg -i shadow.png -i example.mp4 -filter_complex "[0:v][1:v] overlay=11:11'" -pix_fmt yuv420p output.mp4
About the transparency of the PNG background image, if it can't be maintained, then it's okay for the shadow to be on a white background. Otherwise, if it can be maintained by using an animated GIF as the output format, it is better.
The solution is to remove the transparency from shadow.png. Then:
ffmpeg -i example.mp4 -filter_complex "[0:v] palettegen" palette.png
ffmpeg -loop 1 -i shadow.png -i example.mp4 -i palette.png -filter_complex "[1:v] fps=1,scale=1022:-1[inner];[0:v][inner]overlay=11:11:shortest=1[new];[new][2:v] paletteuse[out]" -map '[out]' -y output.gif
The result is exactly what I wanted:
This solution is inspired by the answer https://stackoverflow.com/a/66318325 and by the article https://www.baeldung.com/linux/convert-videos-gifs-ffmpeg

Trying to crop an image in ffmpeg using alphamerge but produces wrong alpha

I am using alphamerge in order to crop an image within a circle.
What I have so far is:
ffmpeg -f lavfi -i "color=c=white:size=240x240" -i avatar.png -i mask.png -filter_complex \
"[1][2]alphamerge[img]; \
[0][img]overlay[out]" -c:v png -map "[out]" -pix_fmt rgba -t 5 -y out.mp4 2>&1
with avatar.png and mask.png respectively being:
This produces the following output (1 frame of the output video):
which is unexpected, given the original input is much darker than this.
How can I crop the 'avatar.png' using the 'mask.png' so that the output is the avatar.png cropped in a circle and keeping the same alpha?
PS: The important bit here is for me to be able to crop the original image and maintaining the correct colors/apha of the original image. If there is an other way of doing this (other than alphamerge) I am happy to hear it.
Figured it out.
ffmpeg -i avatar.png -i mask.png -filter_complex "[0]scale=400:400[ava];[1]alphaextract[alfa];[ava][alfa]alphamerge" output.png

FFMPEG watermark text / photo transparent opacity problem

I've came across two problems.
I'm trying to resize and set the opacity/transparency of a watermark, and I can't seem to do it in one command line.
command to rescale but not set transparency:
ffmpeg -i video.mp4 -i watermark.png -filter_complex "[1]scale=iw*0.1:-1[wm];[0][wm]overlay=x=15:y=10" output.mp4
Trying to set transparency of text but leave the border of the text greyish.
command to resize text and set border, but could not set transparency:
ffmpeg -i video.mp4 -filter_complex "drawtext=fontfile=wryh.ttf:text='sample':x=20:y=15:fontcolor=white#1:fontsize=15:fontcolor=white:bordercolor=black:borderw=0.51" output.mp4
Combined command:
ffmpeg -i video.mp4 -i watermark.png -filter_complex "[1]format=rgba,colorchannelmixer=aa=0.7,scale=iw*0.1:-1[logo];[0][logo]overlay=15:10:format=auto,drawtext=fontfile=wryh.ttf:text='sample':x=20:y=15:fontcolor=white#1:fontsize=15:fontcolor=white:bordercolor=black:borderw=0.51,format=yuv420p" -c:a copy video.mp4

Overlay/fade image over beginning of MKV with ffmpeg

I've got an MKV that I would like to replace the first 5 seconds with a static png image that fades in/out from black. How can I accomplish this with just ffmpeg?
Easy method is to overlay the image:
ffmpeg -i input.mkv -loop 1 -t 5 -i image.png -filter_complex "[1]fade=type=in:duration=1,fade=type=out:duration=1:start_time=4[fg];[0]drawbox=t=fill:enable='lte(t,5)'[bg];[bg][fg]overlay=eof_action=pass:x=(W-w)/2:y=(H-h)/2" -c:a copy output.mkv
I added the drawbox filter to make a black background because I didn't know the size of your image.
See FFmpeg Filter Documentation.

FFmpeg how to add drawtext on filter complex along with watermark and palettegen

thanks to many posts on stack overflow I could come till here: How to add watermark in a gif with ffmpeg? which i can convert mp4 into animated gif with moderate quality and watermark on. But I'd like to add the drawtext too in these lines.
ffmpeg -i in.mp4 -i watermark.png -filter_complex "[0]fps=10,scale=320:-1:flags=lanczos[bg];[bg][1]overlay=W-w-5:H-h-5,palettegen" palette.png
ffmpeg -i in.mp4 -i watermark.png -i palette.png -filter_complex "[0]fps=10,scale=320:-1:flags=lanczos[bg];[bg][1]overlay=W-w-5:H-h-5[x];[x][2]paletteuse=dither=bayer:bayer_scale=3" output.gif
Would it be possible through filter_complex by adding another [ ]? Thanks a lot to everyone
You can add drawtext after overlay.
ffmpeg -i input.mp4 -i watermark.png -filter_complex "[0]fps=10,scale=320:-1[bg];[bg][1]overlay=main_w-overlay_w-10:main_h-overlay_h-10:format=auto,drawtext=text='your text here':fontsize=24:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" output.gif
This will add text centered in video and overlay will be placed in the lower right.

Resources