Skip to content

Instantly share code, notes, and snippets.

@sxiii
Last active February 19, 2022 18:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sxiii/4898a6fff28a0fac3b036effe2f937c7 to your computer and use it in GitHub Desktop.
Save sxiii/4898a6fff28a0fac3b036effe2f937c7 to your computer and use it in GitHub Desktop.
Linux - v4l2-ctl - Load Webcam Settings From Text File (saved by camset)
#!/usr/bin/python
# Back story
# v4l2-cli by itself does not accept config file
# camset allows us to save setting to text file, but it requires GUI to load it, which is inconvinient
# What I've done
# So I've written script that allows us to load settings from the config file, created with camset earlier
# Code is based on Camset (https://github.com/azeam/camset/)
# Run like: `python cliread.py myconfig.txt` (using myconfig.txt saved by camset)
# Change card to fit your video device
import subprocess
import sys
card = '/dev/video2'
# This part loads settings from file and applies them
def load_settings_from_file(filename):
try:
f = open(filename, "r")
lines = f.readlines()
for line in lines:
splits = line.split("=")
setting = splits[0]
value = splits[1]
#if (setting == "resolution_index"):
# set_active_resolution(int(value))
# continue
subprocess.run(['v4l2-ctl', '-d', card, '-c', '{0}={1}'.format(setting, value)], check=False, universal_newlines=True)
f.close()
print("Settings file {0} successfully loaded".format(filename))
except Exception as e:
print("Unable to load file {0} - {1}".format(filename, e))
return
load_settings_from_file(sys.argv[1])
# This part applies 1280x720 resolution and MJPG format
# For list of your supported formats please run `v4l2-ctl -V`
subprocess.run(['v4l2-ctl -d ', card, ' -v width=1280,height=720,pixelformat=MJPG'], shell=True)
# Config sample (myconfig.txt file contents (remove #s if you want to use it)
#brightness=128
#contrast=128
#saturation=111
#white_balance_temperature_auto=0
#gain=50
#power_line_frequency=0
#white_balance_temperature=3500
#sharpness=140
#backlight_compensation=0
#exposure_auto=1
#exposure_absolute=300
#exposure_auto_priority=0
#pan_absolute=0
#tilt_absolute=0
#focus_absolute=0
#focus_auto=0
#zoom_absolute=100
#resolution_index=33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment