(FFmpeg) How to Use Green Screen to Mask a Video Into Another Video?

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. 

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

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. 

Love FFmpeg? Grab a copy of FFmpeg For Beginners on Kindle or Paperback to learn over 120 ways to master FFmpeg!

buy now on amazon