25

I would like to try using Cheese in Google+ Hangouts, and similar configurations, and I would like to know if it is possible for the webcam to be used by two processes to have access to the webcam at the same time. I have not tried it as yet, but before I do, I figured I would ask.

So, is it possible?

6 Answers 6

26

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!

5
  • 1
    Virtual devices is one of the areas where Linux (and *nix, for that matter) shines. It is more complicated on Windows to even write a basic tee utility that doesn't wait for stdin to finish feeding it stuff. I wouldn't be surprised if this actually uses a binary equivalent to tee with more scalable support under the hood.
    – Claudia
    Aug 12, 2014 at 6:30
  • 2
    Whilst this does answer the question, it would be preferable to include parts of the link here, and provide the link for reference, so that we have some examples - e.g. make 2 devices from 1. What are the options for that? And is it possible (as said in the OP's requirements) to use with Google hangouts? I also just tested it with Skype and it doesn't seem to work. We are now version 4.3 not 4.0...
    – Tim
    Aug 12, 2014 at 7:46
  • 2
    I'll have to check this out when my other system gets back. I'll probably give an additional bounty.
    – RolandiXor
    Aug 12, 2014 at 8:20
  • 3
    This is now actually part of ubuntu - no need for git. Nov 12, 2018 at 14:06
  • Indeed, this is now packaged for Ubuntu / Debian as v4l2loopback-utils No need to self compile this Nov 12, 2019 at 14:54
10

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

1
  • This answer worked for me.
    – Abbas
    May 8, 2021 at 12:04
7
+100

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.

4
  • Thanks! This is concise and tells me what I need to know. Nice garden btw! The bounty will be awarded when the timeout expires (21 hrs to go). Then I will delete this comment.
    – RolandiXor
    Aug 6, 2014 at 19:07
  • @Mew That was my last attempt at getting it to work :)
    – Tim
    Aug 9, 2014 at 18:25
  • @Tim The next answer is exactly what you're looking for if you came here because you need a way to do that.
    – Claudia
    Aug 12, 2014 at 6:38
  • 1
    @impinball nope, just found it an interesting question!
    – Tim
    Aug 12, 2014 at 7:41
7

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.

2
  • BTW if one's webcam has audio then filter audio devices using the command pactl list | grep -A2 'Source #' then use that device in the following pipeline (can run multiple instances of pipeline too) gst-launch-1.0 pulsesrc device=alsa_input.<name of device> ! autoaudiosink Feb 7, 2019 at 22:09
  • This works much better than the FFmpeg solution as I couldn't get FFmpeg to copy h264 without decoding it which resulted in it always using the CPU even when no one was listening
    – Fabian N.
    Oct 8, 2019 at 20:01
2

The answer is no with a maybe... The Library, "Video 4 Linux" detects whether a device is being used, and will not initiate. However; if you wish to download libv4l-dev and edit it manually, you can edit the code to get rid of this check and then just recompile it. I imagine you could get this to work, but it would be extremely unstable. May be worth checking out though.

TLDR: Anything is possible if you are a programmer with some elbow grease handy. Don't want to go that far? Then no; its not possible.

2

I have success using OBS Open Broadcast Software after the following steps:

  1. Install video4linux loopback device, v4l2loopback as suggested by others. I am on Ubuntu 18.04 and had to install a newer version as suggested here

  2. Install OBS. Again I had to install a newer version and ffmpeg as per these instructions.

  3. Install OBS-v4lsink. Go here and download and install the .deb file, version 0.1.0 was the only deb file there.

  4. Start v4l2loopback:

     sudo modprobe v4l2loopback
    
  5. In OBS and select a source. Under "sources" click on "+", give your new layer a name, select your (real) webcam.

  6. In OBS select a sink. In the top menu go to Tools > v42lsink, select the virtual webcam (/dev/videoX, with X being the highest number available), click start.

  7. In your applications, select the virtual webcam. I had Zoom and Discord both using the same new virtual webcam and was also able to record from the camera using OBS.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .