How to Change Webcam Settings in FFmpeg via V4L2

Adjusting webcam parameters like exposure, brightness, and contrast is essential for achieving optimal video quality during recording or live streaming. While FFmpeg is used to capture and encode the video stream, hardware-level webcam adjustments on Linux are handled via Video4Linux2 (V4L2) controls. This guide shows you how to query your camera’s supported parameters and apply changes using the v4l2-ctl utility in tandem with your FFmpeg command.

Step 1: Install V4L2 Utilities

To modify hardware settings, you need the v4l-utils package, which contains the v4l2-ctl tool. Install it using your distribution’s package manager:

sudo apt update && sudo apt install v4l-utils

Step 2: List Available Webcam Controls

Webcams support different ranges of brightness, exposure, and focus. To find the specific parameter names and value ranges for your device (usually /dev/video0), run:

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

This will output a list of controls, their minimum/maximum values, default values, and current settings:

brightness 0x00980900 (int)    : min=0 max=255 step=1 default=128 value=128
exposure_auto 0x009a0901 (menu)   : min=0 max=3 default=3 value=3
exposure_absolute 0x009a0902 (int)    : min=3 max=2047 step=1 default=250 value=250

Step 3: Change Settings Using v4l2-ctl

Before or during your FFmpeg stream, you can set the hardware parameters.

To change the brightness:

v4l2-ctl -d /dev/video0 --set-ctrl=brightness=150

To control exposure manually, you must first disable auto-exposure (usually by setting exposure_auto to 1) and then set the absolute exposure value:

v4l2-ctl -d /dev/video0 --set-ctrl=exposure_auto=1
v4l2-ctl -d /dev/video0 --set-ctrl=exposure_absolute=150

Step 4: Run the FFmpeg Command

Once the settings are applied to the device, they remain active in the hardware. You can launch FFmpeg to capture the video stream with your custom settings applied:

ffmpeg -f v4l2 -input_format mjpeg -video_size 1920x1080 -framerate 30 -i /dev/video0 output.mp4

One-Line Command execution

You can chain the V4L2 configuration and FFmpeg commands together so they execute sequentially in a single terminal line:

v4l2-ctl -d /dev/video0 --set-ctrl=brightness=140 --set-ctrl=exposure_auto=1 --set-ctrl=exposure_absolute=200 && ffmpeg -f v4l2 -i /dev/video0 output.mp4