1
0
mirror of https://github.com/tiyn/wiki.git synced 2025-07-31 10:57:46 +02:00
wiki/wiki/linux/v4l2.md

98 lines
3.2 KiB
Markdown

# V4L2
V4L2 is the second version of
[Video4Linux](https://www.linuxtv.org/wiki/index.php/Main_Page).
It functions as a driver for [webcams](/wiki/webcams.md) and [tv tuner cards](/wiki/video.md) and
other devices.
## v4l2loopback
The following section is based on a
[gist by ioquatix](https://gist.github.com/ioquatix/18720c80a7f7eb997c19eef8afd6901e).
[v4l2loopback](https://github.com/umlaeute/v4l2loopback) is a utility to create
a virtual video device.
One use of it is in [OBS Virtual Camera](/wiki/obs.md#virtual-camera).
### Setup
To use v4l2loopback an according
package needs to be installed.
Some distributions have a package named `v4l2loopback-dkms` in the package
manager, making the manual installation process unnecessary.
Make sure to
[start the service](/wiki/linux/systemd.md#startstopenabledisable-a-service) by
running `systemctl start v4l2loopback.service` and/or enable it to start after each
boot with `systemctl enable v4l2loopback.service`.
### Usage
This section focusses on various use-cases for the v4l2loopback software.
### Create Temporary V4L2 Devices
v4l2loopback can be manually started by running:
```sh
modprobe v4l2loopback card_label='V4L2 Loopback' video_nr=7 exclusive_caps=1
```
`video_nr=7` states the device path to use with v4l2loopback - in this case
`/dev/video7`.
This can be changed according to the needs.
It is important that the device is not already in use by another application.
To create more than one device more labels or numbers can be appended.
An example command with 2 V4L2 video devices will look like the following command.
```sh
modprobe v4l2loopback card_label='OBS Virtual Camera','IP Webcam' video_nr=7,8 exclusive_caps=1
```
### Create Permanent V4L2 Devices
For an automatic start at boot via [SystemD](./systemd.md) a service can be
created - as described in [the SystemD entry](./systemd.md#run-command-on-boot).
For v4l2loopback create the file `/etc/systemd/system/v4l2loopback.service`
containing the following lines:
```txt
[Unit]
Description=V4L2 Loopback
[Service]
Type=simple
RemainAfterExit=yes
ExecStart=/bin/modprobe v4l2loopback card_label='OBS Virtual Camera' video_nr=7 exclusive_caps=1
ExecStop=/bin/rmmod v4l2loopback
[Install]
WantedBy=default.target
```
Note the line starting with `ExecStart=/bin/` is the same line described in the
[above section](#create-temporary-v4l2-devices).
Make sure to change it if needed.
Afterwards enable the service to start at every boot process by running:
```sh
sudo systemctl enable v4l2loopback
```
## Troubleshooting
This section focusses on various errors that can appear in the v4l2loopback software.
### `Failed to start streaming on '/dev/video2' (Invalid argument)`
This error does occur most of the time when trying to start
[OBS' virtual camera](/wiki/obs.md#virtual-camera).
This section is based on an
[OBS Forum entry by rebelduck](https://obsproject.com/forum/threads/obs-virtual-camera-failed-to-start-streaming-on-dev-video2-invalid-argument.184717/)
To fix it downgrade the packages `v4l2loopback-dkms` and `v4l2loopback-utils` to the last working
version.
For [Arch Linux](/wiki/linux/arch-linux.md) this can be achieved by using
[Downgrade](/wiki/linux/package_manager.md#downgrading-packages).