A virtual webcam

There are a lot of fun things you can do with Linux. What I propose you today is to stream an arbitrary video as if it were your webcam and microphone output.

Some sort of disclaimer

Okay, so the draft of this blog post has been sitting on my hard drive for months now, and if I don’t post it yet, I’ll never post it. The reason I didn’t is because it is incomplete, as there’s a tiny something I don’t understand. I lost interest in this so I won’t look for the solution myself, but if you find it you’re welcome to share it with me! Anyway, let’s see this.

Setup a virtual webcam

First, install v4l2loopback. It’s a kernel module for Linux we will use to create a virtual webcam.

Then we will check for already existing webcams. Like almost every devices in UNIX, they’re located in /dev/ and their names are video followed by a number.

$ ls /dev/ | grep video
video0

We see here that I have one webcam: video0.

Let’s load v4l2loopback:

# modprobe v4l2loopback

Now we’ll see if our virtual webcam is there:

$ ls /dev/ | grep video
video0
video1

Yes, it is!

Setup a virtual microphone

Actually, we won’t create a microphone, we’ll create a soundcard. Microphones are handled by soundcards, which deal with the collected data.To create a virtual soundcard we will proceed similarly as we did with the webcam, first we list the connected soundcards:

Stream video to the virtual webcam

Video only

We’ll use ffmpeg to extract a stream from a file and input it to the virtual webcam, in this case /dev/video1. You don’t have to, but you should read at least its synopsis.

$ ffmpeg -re -i 'your/file.avi' -f v4l2 /dev/video1

If it doesn’t work, you’ll have to explicitly set options for the input file, read ffmpeg‘s manpage to know those.

Sound only

Ok, this is the part I’m unsure about. I don’t get why I have to specify ,1 in hw:3,1 . If this setting doesn’t work for you, well, try trial and error. And I you know why it’s this and not anything else, I’ll be glad to hear why!