Linux (v4l) webcam - make settings "stick"

8,527

Solution 1

First the bad news: There is currently no built-in solution to do that in any distro I know.

The good news is that such a solution is very easy to implement and can be adapted for many similar problems: we only need to use a bit of udev and bash.

First, we need a programatic (script-friendly) way to set these settings. My favorite is v4l2ctrl, as it can save V4L2 configurations and then restore them on demand. For example:

  1. First change your camera settings with any program you like. I really recommend qv4l2 for that, it's awesome.
  2. Then, save the camera settings to a file. (-d /dev/video0 is optional, but useful if you have more than one camera.)

    v4l2ctrl -d /dev/video0 -s camera-settings.txt
    
  3. If you unplug and plug again the settings will be lost, but now we can recover them:

    v4l2ctrl -d /dev/video0 -l camera-settings.txt
    

Once you get it working like that, it's time to automate it with udev. For this we will create a rule file of our liking that matches the camera. The file can be called /etc/udev/rules.d/99-v4l2-default-settings.rules and contain something like this:

SUBSYSTEM=="video4linux", ATTR{name}=="HD Pro Webcam C920", RUN+="/etc/camera-defaults/udev-hook.sh"

The above will run as root the script /etc/camera-defaults/udev-hook.sh that we will soon create whenever a device of subsystem video4linux whose manufacturer name is "HD Pro Webcam C920" is detected or unplugged.

We could add many different filters to our rule. You can use udevadm info -a -p $(udevadm info -q path -n /dev/video0) to get all possible variables for the device, as well as for its parent devices.

Finally, create the script. Here is something simple that will work. Give it execution permissions!

#!/bin/bash
set -eu

# Useful for debugging, if you want to check what variables are available:
# set > /tmp/vars

if [ "$ACTION" == "add" ]; then
  v4l2ctrl -d "$DEVNAME" -l "/etc/camera-defaults/camera-settings.txt"
fi

Move camera-settings.txt to the specified directory, unplug and plug again. The settings from that file should have been restored successfully.

Any time in the future you need to save your settings permanently, use your favorite program to tweak them and then run:

v4l2ctrl -s /etc/camera-defaults/camera-settings.txt

Solution 2

I fixed a similar problem following these steps (adapted from source):

$ sudo apt-get install mercurial
$ hg clone http://linuxtv.org/hg/v4l-dvb
$ cd v4l-dvb
$ sudo make menuconfig <-- dont change anything, just "Exit" and save changes
$ sudo emacs v4l/.config <-- change CONFIG_DVB_FIREDTV=m to CONFIG_DVB_FIREDTV=n
$ make
$ sudo make install
$ v4l2ucp <-- Auto Gain off
Share:
8,527

Related videos on Youtube

petr
Author by

petr

Updated on September 18, 2022

Comments

  • petr
    petr over 1 year

    My Logitech camera (C300) is properly detected and present at /dev/video0. However, by default the image has contrast and saturation set to maximum, which significantly degrades the image quality. I can change the settings using guvcview but after quitting the settings are back to normal (I am using mplayer to capture frames). How can I make the changes I make in guvcview permanent?

    EDIT:

    It looks like the guvcview's settings are indeed "sticky" - if I open it again, the image stays the same. So it is something the other programs are doing to the initialisation of the webcam (same problem on mplayer and vlc)

  • Antonio Ospite
    Antonio Ospite over 5 years
    I consolidated this solution and published it in a git repository: git.ao2.it/v4l2-persistent-settings.git I also tried to ask on linux-media to see what v4l developers think about it: spinics.net/lists/linux-media/msg143521.html