Tutorial for Green Screen with Avisynth?

1. Maybe my Googling skills are poor, but I'm having a hard time finding any tutorial on how to create green screen movies with avisynth. I searched this forum, but this is the closest thing I could find that was of help:

I tried to use that guy's code to create my own green screen with avisynth, but it didn't work.

This guy claims that he used Avisynth to create the very professional looking green screen video here: http://www.youtube.com/watch?v=RXByaCFgIHc

I have a green screen video of a girl dancing. I downloaded it from the internet. So I don't need instructions on how to film a green screen video, since I already have the green screen footage. What I need help with is figuring out how to get avisynth to take a background clip and overlay the green screen video on top of it.

Fyi, my knowledge of Avisynth is at the beginner's level. I've successfully used the program to join 9 videos into 1 video, and other basic stuff like cropping, resizing, etc.

Any help would be appreciated.
2. The last post in the thread linked below may be helpful to you. I experimented with the script several years ago (mainly just to see it work using AviSynth) and tweaked settings of RGB, opacity, etc. with varying results.

3. Thanks for the info. russell_t!

The script looks promising, but I'm having a hiccup with RGB32. The script says that the green screen video must be RGB32 (in bold below):

Code:
```clip1 = AviSource("background.avi")
clip2 = AviSource("greenscreen186by300.avi") #clip2 must be RGB32
# Second argument is RGB into hexadecimal numbers. Third argument is a tolerance.
But my green screen video is NV12. Do you know a way to get avisynth to convert the nv12 video to RGB32?

I tried this, to no avail:

Code:
`clip2 = AviSource("greenscreen186by300.avi").ConvertToRGB32()`
Or could I convert the video to RGB32 in VirtualDub or some other editor?
4. NV12 is treated the same as YV12 in avisynth. It's essentially the same thing, but U, V planes are interleaved and the memory layout is different

ConvertToRGB32() should work

Code:
```AviSource("greenscreen186by300.avi").ConvertToRGB32()
Info()```
What does this script say about the colorspace ?

Post the exact error message verbatim, and your avs script verbatim
5. I'm not sure what you mean about color space.

There's no error message. The background plays, but the green screen overlay doesn't appear. I've tried running the script in both VirtualDub and MediaPlayerClassic, but the same results. I have the script and the videos in the same folder, so it's not an issue of the files not being where they should be or named incorrectly.

Here's the code:

Code:
```clip1 = AviSource("background.avi")
clip2 = AviSource("greenscreen186by300.avi").ConvertToRGB32() #clip2 must be RGB32
# Second argument is RGB into hexadecimal numbers. Third argument is a tolerance.
FYI, both the green screen clip and the background clip have NV12. So I tried ConvertToRGB32() with the background clip as well. But it still just shows the background.

I don't know if the following information will be useful, but thought I should add it:

background clip: 23.98fps
green screen clip: 29.97fps

background clip: 608x352
green screen clip: 186x300
6. Could it be a case of needing to "tweak the settings of RGB, opacity, etc", as Russell stated?
7. Yes, adjust the hex value, maybe increase the tolerance

If you open the greenscreen video in avspmod, there is a hex color picker (mouse over some areas of the greenscreen, and it will show the corresponding hex values) . You can set it to show RGB values as well

It's working ok here on some test videos. If you still can't get it to work, post a small sample video

There is another avisynth plugin, colorscreenmask, as well

Other free non avisynth methods cinegobs keyer, blender. Blender will get you the best results out of the free methods by far, but learning curve is steep. It will get you similar results to professional keyers like keylight, primatte
8. @vidvidhh - For whatever it's worth, the two video clips in my experiment had the same frame rate and resolution. I'm uncertain if they need to be the same in this scenario, but I know they are a necessity in other areas of AviSynth usage, so it may be true in this case as well (perhaps poisondeathray or someone else can assist here). The RGB tweaking I performed was on the hex value of the green background (in ColorKeyMask). As I recall, I had downloaded a color chart with corresponding hex values, then used some of those values in various AviSynth scripts to see how the changes would effect keying. If you don't make the progress you'd like with AviSynth, then the CineGobs Keyer might be worth a look. Although no longer in development, it is still available for download. CineGobs doesn't support audio, but demuxing the audio from your source video and muxing it into your keyed video would be a possible option.

Edit: thanks poisondeathray for your post above.
9. I was able to get it working using a script I found on this thread: https://forum.videohelp.com/threads/184560-AviSynth-Chroma-Key

Code:
```clip1=avisource("greenscreen186by300.avi").converttorgb32()
clip2=avisource("background.avi").converttorgb32()
layer(clip2, transparent)```
It's a bit rough, but I think I'll be able to tweak it.

One thing that's different about this script is that clip1 is the green screen video and not the background.

I went back to the older script and switched some code around:

Code:
```clip1 = AviSource("greenscreen186by300.avi").ConvertToRGB32()
clip2 = AviSource("background.avi").ConvertToRGB32() #clip2 must be RGB32
# Second argument is RGB into hexadecimal numbers. Third argument is a tolerance.
It works somewhat, but the video takes on the size of the smaller green screen video, and the green appears/disappears in places and the dancing girl appears/disappears in places.

I also changed \$125d12 to \$0DB40C.

So I think I'll use the script I found above, which gives much better results.
10. Just to show you the difference with both codes:

With code Russell recommended:

Code:
```clip1 = AviSource("greenscreen186by300.avi").ConvertToRGB32()
clip2 = AviSource("background.avi").ConvertToRGB32() #clip2 must be RGB32
# Second argument is RGB into hexadecimal numbers. Third argument is a tolerance.

You can see that the green appears/disappears in places and the dancing girl appears/disappears in place. Also the video takes on the dimensions of the smaller green screen vid and not the bigger background vid.

With the code I got from this thread:

Code:
```clip1=avisource("greenscreen186by300.avi").converttorgb32()
clip2=avisource("background.avi").converttorgb32()
layer(clip2, transparent)```

I put a black bar over the naughty bits.

It's a bit rough, but at least she mostly appears and the video has the dimensions of the background video.
11. Change the Overlay() order (ie.. Overlay(clip 2, clip1) )

The syntax is Overlay(background image, foreground image, x=, y=) . You can reposition the foreground image by using x=, y= . x=0,y=0 is the top left corner . You can do the same with layer() if you wanted to move the girl to the middle

Overlay() takes the properties of the background clip (the clip you specify first). So different fps, different lenght, etc.. means it takes the background video's fps, length etc...
12. Thanks poisondeathray, but how do I incorporate that overlay positioning code into the following code?

Code:
```clip1=avisource("greenscreen186by300.avi").converttorgb32()
clip2=avisource("background.avi").converttorgb32()
layer(clip2, transparent)```
13. The same way . Layer() uses almost the same syntax as Overlay()

For example, if you wanted to move the girl 50 pixels to the right, 10 pixels down

layer(clip2, transparent, x=50, y=10)
14. Originally Posted by russell_t
@vidvidhh - For whatever it's worth, the two video clips in my experiment had the same frame rate and resolution. I'm uncertain if they need to be the same in this scenario, but I know they are a necessity in other areas of AviSynth usage, so it may be true in this case as well (perhaps poisondeathray or someone else can assist here). The RGB tweaking I performed was on the hex value of the green background (in ColorKeyMask). As I recall, I had downloaded a color chart with corresponding hex values, then used some of those values in various AviSynth scripts to see how the changes would effect keying. If you don't make the progress you'd like with AviSynth, then the CineGobs Keyer might be worth a look. Although no longer in development, it is still available for download. CineGobs doesn't support audio, but demuxing the audio from your source video and muxing it into your keyed video would be a possible option.

Edit: thanks poisondeathray for your post above.
I just made a green screen in CineGobs and it's pretty easy. There are no invisible bits, but the edges of the dancing girl were ragged. But I was able to overcome that somewhat by using the blur function.

CineGobs is a neat tool I intend to explore. But I still would like to pursue AviSynth and maybe become sophisticated enough with it to produce green screens as good as the guy in the YouTube clip I posted in the OP.
15. Honestly - you will have a very difficult time getting awesome chroma key results in avisynth unless your lighting was perfect. Good lighting on the greenscreen shoot makes everything easy. Avisynth is great for many things, unfortunately chroma/color keying is not one of them

There are a lot of "tricks" that FX people use to get good results. The "key" (nice pun) is trying to preserve edge detail. Unless your lighting was perfect, often you have to use multiple keys and composites with masks. It's just too difficult to do that sort of thing in avisynth . Much easier to do in other programs. It's difficult to address green spill in avisynth as well
16. Another reason you're probably having a very difficult time - your greenscreen shot is low resolution, probably 4:2:0, also probably full of compression artifacts

If your naming is correct, 186x300 is tiny, tiny. The chroma resolution would be 93x150 for 4:2:0. It's difficult to get tight around the subject and there will be a ring of ugly "crunchiness"
17. Originally Posted by poisondeathray
The same way . Layer() uses almost the same syntax as Overlay()

For example, if you wanted to move the girl 50 pixels to the right, 10 pixels down

layer(clip2, transparent, x=50, y=10)
Thanks! That worked.
18. Originally Posted by poisondeathray
Another reason you're probably having a very difficult time - your greenscreen shot is low resolution, probably 4:2:0, also probably full of compression artifacts

If your naming is correct, 186x300 is tiny, tiny. The chroma resolution would be 93x150 for 4:2:0. It's difficult to get tight around the subject and there will be a ring of ugly "crunchiness"
I'll take your advice and focus my attention on Cinegobs for keying. I've been playing around with Cinegobs and the results are much better, since it has lots of controls to tweak things.

A higher resolution version, with a scene from Blade Runner as the background:

Next step is figuring out how to get the background to not scrunch up to the dimensions of the green screen layer.

Statistics