Created
April 30, 2025 12:56
-
-
Save samdark/a2411aa380227f41711c88acd939a339 to your computer and use it in GitHub Desktop.
Controlling camera gain in Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ $# -ne 1 ]; then | |
echo "Usage: $0 <direction> (up/down)" | |
exit 1 | |
fi | |
direction=$(echo "$1" | tr '[:upper:]' '[:lower:]') | |
current_gain=$(v4l2-ctl --get-ctrl=gain 2>/dev/null | grep -oP '[0-9]+$') | |
if [[ "$direction" == "up" ]]; then | |
max_gain=$(v4l2-ctl --list-ctrls | grep 'gain' | sed -n 's/.*max=\([0-9]*\).*/\1/p') | |
new_gain=$((current_gain + 1)) | |
if [ "$new_gain" -gt "$max_gain" ]; then | |
exit 0 | |
fi | |
elif [[ "$direction" == "down" ]]; then | |
min_gain=$(v4l2-ctl --list-ctrls | grep 'gain' | sed -n 's/.*min=\([0-9]*\).*/\1/p') | |
new_gain=$((current_gain - 1)) | |
if [ "$new_gain" -lt "$min_gain" ]; then | |
exit 0 | |
fi | |
else | |
echo "Invalid direction. Use 'up' or 'down'." | |
exit 2 | |
fi | |
v4l2-ctl --set-ctrl=gain="$new_gain" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have to adjust gain for the webcam often, so I used some bash and Gnome hotkeys to do it right from the keyboard.
~
is allowed.Usage: