How can I stream my desktop/screen to /dev/video1 as a (fake) "webcam" on Linux?

15,176

Solved.

Steps to solve:

  1. Unload previous v4l2loopback sudo modprobe -r v4l2loopback
  2. git clone https://github.com/umlaeute/v4l2loopback/
  3. make && sudo make install (if you're using secure boot, you'll need to sign it first https://ubuntu.com/blog/how-to-sign-things-for-secure-boot)
  4. sudo depmod -a
  5. Load the videodev drivers sudo modprobe videodev
  6. sudo insmod ./v4l2loopback.ko devices=1 video_nr=2 exclusive_caps=1 Change video_nr based on how many cams you already got. Zero indexed
  7. ls -al /dev/video* Use /dev/video[video_nr] with ffmpeg
  8. sudo ffmpeg -f x11grab -r 60 -s 1920x1080 -i :0.0+1920,0 -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 -vf 'hflip,scale=640:360' /dev/video2
  9. Go to https://webcamtests.com and test your dummy cam dummy cam
  10. Profit! working

If you want this to persist between boots, https://askubuntu.com/a/1024786/721238 should do it.

Share:
15,176

Related videos on Youtube

dǝɥɔS ʇoıןןƎ
Author by

dǝɥɔS ʇoıןןƎ

Updated on September 18, 2022

Comments

  • dǝɥɔS ʇoıןןƎ
    dǝɥɔS ʇoıןןƎ almost 2 years

    I have two commands, one that lets me record my screen to an AVI video file, and another which lets me stream a video file as a (fake) "webcam". This is really useful in apps that doesn't support selecting one screen to share (I'm looking at you Slack).

    command #1 (https://askubuntu.com/a/892683/721238):

    ffmpeg -y -f alsa -i hw:0 -f x11grab -framerate 30 -video_size 1920x1080 -i :0.0+1920,0 -c:v libx264 -pix_fmt yuv420p -qp 0 -preset ultrafast screenStream.avi

    command #2 (https://unix.stackexchange.com/a/466683/253391):

    ffmpeg -re -i screenStream.avi -map 0:v -f v4l2 /dev/video1

    Why can't I just run both of these in parallel? Well, the second command starts streaming from the beginning of the file, whenever I use my "webcam". So I have to time it really close, otherwise there is latency.

    I've tried lots and lots of solutions (including solutions with gstreamer instead of ffmpeg), can't get anything to work. This is my last hope.

    How can I stream my desktop/screen to /dev/video1 as a (fake) "webcam" on Ubuntu?

    • Bart
      Bart almost 5 years
      did you try v4l2loopback? askubuntu.com/questions/881305/…
    • dǝɥɔS ʇoıןןƎ
      dǝɥɔS ʇoıןןƎ almost 5 years
      Yes, the closest I got with that (after building the latest version from source) was sudo ffmpeg -f x11grab -r 60 -s 1920x1080 -i :0.0+1920,0 -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 -vf 'hflip,scale=640:360' /dev/video1. It's not detected on Chrome, and Slack, though it does work on Firefox. I found some bug reports about that, but I can't remember where. I can try to find them again if it helps
    • dǝɥɔS ʇoıןןƎ
      dǝɥɔS ʇoıןןƎ almost 5 years
      I should say, I'm using webcamtests.com to test wether my "webcam" is working, rather than calling my collegues every time. If it works on that site on Chrome, the Slack app will probably also work. I know this because I actually had a working solution a few months ago. I no longer have my working solution :/
    • Bart
      Bart almost 5 years
      weirdly enough, this works on my CentOS 7 Chrome, using this command: ffmpeg -f x11grab -r 15 -s 1280x720 -i :0.0+0,0 -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video0
    • Bart
      Bart almost 5 years
      and am not sure this is still relevant, but see: bugs.chromium.org/p/chromium/issues/detail?id=757399
    • dǝɥɔS ʇoıןןƎ
      dǝɥɔS ʇoıןןƎ almost 5 years
      Very interesting. I'm running Ubuntu bionic. I get the two cams on Firefox, but on Chrome I only get my itegrated one. See my screenshot of the media devices imgur.com/a/SmDR84b (couldn't take screenshot of webcamtests.com). That bug report is relevent. I'm guessing Skype and Slack both use Electron, which I think is based on Chromium, so it makes sense they're affected
    • dǝɥɔS ʇoıןןƎ
      dǝɥɔS ʇoıןןƎ almost 5 years
      Might have just solved it using github.com/umlaeute/v4l2loopback/issues/78. Will answer question once I confirm
    • Coder Guy
      Coder Guy over 3 years
      Combine this with tmtimer.calebgrove.com and you've got a very suitable Toastmasters timer (without the need for Zoom screensharing) for your timer role.
  • dǝɥɔS ʇoıןןƎ
    dǝɥɔS ʇoıןןƎ almost 5 years
    @Bart, thanks for the help, couldn't have done it without you!
  • pbhj
    pbhj about 4 years
    For me it wasn't necessary to download from github, just sudo apt install v4l2loop*. As I was using generally available modules I just used modprobe where the answer uses insmod. Other than that, very useful. I was able to play a file using -stream_loop -1 as if it were a webcam feed.
  • undercat
    undercat about 4 years
    In my case (Ubuntu 18.04) it was the old v4l2loop version from the repo that was the problem, as it turns out. This answer explains how to use the latest github version that does work with my distro. Thanks so much!