Skip to main content

6-3 Camera Control Using Video4Linux

This guide provides an overview of how to control cameras using the Video4Linux2 (V4L2) interface. We’ll cover how to list camera devices, view and set supported resolutions and formats, adjust camera parameters, and capture images or videos using various tools like v4l2-ctl, ffmpeg, and OpenCV.


Introduction to v4l2-ctl

The v4l2-ctl tool is used to interact with Video4Linux devices (video, VBI, radio, or software-defined radio), supporting full control over nearly all device settings using the V4L2 API.

Reference:
https://manpages.ubuntu.com/manpages/lunar/man1/v4l2-ctl.1.html

Installation

Install the required utilities with:

sudo apt install v4l-utils

Listing Video Devices

To list all video devices and their corresponding paths:

v4l2-ctl --list-devices

Example Output:

S-YUE 8MP USB Camera: S-YUE 8MP (usb-0000:00:14.0-8):
/dev/video0
/dev/video1
/dev/media0

UVC Camera (046d:0990) (usb-0000:00:14.0-7):
/dev/video2
/dev/video3
/dev/media1

Viewing Device Information

Show All Settings:

v4l2-ctl --all

Show Driver Information:

v4l2-ctl -D

Listing Supported Formats and Resolutions

List Basic Formats:

v4l2-ctl -d /dev/video0 --list-formats

List Extended Format Info (Resolution & FPS):

v4l2-ctl -d /dev/video0 --list-formats-ext

Example Output:

[0]: 'MJPG' (Motion-JPEG, compressed)
Size: Discrete 3264x2448 @ 15.000 fps
Size: Discrete 1920x1080 @ 30.000 fps
[1]: 'YUYV' (YUYV 4:2:2)
Size: Discrete 640x480 @ 30.000 fps

Setting Camera Resolution and Format

Example:

v4l2-ctl --set-fmt-video=width=1920,height=1080,pixelformat=YUYV

Checking USB Version (2.0/3.0)

For high-resolution video, ensure the camera is connected to a USB 3.0 port:

lsusb

USB Bus Info Example:

Bus 002 = USB 3.0  
Bus 001 = USB 2.0

Capturing an Image

Capture a single frame:

v4l2-ctl --device /dev/video0 \
--set-fmt-video=width=1600,height=1200,pixelformat=MJPG \
--stream-mmap --stream-to=frame.jpg --stream-count=1

Listing and Modifying Camera Controls

List Controls:

v4l2-ctl --list-ctrls

Detailed Controls with Menus:

v4l2-ctl --list-ctrls-menus

Example Controls Output:

brightness (int)        : min=0 max=255 default=128 value=128
contrast (int) : min=0 max=255 default=32 value=32
saturation (int) : min=0 max=255 default=32 value=32
white_balance_auto (bool): default=1 value=1
gain (int) : min=0 max=255 default=0 value=0
power_line_frequency (menu): options include 50Hz / 60Hz

Change Control Parameter Example:

v4l2-ctl -c brightness=150
tip

Use a camera preview app like Cheese to observe the changes live.

Capturing Media with FFmpeg

Install FFmpeg:

sudo apt install ffmpeg

Display live camera feed:

ffplay /dev/video0

Capture a picture:

ffmpeg -f v4l2 -video_size 1600x1200 -i /dev/video0 -frames 1 out.jpg

Record a video clip:

ffmpeg -f v4l2 -framerate 30 -video_size 1600x1200 -input_format mjpeg \
-i /dev/video0 -c copy out.mkv

Press q to stop recording.

Using OpenCV to Capture Video

note

For S-YUE Camera on Renesas:

Set the pixel format to YUYV, as it defaults to MJPG which may not be compatible.

OpenCV C++ Example:

#include <opencv2/opencv.hpp>
using namespace cv;

int main() {
VideoCapture cap(0);
if (!cap.isOpened()) {
std::cout << "Cannot open camera\n";
return -1;
}

cap.set(CAP_PROP_FRAME_WIDTH, 640);
cap.set(CAP_PROP_FRAME_HEIGHT, 480);

Mat frame;
while (true) {
cap >> frame;
imshow("Camera", frame);
if (waitKey(10) == 27) break; // ESC to exit
}
return 0;
}

Conclusion

With v4l2-ctl, ffmpeg, and OpenCV, we can configure and utilize camera devices on Linux-based systems. Whether you're building embedded systems with Renesas boards or just testing a webcam on Ubuntu, these tools provide extensive flexibility and control.