Linux Kernel Module: v4l2loopback Virtual Video Device
Introduction
v4l2loopback – a kernel module to create V4L2 loopback devicesThis module allows you to create “virtual video devices”. Normal (v4l2) applications will read these devices as if they were ordinary video devices, but the video will not be read from e.g. a capture card but instead it is generated by another application. This allows you for instance to apply some nifty video effects on your Zoom video call… It also allows some more serious things (e.g. I’ve been using it to add streaming capabilities to an application by the means of hooking GStreamer into the loopback devices).
This module allows you to create “virtual video devices”. Normal (v4l2) applications can read these virtual devices as if they were ordinary video devices, but the video is not read from, for example, a capture card; instead, it is generated by another application. This enables you to apply cool video effects to your Zoom video calls… It also facilitates more serious functionalities (for instance, I have been adding streaming capabilities to an application by integrating GStreamer into the loopback devices).
<span>v4l2loopback</span> is a Linux kernel module used to create virtual video devices. Through these virtual devices, users can pass video streams from one program to another, simulating a camera or sharing the output of a physical camera.
Official Repository: https://github.com/v4l2loopback/v4l2loopback
Arch Linux Documentation: https://wiki.archlinux.org.cn/title/V4l2loopback
Features and Use Cases
<span>v4l2loopback</span>‘s main function is to create virtual camera devices (such as <span>/dev/video0</span>), which can be read by regular V4L2 applications. Common applications include:
- • Simulating a camera without a physical camera.
- • Using desktop screen recordings or video files as virtual camera input.
- • Allowing multiple programs to share the video stream from a physical camera.
- • Adding effects or filters during video conferences.
Installation and Configuration
Install Driver Module
- • Install using a package manager:
sudo apt-get install v4l2loopback-dkms - • Alternatively, download the source code and compile it yourself:
git clone https://github.com/v4l2loopback/v4l2loopback.git cd v4l2loopback make sudo make install
Load Module
Load the <span>v4l2loopback</span> module and create virtual devices:
sudo modprobe v4l2loopback
<span>v4l2loopback</span> can load various device creation options:
sudo modprobe v4l2loopback video_nr=9 card_label=Video-Loopback exclusive_caps=1
This creates <span>/dev/video9</span> as a loopback device. The <span>exclusive_caps=1</span> option is necessary for some Chromium/WebRTC-based applications. This enables <span>exclusive_caps</span> mode, which exclusively reports CAPTURE/OUTPUT capabilities. The newly created device will only declare OUTPUT capabilities (thus ordinary webcam applications, including Chrome, will not see it). Once you connect a producer to the device, it will start declaring only CAPTURE capabilities (therefore applications that attempt to open the device with capabilities other than capture will be denied). More options can be found in the official documentation.
You can create multiple loopback devices using the <span>devices</span> parameter. Options for each device are specified by commas. The following command creates three loopback devices, <span>/dev/video8</span>, <span>/dev/video9</span>, and the first available <span>/dev/videoX</span>, all with <span>exclusive_caps</span> enabled.
sudo modprobe v4l2loopback devices=3 video_nr=8,9 exclusive_caps=1,1,1 card_label="Loopback-1,Loopback-2,Loopback-3"
Verify Devices
Check if the virtual devices were created successfully:
# Directly check video device nodes to see if virtual devices are present
ls /dev/video*
# You can also use v4l2-ctl
v4l2-ctl --list-devices
Usage Examples
View Virtual Video Devices
Loopback virtual video devices can be tested and viewed using any of the following methods:
- •
<span>ffplay</span>(part of<span>ffmpeg</span>), - •
<span>mpv</span> - •
<span>gst-launch</span>(part of<span>gstreamer</span>)
$ ffplay /dev/video0
$ mpv av://v4l2:/dev/video0
$ gst-launch-1.0 -v v4l2src device=/dev/video0 ! glimagesink
Simulate Camera
Use ffmpeg to push desktop screen recordings to the virtual camera:
ffmpeg -f x11grab -r 15 -s 1280x720 -i :0.0 -vcodec rawvideo -pix_fmt yuv420p -f v4l2 /dev/video0
Share Physical Camera
Use v4l2-ctl to output the video stream from the physical camera to the virtual device:
sudo v4l2-ctl --stream-mmap --stream-to=/dev/video1 --stream-from=/dev/video0
Now, multiple programs can simultaneously open the virtual camera device <span>/dev/video1</span> and read the video stream from the camera. Here, <span>v4l2-ctl</span> is provided by the <span>v4l-utils</span> toolkit, where <span>video0</span> is the physical camera, and <span>video1</span> is the virtual camera, which should be adjusted according to the situation and needs.
Use ffmpeg to copy the video stream from the physical camera to the virtual device:
ffmpeg -f video4linux2 -s 640x480 -r 30 -i /dev/video0 -vcodec copy -f v4l2 /dev/video10 -vcodec copy -f v4l2 /dev/video11
Here, <span>video0</span> is the physical camera, and <span>video10</span> and <span>video11</span> are the virtual cameras created.
Dynamic Device Management
Add a new virtual device:
sudo v4l2loopback-ctl add -n "Virtual Camera" /dev/video2
Dynamically delete a virtual device:
sudo v4l2loopback-ctl delete /dev/video2
This requires using the <span>v4l2loopback-ctl</span> tool, which can be installed directly via the <span>v4l2loopback-utils</span> toolkit, or compiled manually from the <span>v4l2loopback</span> source code:<span>cd utils && make && sudo make install</span>.
More Usage
For more usage examples, please refer to the official wiki:https://github.com/v4l2loopback/v4l2loopback/wiki
Notes
- • If the system has Secure Boot enabled, you may need to disable Secure Boot or sign the module.
- • Ensure that the kernel version is compatible with the module to avoid loading failures.
- • You can set the automatic loading and default options for the module through the configuration files in
<span>/etc/modules-load.d/</span><code><span> and </span><code><span>/etc/modprobe.d/</span><code><span>.</span>
Conclusion
v4l2loopback is a powerful tool suitable for scenarios requiring virtual cameras or video stream sharing, especially useful in video conferencing, testing, and development.
Welcome to Follow:
Feel free to add WeChat to join the group for discussions:

For more, please click the bottom left corner Read the original text!