How can I change my webcam's power line frequency setting?

7,003

Solution 1

Use the control utility for the Linux UVC driver

(This is a variant on @telcoM's answer)

(USB) webcams are controlled by the Linux USB Video Client driver. Their device files are /dev/video0, /dev/video1 etc.

One of the driver's setting is the power line frequency, which (typically) has 3 options:

  • 0 for Disabled, i.e. the driver won't account for the voltage cycle at all.
  • 1 for 50 Hz (most of the world)
  • 2 for 60 Hz (US and some others)

There are two alternative control utilities for this driver available on Debian systems. One is named v4l-ctl, covered in telcoM's answer, and the other is named uvcdynctrl; it is installed with the eponymous package, i.e. you need to run the following (as root or via sudo):

apt install uvcdynctrl

When the package is installed, do the following (not necessarily as a root user - it depends on the permissions of your camera's device file):

  1. List the available devices to make sure you see your camera:

    uvcdynctrl -l
    

    The output should look something like this:

    Listing available devices:
    video0   UVC Camera (046d:0825)
    Media controller device: /dev/media0
    Entity 1: UVC Camera (046d:0825). Type: 65537, Revision: 0, Flags: 1, Group-id: 0, Pads: 1, Links: 0
      Device node
      Entity: 1, Pad 0, Flags: 1
    video1   UVC Camera (046d:0825)
    Media controller device /dev/media1 doesn't exist
    
  2. Let's assume your device is /dev/media0:
    webcam_device="video0"
    
  3. Get the current power line frequency setting:
    uvcdynctrl -d${webcam_device} "--get=Power Line Frequency"
    
    This time, the output should be just a single number. In your case it will likely be 2, which corresponds to a frequency of 60 Hz.
  4. Set a new power line frequency:
    uvcdynctrl -d${webcam_device} "--set=Power Line Frequency" 1
    

If you know your device name, only the fourth command is necessary, of course.

Persisting your setting

As @telcoM notes, however, this is not a persistent setting. To make it persistent, you need to add a udev rule, to be executed when the camera is connected.

Create a file named /etc/udev/rules.d/81-uvcvideo.rules, with contents:

# Set power line frequency to 50 Hz
ACTION=="add", SUBSYSTEM=="video4linux", DRIVERS=="uvcvideo", RUN+="/usr /bin/uvcdynctrl -d$attr{name} --set=Power\\ Line\\ Frequency 1"

Notes:

  • $attr{name} should hold the new attached device's name, e.g. video0, video1 etc.
  • Theoretically, one could make this setting globally, irrespective of the device used. But - who knows? Maybe one of your cameras is pointed to a naturally-lit room where no powerline frequency correction is necessary.
  • The above rule has not been tested (!)

Solution 2

On the command line, you can set the uvcvideo driver's power line frequency setting to the 50 Hz value with:

v4l2-ctl --set-ctrl=power_line_frequency=1

If your webcam is not /dev/video0, add a -d /dev/videoN option with the correct number. The v4l2-ctl command comes in package v4l-utils, at least on Debian and related distributions.

Also, v4l2-ctl -L will display a list of settings available in your webcam. It will also describe the available choices for settings like power line frequency. Your webcam may have a list of available settings that is different from mine.

To make the power line frequency setting persistent, you might want to make an udev rule of it. To do that, create a file named /etc/udev/rules.d/81-uvcvideo.ruleswith the following contents:

# Set power line frequency to European
ACTION=="add", SUBSYSTEM=="video4linux", DRIVERS=="uvcvideo", RUN+="/usr/bin/v4l2-ctl --set-ctrl=power_line_frequency=1"

Solution 3

If the app you're using does not allow you to make the relevant settings, and there's no "settings applet", use an app which can make these settings.

A prominent example would be guvcview - the GNU UVC viewer program. It's a utility for capturing video from devices using the Linux USB Video Class driver (UVC). When you start it up, you'll find the power line frequency setting:

enter image description here

and now you can change the US-centric 60 Hz into the more common 50 Hz.

Notes:

  • GUVCView may fail to start if another app is actively using the camera.
  • Setting the frequency this way does not persist with system reboots.
Share:
7,003

Related videos on Youtube

einpoklum
Author by

einpoklum

Made my way from the Olympus of Complexity Theory, Probabilistic Combinatorics and Property Testing to the down-to-earth domain of Heterogeneous and GPU Computing, and now I'm hoping to bring the gospel of GPU and massive-regularized parallelism to DBMS architectures. I've post-doc'ed at the DB architecture group in CWI Amsterdam to do (some of) that. I subscribe to most of Michael Richter's critique of StackOverflow; you might want to take the time to read it. If you listen closely you can hear me muttering "Why am I not socratic again already?"

Updated on September 18, 2022

Comments

  • einpoklum
    einpoklum almost 2 years

    I'm using a Logitech C720 webcam with my PC, which runs Devuan Beowulf GNU/Linux (~= Debian 10 Buster but without systemd).

    In a related, but not Linux-specific, question on SuperUser, it turns out that I need to change my webcam's power line frequency setting.

    However - I have no idea how to do that. My desktop environment, Cinnamon, does not have an item in the "System Settings" dialog for it. How do I make this setting, then?

  • einpoklum
    einpoklum about 4 years
    There is no v4l2-ctl, nor does there seem to be a package offering it.
  • telcoM
    telcoM about 4 years
    @einpoklum I saw your comment edits and tried to incorporate them all to my answer; how about now?
  • einpoklum
    einpoklum about 4 years
    I guess that's better, but - this just doesn't work on Devuan/Debian. Maybe this utility is found on other distributions? Or older versions of Debian/Devuan? The approach is sound, of course; see my new answer!
  • telcoM
    telcoM about 4 years
    @einpoklum Interesting: my answer is based on what I have on my Debian 10 system, and it certainly works here! But you might have a different webcam with differently-named settings (I believe at least some of the names come from the webcam firmware, although I might be wrong there). Are you sure you have the v4l-utils package installed?
  • einpoklum
    einpoklum about 4 years
    I don't. It is not available. I wonder if this isn't something that systemd has taken over in Debian...
  • einpoklum
    einpoklum about 4 years
    Ok, so, it seems that this is due to an apt bustage on my own system. v4l-ctl is available on Devuan and works fine (but not for me)... so, accepting your answer.
  • einpoklum
    einpoklum about 4 years
    Although - your link regards Devuan 2 ASCII, not Devuan 3 Beowulf.
  • telcoM
    telcoM about 4 years
    For Beowulf: deb.devuan.org/merged/dists/3.0/main/binary-amd64/Packages.x‌​z says Filename: pool/DEBIAN/main/v/v4l-utils/v4l-utils_1.16.3-3_amd64.deb so it does exist on Beowulf too, apparently still as a straight copy of the respective Debian package.
  • Thomas Guyot-Sionnest
    Thomas Guyot-Sionnest over 3 years
    If you have more than one webcam you may want to specify the device you're changing settings for. There are many ways to select devices in udev so I'll just show one example; the rest is well documented in udev manpage (man 7 udev). My Logitech C930e is configured with this rule: SUBSYSTEM=="video4linux", KERNEL=="video[0-9]*", ACTION=="add", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="0843", RUN+="/usr/bin/v4l2-ctl -d %N --set-ctrl zoom_absolute=140". You can print the required device infos with udevadm info /dev/video0 - change the dev path to other webcams you have as needed.
  • Raphael
    Raphael over 3 years
    Unfortunately, these settings are not persistent. :/ It's not like the power line frequency changed between webcams, or when re-plugging one, or ... ever, really.
  • einpoklum
    einpoklum over 3 years
    @Raphael: Indeed... and this is addressed in my other answer :-)