There can be a lot of reasons you would want to create a virtual webcam on your machine. Your machine might not have a webcam, or it could be broken. You might want to mix your webcam video with some cool effects. Or you might have more obscure ideas in mind but that’s none of my business.

What is a virtual WebCam

Let’s first define what it is. A virtual webcam or emulated webcam is a piece of software that emulates the hardware of a webcam. To the kernel (on a Linux environment) it looks like there is a physical webcam attached to the system. You can then use some applications to stream content to that fake webcam. Finally you can use that as a recording device in the video-conferencing application or video-chats.

Here comes v4l2loopback

So I am biased by the fact I have been using Ubuntu for a long time, and this tutorial is essentially for myself, I will only cover Ubuntu. The most common option you will find is v4l2loopback. It is actually part of the supported applications. You just need to install it via apt-get

sudo apt install v4l2loopback-dkms ffmpeg

Then check it is working by enabling it and streaming to it:

sudo modprobe v4l2loopback devices=1 video_nr=10 max_buffers=2 exclusive_caps=1 card_label="Default WebCam"
ffmpeg -stream_loop -1 -re -i path-to-videofile.mp4 -f v4l2 -vcodec rawvideo -s 640x480 /dev/video10

Then open VLC or a webcam enabled application and you should see the selected video playing as the webcam.

Let’s review the parameters of modprobe:

  • devices=1: this creates 1 virtual webcam. You can create more than one by changing this number
  • video_nr: a comma separated list of numbers. This tells which device id it will create. You should have the same number of device ids as the value for devices. I.e. if devices=3 you should have video_nr=2,3,4 (or any 3 numbers). Here I selected 10 so it will create /dev/video10.
  • card_label: the name the virtual webcam will appear in the settings of the application using it

For the ffmpeg command parameters:

  • -stream_loop -1: will loop the video forever. Set a positive number for a specific number of loops of the video to be played
  • -re: honestly I copied this from a SO answer. It might not be needed 🙂
  • -i: the input file
  • -f v4l2: the format to Video4Linu v2
  • -vcodec rawvideo: I found it made all videos I try working well
  • -s 640x480: any usual webcam resolution will do.
  • /dev/video10: the device I created with the modprobe command

Survive a reboot

Once it is working well, you need to configure modprobe to create the device at each reboot.
First edit the file /etc/modules and add a new line with

v4l2loopback

Then create a file /etc/modprobe.d/v4l2loopback.conf with this content (all in one line):
options v4l2loopback devices=1 video_nr=10 max_buffers=2 exclusive_caps=1 card_label="Default WebCam"

Then reboot and check the device is created and you can stream video to it.

Troubleshooting

At first I couldn’t make it to create the right configuration on reboot. I added the options into an existing file under /etc/modprobe.d/ folder.