9

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?

8
  • did you try v4l2loopback? askubuntu.com/questions/881305/… – Bart Jul 4 '19 at 14:32
  • 1
    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 – Chicken Suop Jul 4 '19 at 14:38
  • 1
    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 :/ – Chicken Suop Jul 4 '19 at 14:55
  • 1
    and am not sure this is still relevant, but see: bugs.chromium.org/p/chromium/issues/detail?id=757399 – Bart Jul 4 '19 at 15:18
  • 1
    Might have just solved it using github.com/umlaeute/v4l2loopback/issues/78. Will answer question once I confirm – Chicken Suop Jul 4 '19 at 15:34
11

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.

3
  • 1
    @Bart, thanks for the help, couldn't have done it without you! – Chicken Suop Jul 4 '19 at 15:47
  • 2
    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. – pbhj Apr 12 '20 at 23:46
  • 2
    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! – undercat applauds Monica Apr 21 '20 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.