A bash script that converts green screen video into transparent video files for use on the web.
It produces two output files from a single input:
.webm(VP9 with alpha) for Chrome, Firefox, and Edge.mov(HEVC with alpha) for Safari
- ffmpeg with
libvpx-vp9andhevc_videotoolbox(macOS) support - Python 3 (for auto-detecting the green screen color)
On macOS with Homebrew:
brew install ffmpeg python./make-video-transparent.sh [options] <input>| Flag | Description | Default |
|---|---|---|
-c, --color <hex> |
Green screen color, e.g. 97c64c |
auto-detect |
-s, --similarity <float> |
Color match tolerance, 0.0-1.0 | 0.12 |
-b, --blend <float> |
Edge blend amount, 0.0-1.0 | 0.05 |
-o, --output <dir> |
Output directory | same as input |
-h, --help |
Show help |
# Auto-detect green screen color from the video
./make-video-transparent.sh video.mp4
# Specify the green color manually
./make-video-transparent.sh -c 97c64c video.mp4
# Tweak similarity and blend for cleaner edges
./make-video-transparent.sh -s 0.15 -b 0.08 video.mp4
# Output to a specific directory
./make-video-transparent.sh -o ~/Desktop/output video.mp4<video autoplay loop muted playsinline>
<source src="video.mov" type="video/mp4; codecs=hvc1">
<source src="video.webm" type="video/webm">
</video>Then change the background with CSS:
video {
background-color: rebeccapurple;
}See the live demo at https://zeke.github.io/make-video-transparent — it shows a transparent video with a color picker to dynamically change the background.
The demo/ directory contains the source for this page. To test locally with your own videos, generate transparent videos with the script, drop them in demo/, and open demo/index.html in a browser.
- Auto-detects the green screen color by sampling a 200x200 pixel patch from the top-left corner of the first frame
- Applies ffmpeg's
colorkeyfilter to replace matching pixels with transparency - Encodes to WebM (VP9) and MOV (HEVC) with alpha channels
The color auto-detection assumes the top-left corner of the video is green screen. If your subject covers that area, pass the color manually with -c.