Skip to content

Instantly share code, notes, and snippets.

@samdark
Created April 30, 2025 12:56
Show Gist options
  • Save samdark/a2411aa380227f41711c88acd939a339 to your computer and use it in GitHub Desktop.
Save samdark/a2411aa380227f41711c88acd939a339 to your computer and use it in GitHub Desktop.
Controlling camera gain in Linux
#!/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"
@samdark
Copy link
Author

samdark commented Apr 30, 2025

I have to adjust gain for the webcam often, so I used some bash and Gnome hotkeys to do it right from the keyboard.

  1. You have to make it executable.
  2. Gnome shortcuts should use absolute paths, no ~ is allowed.

Usage:

camera_gain.sh up
camera_gain.sh down

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment