1. Overview
Sometimes, a machine’s webcam can produce horizontal or vertical flickering. This is often due to the camera shutter interacting with the power line frequency. For example, in Europe, the power line frequency is 50Hz, while in America, it’s 60Hz. So, typically, to avoid flickering, the camera shutter setting should be set up for 50Hz in Europe, and for 60Hz in America.
In this tutorial, we’ll learn how to change the webcam power line frequency settings using the Linux command line interface (CLI). Initially, we’ll look at the v4l2-ctl command. Then, we’ll learn how to use the uvcdynctrl command. Finally, we’ll see how to make these settings persistent.
2. Using v4l2-ctl
Video4Linux2 (usually referred to as v4l2) is a standard Linux tool to interact with video devices. Let’s use it to update the settings of our webcam.
To begin with, we can check the current power line frequency setting of the webcam. For that, we use the v4l2-ctl command with the –get-ctrl option:
$ v4l2-ctl --get-ctrl=power_line_frequency
power_line_frequency: 2
We can see that our current setting is 2, which stands for 60Hz.
Overall, the power line frequency has three options:
- 0: disabled
- 1: 50 Hz
- 2: 60 Hz
Now that we know our current webcam power line frequency setting, let’s update it to 50Hz. For that, we use the –set-ctrl option, and the setting 1 which stands for 50Hz:
$ v4l2-ctl --set-ctrl=power_line_frequency=1
If we see no errors, the setting should be updated correctly.
In case we have more than one webcam connected, we may need to specify the correct video device using the -d option:
$ v4l2-ctl -d /dev/video0 --set-ctrl=power_line_frequency=1
Here, we’ve specified that our webcam is using the /dev/video0 device file.
To confirm that we updated correctly, we can use –get-ctrl again:
$ v4l2-ctl --get-ctrl=power_line_frequency
power_line_frequency: 1
As we can see, the power line frequency is now 1, which means that we’ve updated the setting to 50Hz correctly.
3. Using uvcdynctrl
The webcam settings can also be controlled by the uvcdynctrl utility.
To install the tool, we can use apt:
$ sudo apt install uvcdynctrl
Once installed, let’s get the current power line setting using the uvcdynctrl command:
$ uvcdynctrl -d "video0" "--get=Power Line Frequency"
2
Here, we’ve used the -d option to specify the video device we’re using, while –get=Power Line Frequency extracts the current setting.
In this case, that’s 2, which stands for 60Hz.
Let’s now change it to 50Hz. For that, we use a similar command, but we replace –get with –set, and add the desired setting at the end:
$ uvcdynctrl -d "video0" "--set=Power Line Frequency" 1
If no error occurred, the setting should now be updated.
Let’s verify our configuration is now correct:
$ uvcdynctrl -d "video0" "--get=Power Line Frequency"
1
As expected, the power line frequency setting is now 1. So, we’ve updated the setting to 50Hz correctly.
4. Persist Power Line Frequency Settings
The above commands can change the webcam power line frequency, but the setting won’t be saved after the system reboots.
To make it persistent, we can add a udev rule file named /etc/udev/rules.d/81-uvcvideo.rules. The file content depends on the command we use.
If we use the v4l2-ctl command, the udev rule file content reflects that:
# Set power line frequency to 50 Hz
ACTION=="add", SUBSYSTEM=="video4linux", DRIVERS=="uvcvideo", RUN+="/usr/bin/v4l2-ctl -d /dev/video0 --set-ctrl=power_line_frequency=1"
As we can see, the rule uses the video4linux subsystem and the uvcvideo driver, while the RUN option has the v4l2-ctl we used above.
If we prefer to use the uvcdynctrl command, then the udev rule file content will have a different RUN section:
# Set power line frequency to 50 Hz
ACTION=="add", SUBSYSTEM=="video4linux", DRIVERS=="uvcvideo", RUN+="/usr /bin/uvcd/bin/uvcdynctrl -dvideo0 --set=Power\\ Line\\ Frequency 1"
Now, our system should automatically set the webcam to 50Hz after reboot.
5. Conclusion
In this article, we looked at how to update the power line frequency setting for the webcam using CLI.
First, we used the v4l2-ctl command for that. After that, we learned the uvcdynctrl command as an alternative to v4l2-ctl. Finally, we made our setting persistent by adding the udev rule.