Moviepy: Green Screen Video

Created on 27 May 2019  路  5Comments  路  Source: Zulko/moviepy

Hello, I have succeed the fact to remove green on a image and to add a video in the background, but it doesn't work with a green screen video. How can I achieve that ?

Here is the code that works:

ai=ImageClip("afterimage.png")
masked_clip = vfx.mask_color(ai, color=[0,255,1]) # for green

some_background_clip = VideoFileClip("random_stuff.mp4")

final_clip = CompositeVideoClip([some_background_clip, new],
                                    use_bgclip=True)
final_clip.duration=5
final_clip.write_videofile("afterimage.mp4", fps=30)

I want ai that is a ImageClip to be a VideoFileClip like a mp4 that as a green screen

documentation examples

Most helpful comment

I did not find much information in the official documentation.
Only from sourcecode: https://zulko.github.io/moviepy/_modules/moviepy/video/fx/mask_color.html#mask_color

import moviepy.editor as mp

clip = mp.VideoFileClip('/home/me/path/to/green-screen-video.mp4')
# Mountains background
background = mp.ImageClip('https://www.worldatlas.com/r/w728-h425-c728x425/upload/66/14/d8/kangchenjunga.jpg')

masked_clip = clip.fx(mp.vfx.mask_color, color=[0, 255, 8], thr=100, s=5)
# You can remove this resize, it's just for test...
masked_clip = masked_clip.resize(0.3).set_pos(('center', 'bottom'))

final_clip = mp.CompositeVideoClip([
    background,
    masked_clip
]).set_duration(1)

final_clip.write_videofile('test.mp4')

For each video I selected individually the parameters.

clip.fx(mp.vfx.mask_color, color=[0, 255, 8], thr=100, s=5)

I left the "s" parameter unchanged.
I only changed "thr" from 1 to 150 ... The higher the value of the parmatter, the better it removes the edges. But if it is too large, then the code starts to remove other colors besides green.

img2

All 5 comments

Anyone ?

I did not find much information in the official documentation.
Only from sourcecode: https://zulko.github.io/moviepy/_modules/moviepy/video/fx/mask_color.html#mask_color

import moviepy.editor as mp

clip = mp.VideoFileClip('/home/me/path/to/green-screen-video.mp4')
# Mountains background
background = mp.ImageClip('https://www.worldatlas.com/r/w728-h425-c728x425/upload/66/14/d8/kangchenjunga.jpg')

masked_clip = clip.fx(mp.vfx.mask_color, color=[0, 255, 8], thr=100, s=5)
# You can remove this resize, it's just for test...
masked_clip = masked_clip.resize(0.3).set_pos(('center', 'bottom'))

final_clip = mp.CompositeVideoClip([
    background,
    masked_clip
]).set_duration(1)

final_clip.write_videofile('test.mp4')

For each video I selected individually the parameters.

clip.fx(mp.vfx.mask_color, color=[0, 255, 8], thr=100, s=5)

I left the "s" parameter unchanged.
I only changed "thr" from 1 to 150 ... The higher the value of the parmatter, the better it removes the edges. But if it is too large, then the code starts to remove other colors besides green.

img2

@mowshon would you like to propose a PR with your answer as a doc example?

@mgaitan yeah sure!

To be honest, I don't know how to solve your problem, but I want to recommend a website with green screen, maybe something would be interesting for you, and you will make something like that https://greenscreenstock.com/

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tburrows13 picture tburrows13  路  3Comments

Swiffers picture Swiffers  路  4Comments

buddhashrestha picture buddhashrestha  路  3Comments

skizzy picture skizzy  路  3Comments

RahulPrasad picture RahulPrasad  路  4Comments