Trick #8: Create commands to start the webcam in low & high quality

Printoid PRO and PREMIUM allow you to see what’s happening in front of your camera.

  • When you are connected to your local network, you probably want to have the best quality possible.
  • When you are connected ‘outside’ of your network, for example on the 3G/4G, you probably want to have a lower quality in order to preserve your data usage
  • Or you probably simply want to switch OFF your camera during the night

You can create three custom SSH commands in Printoid to achieve that. Read how to create custom SSH commands in Printoid first 🙂

Here are my own scripts in my Raspberry Pi.

  • The both are stored in /home/pi/scripts/ in this example
  • The script webcamDaemon is available for USB webcam and Raspi Camera

/home/pi/scripts/webcamDaemon

#!/bin/bash

MJPGSTREAMER_HOME=/home/pi/mjpg-streamer/mjpg-streamer-experimental
MJPGSTREAMER_INPUT_USB=”input_uvc.so”
MJPGSTREAMER_INPUT_RASPICAM=”input_raspicam.so”

# init configuration
camera=”auto”
camera_usb_options_low_quality=”-r 640×480 -f 10 -q 85″
camera_usb_options_high_quality=”-r 1080×720 -f 15 -q 100″
camera_raspi_options_low_quality=”-fps 10 -quality 100 -x 1080 -y 720 -rot 270″
camera_raspi_options_high_quality=”-fps 10 -quality 100 -x 1080 -y 720 -rot 270″

if [ -e “/boot/octopi.txt” ]; then
source “/boot/octopi.txt”
fi

# runs MJPG Streamer, using the provided input plugin + configuration
function runMjpgStreamer {
input=$1
pushd $MJPGSTREAMER_HOME
echo Running
./mjpg_streamer -o “output_http.so -w ./www” -i “$input”
LD_LIBRARY_PATH=. $MJPGSTREAMER_HOME
popd
}

# starts up the RasPiCam
function startRaspi {
logger “Starting Raspberry Pi camera”
case “$1” in
high)
runMjpgStreamer “$MJPGSTREAMER_INPUT_RASPICAM $camera_raspi_options_high_quality”
;;
low)
runMjpgStreamer “$MJPGSTREAMER_INPUT_RASPICAM $camera_raspi_options_low_quality”
;;
*)
echo “usage: startRaspi {high|low}”
;;
esac
}

# starts up the USB webcam
function startUsb {
logger “Starting USB webcam at $1 quality”
case “$1” in
high)
runMjpgStreamer “$MJPGSTREAMER_INPUT_USB $camera_usb_options_high_quality”
;;
low)
runMjpgStreamer “$MJPGSTREAMER_INPUT_USB $camera_usb_options_low_quality”
;;
*)
echo “usage: startUsb {high|low}”
;;
esac
}

# we need this to prevent the later calls to vcgencmd from blocking
# I have no idea why, but that’s how it is…
vcgencmd version

# echo configuration
echo camera: $camera
echo usb options LQ: $camera_usb_options_low_quality
echo usb options HQ: $camera_usb_options_high_quality
echo raspi options LQ: $camera_raspi_options_low_quality
echo raspi options LQ: $camera_raspi_options_high_quality

# keep mjpg streamer running if some camera is attached
while true; do
if [ -e “/dev/video0” ] && { [ “$camera” = “auto” ] || [ “$camera” = “usb” ] ; }; then
startUsb $1
elif [ “`vcgencmd get_camera`” = “supported=1 detected=1” ] && { [ “$camera” = “auto” ] || [ “$camera” = “raspi” ] ; }; then
startRaspi
fi

sleep 120
done

 

/home/pi/scripts/webcam

#!/bin/bash
# Start / stop streamer daemon

case “$1” in
start)
/home/pi/scripts/webcamDaemon $2 >/dev/null 2>&1 &
echo “$0: started with quality $2”
;;
stop)
pkill -x webcamDaemon
pkill -x mjpg_streamer
echo “$0: stopped”
;;
*)
echo “Usage: $0 {start|stop} {low|high}” >&2
;;
esac

 

How to use these scripts?

  • To start your streaming in low quality (LQ): /home/pi/scripts/webcam start low
  • To start your streaming in high quality (HQ): /home/pi/scripts/webcam start high
  • To stop your streaming: /home/pi/scripts/webcam stop

Finally in Printoid, you can do something like that:

screenshot_20170112-224627

 

  • Don’t forget to specify the full path of the script: webcam start high is not sufficient
  • If you wanna switch from LQ to HQ, you have to stop the webcam server first

 

Download links

webcam:

webcamDaemon:


One thought on “Trick #8: Create commands to start the webcam in low & high quality

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s