V4L: A Complete Practical Tutorial

Deepesh Deepak
4 min readMay 7, 2023

--

In this article, I will be discussing V4L (short for Video4Linux), a kernel interface that provides access to video devices such as webcams, streaming media, and other video capture devices in Linux-based systems. It is the standard API for video capture and output on Linux platforms.

Image generated by AI

This article is part of the series Multimedia Frameworks and Utilities where I am discussing V4L, FFMPEG, and Gstreamer.

The article is organized into the following sections:
1. Introduction to V4L
2. V4L Commands to interact with video devices

1. Introduction to V4L

Video4Linux is a framework that provides an API for handling video devices in the Linux kernel. It allows applications to interact with video devices such as webcams, TV tuners, and capture cards and provides a standard interface to capture and manipulate video frames from these devices.

V4L has been around for many years and has evolved through various versions, with the current version being V4L2. Since V4L2 is a kernel-level interface, it comes pre-installed in most modern Linux distributions.

V4L2 provides a set of tools and utilities that make it easy to configure and test video devices. It also provides support for various video formats, including MPEG-2, MPEG-4, and H.264. It also provides a set of APIs for applications that need to control video devices. These APIs allow applications to set video parameters such as resolution, frame rate, and color format.

Gstreamer, OpenCV, and FFMPEG all provide support to V4L2. They have V4L2 plugins or API functions that allow them to capture and process video from V4L2-compatible devices.

2. V4L Commands to interact with video devices

Now that we have a basic understanding of V4L, let us dive into some of the important commands and learn about what they do. There are several packages that provide user-level utilities and libraries for V4L2. One of them is:

  • v4l-utils: A collection of command-line utilities that allow you to control V4L2 devices and capture video streams. To install it use:
    sudo apt install v4l-utils.
    It providesv4l2-ctlwhich is an application to control V4L drivers.

v4l2-ctl: This command is used to control V4L2 devices from the command line. It allows you to query device capabilities, set device parameters, and capture video and still images. Some common options include:

  • --list-devices: Lists all available V4L2 devices on the system.
  • -d, --device <device>: Specifies the device to operate on.
  • -l, --list-ctrls: Lists all available controls for the device.
  • -c, --set-ctrl <control>=<value>: Sets the value of a control.

Now let us see some of the full commands in practice:

  • v4l2-ctl --version: Checks the V4L2 version.
v4l2-ctl 1.22.1
  • v4l2-ctl --list-devices: Lists all V4L2 devices available on the system.
USB2.0 HD UVC WebCam: USB2.0 HD (usb-0000:00:14.0-7):
/dev/video0
  • v4l2-ctl -V: This command is used to display the current video format of a V4L2 device. The -V option is short for— get-fmt-video, which gets the current video format for the selected device.
Format Video Capture:
Width/Height : 640/480
Pixel Format : 'MJPG' (Motion-JPEG)
Field : None
Bytes per Line : 0
Size Image : 614989
Colorspace : Default
Transfer Function : Default (maps to Rec. 709)
YCbCr/HSV Encoding: Default (maps to ITU-R 601)
Quantization : Default (maps to Full Range)
Flags :
  • v4l2-ctl --list-formats: Lists all supported video formats and frame sizes for a given device.
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture

[0]: 'MJPG' (Motion-JPEG, compressed)
[1]: 'YUYV' (YUYV 4:2:2)

The --list-formats command shows only the basic information about each format, which may not be sufficient to determine the exact video parameters needed for a particular use case.

  • v4l2-ctl --list-formats-ext: This command also lists all supported video formats and frame sizes for a given device but provides more detailed information about each video format, including the exact frame size, the image cropping and scaling parameters, and the frame interval. It displays the same information as --list-formats, but with additional details:
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture

[0]: 'MJPG' (Motion-JPEG, compressed)
Size: Discrete 1280x720
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 800x600
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 352x288
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 320x240
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 176x144
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 160x120
Interval: Discrete 0.033s (30.000 fps)
[1]: 'YUYV' (YUYV 4:2:2)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 800x600
Interval: Discrete 0.050s (20.000 fps)
Size: Discrete 352x288
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 320x240
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 176x144
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 160x120
Interval: Discrete 0.033s (30.000 fps)
  • v4l2-ctl -d /dev/video0 --set-ctrl brightness=128: Sets the brightness value to 128 on the video0 device.

To learn more about it, you can enter see the manual by entering the command: man v4l2-ctl info v4l2-ctl v4l2-ctl --help

This brings us to the end of this short tutorial. If any queries or questions do post them here in the comments. In the next article in this series, I will be discussing FFMPEG and later on Gstreamer.

--

--

Deepesh Deepak

Riding the wave of AI, ML, DL, CV, RL, Drones & Robotics. Let's automate the repetitive tasks so that the humanity can march towards higher form of intelligence