Alex Balgavy

Just some stuff about me.

Here's my dotfiles repository.

Check out my blog.

My keys: PGP, SSH

My crypto wallets (BTC, XMR)

ffmpeg how to use green screen to mask a video into another video

Written by John Riselvato - original link

The use of a green screen is the most common way to add a subject to a scene in video production. By setting the scene at the purest green (not commonly found in nature) objects can interact on the green screen while computer generation can replace a new image in the background.

With FFmpeg, this can easily be accomplished using the colorkey filter. The colorkey filter takes an input, looks for a specific color depending on similarity, and stores the information in a new stream. 

{width=“252” height=“139”}

In this example, the use of greenscreen.mp4 is used as a mask to be applied to input.mp4, as seen in figure 1. 

If you have an image with green screen content like above but want to turn the image into a video try this code out:

$ ffmpeg -loop 1 -i input.png -I -c:v libx264 -pix_fmt yuv420p output.mp4

{width=“252” height=“139”}

The result includes the black background but the train video in replace off the green words, as seen in figure 2:

 $ ffmpeg -i input.mp4 -i greenscreen.mp4 -filter_complex '[1:v]colorkey=color=00FF00:similarity=0.85:blend=0.0[ckout];[0:v][ckout]overlay[out]' -map '[out]' output.mp4

Tip: #00FF00 is the pure green found in green screens.