Is it possible for two processes to access the webcam at the same time?

Solution 1:

Happily, now, YES!

There is currently a project on GitHub that can show not only two, but an infinite (limited only by system capacity) number of video devices from a single source.

The project is called v4l2loopback. (Check the link and scroll down to view the README for instructions.)

You can install v4l2loopback by cloning its GitHub and running these commands:

$ git clone https://github.com/umlaeute/v4l2loopback.git
$ cd v4l2loopback
$ make
$ sudo make install

Examples

For samples on how to use it with GStreamer, FFmpeg, MPlayer, and Skype, check their small wiki. Hopefully you could find the right settings to fit your needs.

Enjoy tweaking around!

Solution 2:

Well I just tried. Interesting results.

I have got skype and cheese and webcam toy and hangouts (which all do work) and tried them together.

Results:

You can't use the same webcam on 2 things at once.

Cheese (Working) + Webcam Toy (Not working) + Skype (Small, top right. Not working).

enter image description here

You can use 2 webcams on 2 things though. (Cheese + Webcam Toy).

enter image description here

Hangouts (Not working) + Cheese (Working)

enter image description here

Hangouts (Working on it's own).

enter image description here

Cheese broken when I opened Hangouts first. It did the same with the others, but my internet is too slow to upload every single screenshot (and I doubt you'd appreciate 30+ screenshots)...

enter image description here

I even tried making a symbolic link to /dev/video0 and saving it in /dev as video2. That didn't work.

I also can't run 2 cheese processes at once.

If you had to, I would advise making it full screen and doing 2 screen recordings. I doubt that would work for what you want though.

Solution 3:

Based on @The Eye answer

I first installed gstreamer packages on Ubuntu 18.04 https://gstreamer.freedesktop.org/documentation/installing/on-linux.html

$ git clone https://github.com/umlaeute/v4l2loopback.git
$ cd v4l2loopback
$ make
$ sudo make install

I got warning message as here on Ubuntu 18.04 LTS https://github.com/umlaeute/v4l2loopback/issues/139 (but it didn't prevent me from loading v4l2loopback driver)

$ sudo depmod -a

I just have 1 webcam on my laptop /dev/video0 and I wanted to get 2 streams from the same hardware. Based on https://github.com/umlaeute/v4l2loopback/blob/master/README.md

$ modprobe v4l2loopback devices=2

There should now be /dev/video1 and /dev/video2 created assuming /dev/video0 was the only video device.

Now I run the following in one terminal window

gst-launch-1.0 v4l2src device=/dev/video0 ! tee name=t ! queue ! v4l2sink device=/dev/video1 t. ! queue ! v4l2sink device=/dev/video2

I open 2 more tabs

In the first tab

gst-launch-1.0 v4l2src device=/dev/video1 ! videoconvert ! ximagesink

In the second tab

gst-launch-1.0 v4l2src device=/dev/video2 ! videoconvert ! ximagesink

Now one should see 2 video streams

UPDATE

Even if I use the same /dev/video1 device multiple times it all gives me that many stream. example.

In the first tab

gst-launch-1.0 v4l2src device=/dev/video1 ! videoconvert ! ximagesink

In the second tab

gst-launch-1.0 v4l2src device=/dev/video1 ! videoconvert ! ximagesink

In the third tab

gst-launch-1.0 v4l2src device=/dev/video1 ! videoconvert ! ximagesink

gives me three streams.

Solution 4:

Since there is no answer using ffmpeg, this is it.


Explanation

While v4l2loopback creates virtual video devices, they are not associated with anything by default. To make them useful, you have to send streams to them via external applications such as ffmpeg.

First load the module

$ sudo modprobe v4l2loopback exclusive_caps=1 video_nr=5 #creates `/dev/video5`

and then send your webcam's input to the virtual device.

$ ffmpeg -i /dev/video0 -f v4l2 -codec:v rawvideo -pix_fmt yuv420p /dev/video5

Now /dev/video5 works as a pseudo-webcam. Note, unlike a physical webcam, a single virtual video device can be accessed from multiple applications simultaneously. Thus, in this case, you don't have to create many virtual devices.

I wrote a script to wrap these steps: v4l2_webcam - GitHub.


Application

You can send any (raw) video to a virtual video device. This means, for example, it is possible to use a complicatedly mixed video by OBS as a camera input to a Web meeting (e.g. Skype, Zoom, etc.).

Here is a relatively complex but very practical example:

Structure

example_structure

Result

example_result