Skip to content

Instantly share code, notes, and snippets.

@miraculixx
Last active August 31, 2021 01:30
Show Gist options
  • Save miraculixx/49a3a8cad895f8a72b3a6bdc089d0580 to your computer and use it in GitHub Desktop.
Save miraculixx/49a3a8cad895f8a72b3a6bdc089d0580 to your computer and use it in GitHub Desktop.
ffmpeg green screen chromakey virtual background for linux
setup
1. Install video 4 linux (v4l)
https://github.com/umlaeute/v4l2loopback
2. Install ffmpeg
https://linuxize.com/post/how-to-install-ffmpeg-on-ubuntu-18-04/
3. Make camera cap exlusive (otherwise Chrome cant access the vcam video
sudo modprobe v4l2loopback -r && sudo modprobe v4l2loopback video_nr=4 exclusive_caps=1 card_label="vcam"
4. Enable access by chrome, zoom etc. user apps
chmod 660 /dev/video4
#!/bin/bash
IMAGE=${1:-$HOME/Pictures/camback_640480.png}
FRAME_RATE=35
CAMERA=/dev/video2
OUTPUT=/dev/video4
SIZE=640x480
# make sure access is granted to user processes, e.g. chrome, zoom
sudo modprobe v4l2loopback -r
sudo modprobe v4l2loopback devices=1 video_nr=4 exclusive_caps=1 card_label="vcam"
sudo chmod 660 $OUTPUT
ffmpeg -thread_queue_size $FRAME_RATE -filter_complex_threads $FRAME_RATE -y -hide_banner -i $IMAGE \
-f v4l2 -pix_fmt mjpeg -framerate $FRAME_RATE -video_size $SIZE -i $CAMERA -input_format mjpeg \
-filter_complex "[1:v]chromakey=green:0.16:0.0[raw];[raw]crop=in_w-200:in_h:200:200[ckout];[0:v][ckout]overlay=200:000[out]" \
-map "[out]" \
-pix_fmt yuyv422 \
-f v4l2 $OUTPUT
# installation:
# sudo apt-get install v4l2loopback-dkms
# sudo apt install ffmpeg
# sudo apt install v4l-utils
@miraculixx
Copy link
Author

This is the Linux equivalent of a Zoom Virtual Background. Works with Chrome, Meetup, Skype et al

Why? Meetup and Skype don't have virtual backgrounds on Linux. Zoom does. This makes it available verywhere.
Caveats? Framerate is relatively slow. Could only get it to about 25fps. If you have any idea let me know

Thanks to Steven Varga, who inspired me to try ffmpeg. His solution is more convenient, however it uses a different filter setup that did not work for me. https://github.com/steven-varga/chromakey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment