8

I'm using OBS Studio and would like to have a virtual video output saved permanently so that it can be run when the application is launched.

This command creates the virtual output:

sudo modprobe v4l2loopback video_nr=10 card_label="OBS Video Source" exclusive_caps=1

But I must run the command in console on each boot. How can I make this permanent? The plugin that utilizes the output in OBS Studio is configured to auto start but /dev/video10 is not available after a reboot of the system.

$ modinfo v4l2loopback | grep -i parm
parm:           debug:debugging level (higher values == more verbose) (int)
parm:           max_buffers:how many buffers should be allocated (int)
parm:           max_openers:how many users can open loopback device (int)
parm:           devices:how many devices should be created (int)
parm:           video_nr:video device numbers (-1=auto, 0=/dev/video0, etc.) (array of int)
parm:           card_label:card labels for every device (array of charp)
parm:           exclusive_caps:whether to announce OUTPUT/CAPTURE capabilities exclusively or not (array of bool)
parm:           max_width:maximum frame width (int)
parm:           max_height:maximum frame height (int)
Eliah Kagan
  • 119,820
Mark Lee
  • 277
  • One on the proper methods was to add the module name to the /etc/modules file . But nowadays the udev subsystem should insert the required drivers automatically . But if you are sure that it doesn't , add the name of the module to the modules file and reboot to check out the result. – Parsa Mousavi May 30 '20 at 15:56
  • There's an /etc/modprobe.d and /etc/modules-load.d but no /etc/modules directory on my system. – Mark Lee May 30 '20 at 16:12
  • @Parsa Mousavi, I had erroneously looked for an /etc/modules folder rather than a file. – Mark Lee May 30 '20 at 22:12

2 Answers2

11

Normally kernel modules can be added to /etc/modules for loading at boot time.


Add this to /etc/modules...

v4l2loopback video_nr=10 card_label="OBS Video Source" exclusive_caps=1

However, I don't know if you can pass parameters there. So here's another way to do it.

Add this to /etc/modules...

v4l2loopback

Create /etc/modprobe.d/v4l2loopback.conf

options v4l2loopback video_nr=10 card_label="OBS Video Source" exclusive_caps=1

Then...

sudo update-initramfs -c -k $(uname -r)

reboot

Confirm module loading with...

lsmod | grep -i v4l2loopback
heynnema
  • 73,937
  • I created /etc/modprobe.d/v4l2loopback.conf with the option and rebooted and the loopback seems to have loaded. I'll shut down and restart presently because I noticed that sometimes a reboot comes up with the settings that were present.

    The result of grep same as before reboot: v4l2loopback 40960 0 videodev 225280 4 videobuf2_v4l2,v4l2loopback,uvcvideo,videobuf2_common

    – Mark Lee May 30 '20 at 16:48
  • @MarkLee You added the line to /etc/modules, yes? I added an update-initramfs command to my procedure. – heynnema May 30 '20 at 16:51
  • @MarkLee Edit your question and show me modinfo v4l2loopback | grep -i parm. – heynnema May 30 '20 at 17:00
  • There's no /etc/modules on my system. – Mark Lee May 30 '20 at 17:04
  • @MarkLee Create one. sudo pico /etc/modules. Just add v4l2loopback to it. Then do the update-initramfs command. Then reboot. – heynnema May 30 '20 at 17:06
  • op edited as requested. – Mark Lee May 30 '20 at 20:37
  • @MarkLee After creating both files and doing the update-initramfs, and rebooting, is it all working now? – heynnema May 30 '20 at 20:41
  • It didn't work and as I checked /etc/modprobe.d/v4l2loopback.conf just now what was listed in the directory instead was v4l2loopback.conf.save. I just wrote the file again and am about to reboot once more. – Mark Lee May 30 '20 at 20:44
  • Keep me posted. – heynnema May 30 '20 at 20:52
  • I think you've got me over the hump. After a few glitches--1. OBS refused to start (commented out the line in the conf and it started), 2. started with no connection to the camera (stopped the plug-in and uncommented the conf)--I have camera input and video10 output on starting. Hopefully not a one off "magic". Thanks. – Mark Lee May 30 '20 at 21:40
  • @MarkLee Sounds great... I think... so, to sum up... you were able to complete the second procedure in my answer... and it seems to be working now? – heynnema May 30 '20 at 21:48
  • Can't get this to work on 20.04. lsmod | grep -i v4l2loopback doesn't print anything. ❯ cat /etc/modprobe.d/v4l2loopback.conf options v4l2loopback video_nr=10 card_label="OBS Video Source" exclusive_caps=1 – Vala Sep 01 '20 at 15:04
  • 1
    @Thor84no Did you add v4l2loopback to /etc/modules, create the .conf file, update-initramfs, and reboot? – heynnema Sep 01 '20 at 15:22
  • Oh, I didn't realise the v4loopback to /etc/modules was required in addition to the /etc/modprobe.d/v4l2loopback.conf, sorry. I guess I misread your post. It seems to work now. Thanks! – Vala Sep 03 '20 at 08:41
6

Easy way to do it un Ubuntu 20.04 and others:

$ sudo echo "v4l2loopback" > /etc/modules-load.d/v4l2loopback.conf 
$ sudo echo "options v4l2loopback video_nr=10 card_label=\"OBS Video Source\" exclusive_caps=1" > /etc/modprobe.d/v4l2loopback.conf

You can restart to check it works!

NOTICE

Be aware of a bug on v4l2loopback-dkms 0.12.3-1ubuntu0.1

More info on this stackoverflow post.

EDIT - Using tee

As pointed by renyhp, it's better to use the tee command:

$ sudo echo "v4l2loopback" | tee /etc/modules-load.d/v4l2loopback.conf 
$ sudo echo "options v4l2loopback video_nr=10 card_label=\"OBS Video Source\" exclusive_caps=1" | tee /etc/modprobe.d/v4l2loopback.conf

EDIT - Update modules

As commented, it seems that in some systems after rebooting some problems can be experienced. To avoid that, it's always a good idea to update the modules:

sudo update-initramfs -c -k $(uname -r)
PabloRQ
  • 319
  • 1
    This is the simple answer, but it will give Permission denied, since sudo applies to echo and not to redirection. You may actually want to pipe echo into sudo tee – renyhp Dec 03 '20 at 08:26
  • Actually: yes, if it used sudo tee this would be the simple way to add the needed configurations. And thank you for the warning about the bug. However, DO NOT FORGET TO DO sudo update-initramfs -c -k $(uname -r) as the other answer reported. I didn't, and I regretted it (keyboard not working; had to run a live Ubuntu to remove the conf files under etc) – renyhp Dec 03 '20 at 09:07
  • for me, i have trouble to downgrade v4l2loopback-dkms (installed v4l2loopback-dkms package post-installation script subprocess returned error exit status 1) – ses just now

    actually I already have 0.12.3-1ubuntu0.4 - and problem is there too. not sure where to downgrade then.. even if I did not have error 1

    – ses Aug 29 '21 at 05:06
  • @renyhp: thank you for your comments. The solution was updated. – PabloRQ Aug 30 '21 at 11:13
  • @ses I've already commented this in the other solution. – PabloRQ Aug 30 '21 at 11:15