Skip to content

Instantly share code, notes, and snippets.

@rkmax
Created December 1, 2022 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkmax/3fc2a0bc716e497933945a1156c7cb24 to your computer and use it in GitHub Desktop.
Save rkmax/3fc2a0bc716e497933945a1156c7cb24 to your computer and use it in GitHub Desktop.
this script uses v4l2-ctl to set the zoom level of a webcam
#!/usr/bin/env bash
#
# this script uses v4l2-ctl to set the zoom level of a webcam
#
set -e
# you may have multiple cameras but usual video0 is the right one
camera=/dev/video0
# you may need to tweak these values
# v4l2-ctl --list-ctrls-menus may return values between 1 and 5
# or values between 100 and 500 like in my case, but
# the max zoom is 200
max_zoom=200
min_zoom=100
zoom_step=10
set_zoom() {
v4l2-ctl -d $camera --set-ctrl zoom_absolute="${1}"
}
get_zoom() {
v4l2-ctl -d $camera --list-ctrls-menus --list-ctrls-menus | grep zoom_absolute | grep -E -o value=\[0-9\]+
}
increase_zoom() {
# shellcheck disable=SC2004
new_zoom=$(($(get_zoom) + $zoom_step))
# shellcheck disable=SC2046
if [ $new_zoom -le $max_zoom ]; then
set_zoom $new_zoom
fi
}
decrease_zoom() {
# shellcheck disable=SC2004
new_zoom=$(($(get_zoom) - $zoom_step))
# shellcheck disable=SC2046
if [ $new_zoom -ge $min_zoom ]; then
set_zoom $new_zoom
fi
}
# if no argument
if [ -z "$1" ]; then
echo "Usage: $0 {max|min|increase|decrease}"
exit 1
fi
case "$1" in
"min")
set_zoom $min_zoom
;;
"max")
set_zoom $max_zoom
;;
"decrease")
decrease_zoom
;;
"increase")
increase_zoom
;;
*)
echo "Usage: $0 {away|close|middle}"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment