Skip to content

Instantly share code, notes, and snippets.

@mikeflynn
Last active December 28, 2016 21:08
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 mikeflynn/a398469a9850b14d7af2edfc2d571f55 to your computer and use it in GitHub Desktop.
Save mikeflynn/a398469a9850b14d7af2edfc2d571f55 to your computer and use it in GitHub Desktop.
A bash script to download video from Foscam cameras.
#!/usr/bin/env bash
# ./record.sh 192.168.77.51:8051 xxxxxx yyyyyy > /dev/null 2>&1
# Check input.
if [ -z "$1" ]; then
echo "Missing camera hostname."
exit 1
fi
if [ -z "$2" ]; then
echo "Missing camera username."
exit 1
fi
if [ -z "$3" ]; then
echo "Missing camera password."
exit 1
fi
if [ -z "$4" ]; then
DATA_DIR="."
else
DATA_DIR=$4
fi
IFS=':' read -ra ADDR <<< "$1"
HOST=${ADDR[0]}
mkdir -p "/Users/$(whoami)/.foscam/"
IS_RUNNING="/Users/$(whoami)/.foscam/$HOST"
# Check if script is already running.
if [ -e $IS_RUNNING ]; then
echo "Already running."
exit 0
fi
echo "..." > $IS_RUNNING
# Clean up the ffmpeg process on exit.
function cleanup {
pkill -9 ffmpeg
rm -f -- $IS_RUNNING
}
trap cleanup EXIT
# If HD, prime the mjpeg stream
if ! [ -z "$5" ]; then
curl -s "http://$1/cgi-bin/CGIProxy.fcgi?cmd=setSubStreamFormat&format=1&usr=$2&pwd=$3" > /dev/null
fi
# Run download in 5 min segements.
if [ -z "$5" ]; then
# No 5th param; Not HD
STREAM="http://$1/videostream.cgi?user=$2&pwd=$3&resolution=32&rate=13"
else
# HD camera stream
STREAM="http://$1/cgi-bin/CGIStream.cgi?cmd=GetMJStream&usr=$2&pwd=$3"
fi
FFMPEG="/usr/local/bin/ffmpeg"
if ! type $FFMPEG > /dev/null; then
echo "ffmpeg is required but it's not installed in the expected location. Aborting."
exit 1
fi
while :
do
NOW=$(date +"%Y-%m-%d_%H-%M")
$FFMPEG -f mjpeg -r 5 -i $STREAM -r 5 $DATA_DIR/video_${HOST}_$NOW.avi & PID=$!
sleep 300
kill -HUP $PID
done
#!/usr/bin/env bash
# bash <(curl -s https://gist.githubusercontent.com/mikeflynn/a398469a9850b14d7af2edfc2d571f55/raw/record_install.sh)
TEMPCRON=/tmp/usercron
# Install Homebrew
if ! type "brew" > /dev/null; then
echo "Homebrew not found; Installing..."
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
# Install ffmpeg
if ! type "ffmpeg" > /dev/null; then
echo "ffmpeg not found; Installing..."
/usr/local/bin/brew install ffmpeg
fi
# Ask for data directory
read -p "Where will the script and video files be stored? (default: $PWD/foscam) " DATADIR
if [ -z $DATADIR ]; then
DATADIR=${PWD}/foscam
fi
# Make directory
mkdir -p $DATADIR/data
# Download record.sh script
curl -o $DATADIR/record.sh "https://gist.githubusercontent.com/mikeflynn/a398469a9850b14d7af2edfc2d571f55/raw/record.sh"
chmod 755 $DATADIR/record.sh
# Dump the user's cron
crontab -l > $TEMPCRON
echo "####### Added by Foscam Record Installer #######" >> $TEMPCRON
# Ask for camera IP, user, password
CAM_HOST=""
while :
do
read -p 'Camera URL (enter blank when all cameras are entered): ' CAM_HOST
if [ -z $CAM_HOST ]; then
break
fi
read -p 'Camera User: ' CAM_USER
printf "Camera Password: "
read -s CAM_PASS
printf "\n"
read -p 'HD Camera (y/n)?' CAM_HD
# Add cron entry
if [ "$CAM_HD" == "y" ]; then
echo "* * * * * $DATADIR/record.sh $CAM_HOST $CAM_USER $CAM_PASS $DATADIR/data true > /dev/null 2>&1" >> $TEMPCRON
else
echo "* * * * * $DATADIR/record.sh $CAM_HOST $CAM_USER $CAM_PASS $DATADIR/data > /dev/null 2>&1" >> $TEMPCRON
fi
done
# Add the cleanup cron job
echo "0 0 * * 0 find $DATADIR/data -type f -mtime +7 -delete" >> $TEMPCRON
# Save the cron
crontab $TEMPCRON
echo "Setup complete."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment