Tuesday, April 25, 2017

FFMpeg Green Screen - A Chroma Key For Every Lock

Green screen effects are older than I am, by a significant margin.

The technique; essentially recording a subject in front of a uniformly colored screen, often green, then taking that recording figuratively making all the green background transparent and finally overlaying on another video.  The result, the subject filmed in from of the green screen appearing in the context of the second video.  Easiest and safest way to film your kids in a shark tank.

Before we get started, the majority of my FFMpeg posts use filters and capabilities that have existed in FFMpeg for quite some time (e.g. v2.7.2), but the chroma key filter is fairly new, I used v3.2.4, but I suspect the capability was introduced some time earlier.

There are a good deal of green screen videos available on YouTube for playing around.  Let's demonstrate how to generate a crude green screen.

Let's start with the green screen subject.

Then, let's look at a video we wish to introduce our subject into.


Sprinkle in a little FFMpeg magic;
Prior to overlaying the two videos, they need to be similar in size and framerate.
$ ffmpeg -n -i foreground.mp4 -ss 20 -t 120 -strict -2 -s hd720 -r 30 foreground-720p.mp4
$ ffmpeg -n -i background.mp4 -ss 20 -t 120 -strict -2 -s hd720 -r 30 background-720p.mp4

Then, specify the two videos, the color of the green screen and some blending factors.

$ ffmpeg -y -i background-720p.mp4 -i foreground-720p.mp4 -filter_complex '[1:v]colorkey=0x008000\:0.3\:0.2[ckout];[0:v][ckout]overlay[out]' -map '[out]' -t 10 greenScreen.mp4

The result:

Now, take your new skills and your own creativity and show the world your new-found powers!

4 comments:

  1. Nice Article.
    When I try to execute the same, the audio in the forground is lost in the greenScreen.mp4. Is there an option to override this behavior?

    ReplyDelete
  2. Try specifying '-c:a:0' to pull the audio stream from the first video.
    https://trac.ffmpeg.org/wiki/Map

    ReplyDelete
  3. Is there any way to remove/blur the green edges in the resultant video ?

    ReplyDelete
  4. Unfortunately I haven't found a way that doesn't degregate the video. Applying a blur or reduce resolution may hide it, but applied to entire frame.

    ReplyDelete