~voidraven/s4

f091c19978393d108000aaeed34748e85bac15a1 — lotus 1 year, 7 months ago 5c00f37
fixed more shellcheck issues and improved comments
3 files changed, 42 insertions(+), 36 deletions(-)

M .s4/sample-config
M .s4/watch.sh
M s4ctl.sh
M .s4/sample-config => .s4/sample-config +4 -4
@@ 1,7 1,7 @@
#!/bin/sh
# how many cameras are there?
S4_CAMERAS=1
# 640x480, 1280x720, 1920x1080
S4_REZ=1280x720
S4_CAMERAS="1"
# 320x240, 640x480, 1280x720, 1920x1080
S4_REZ="1280x720"
# your networked storage
S4_STORAGE=/mnt/nas-store/vids
S4_STORAGE="/mnt/nas-store/vids"

M .s4/watch.sh => .s4/watch.sh +16 -13
@@ 5,37 5,38 @@

# ------ Begin Global vars --------------------------------------
# ID is the camera number (0-n)
ID=XXX
ID="XXX"

# We expect /dev/video0 to be the webcam
WEBCAM="/dev/video0"

# Example valid resolutions: 640x480, 1280x720, or 1920x1080
REZ=YYY
REZ="YYY"

# Your networked storage location
STORAGE=ZZZ
STORAGE="ZZZ"

# Framerate you want to record at
# 30 or 60 depending on what your cameras support
FRAMERATE=WWW
FRAMERATE="WWW"

# Where the local currently recording video is residing.
# When the video is done recording, we copy it over
CACHE_DIR="$HOME/.s4/cache"

# log file location
S4_LOG_DIR="$HOME/.s4/s4.log"
#S4_LOG_DIR="$HOME/.s4/s4.log"

# FFmpeg arguments:
FF_ARG_STD="-hide_banner -nostats -loglevel fatal"
FF_ARG_LOG="-loglevel warning"
#FF_ARG_LOG="-loglevel warning"
#FF_ARG_DBG=""
FF_FORMAT="avi"

# Logging disabled by default
# since it is not implemented yet
# ( to enable change to "true" )
S4_LOG_ENABLED="NO-S4-LOGGING"
#S4_LOG_ENABLED="NO-S4-LOGGING"
# ------ End Global vars ---------------------------------




@@ 59,7 60,9 @@ fun_record () {
    fun_set_true
    L_VIDNAME=$1
    # -y flag means always overwrite
    ffmpeg -y $FF_ARG_STD -framerate $FRAMERATE -video_size $REZ -i $WEBCAM "$CACHE_DIR/$L_VIDNAME.$FF_FORMAT" &
    # We can safely ignore warning 2086 since we want to word split :)
    # shellcheck disable=SC2086
    ffmpeg -y $FF_ARG_STD -framerate $FRAMERATE -video_size $REZ -i "$WEBCAM" "$CACHE_DIR/$L_VIDNAME.$FF_FORMAT" &
}




@@ 78,8 81,8 @@ fun_stop () {

# Checks if our status files have been modified
fun_check () {
    RUNSTAT=$(cat "$STORAGE/s4/cameras/$ID/run.status")
    STOPSTAT=$(cat "$STORAGE/s4/cameras/$ID/stop.status")
    RUNSTAT=$(cat "$STORAGE"/s4/cameras/"$ID"/run.status)
    STOPSTAT=$(cat "$STORAGE"/s4/cameras/"$ID"/stop.status)
    TEN=$(date "+%M")
    TEN_CHECK=$(echo "${TEN%"${TEN#?}"}")
    HOUR_CHECK=$(date +"%H")


@@ 102,11 105,11 @@ if [ "$#" -eq 1 ] && [ "$1" = "check" ] ; then
fi


if [ -d $STORAGE/s4 ] ; then
if [ -d "$STORAGE"/s4 ] ; then
    # where is this script running from?
    THIS_PWD=$(dirname $0)
    THIS_PWD=$(dirname "$0")
    # watch the status file for changes, if it changes, re-run this script..
    LAST_STATUS=$(cat $STORAGE/s4/cameras/$ID/run.status)
    LAST_STATUS=$(cat "$STORAGE"/s4/cameras/"$ID"/run.status)

    while true ; do
	sleep 1

M s4ctl.sh => s4ctl.sh +22 -19
@@ 22,14 22,14 @@ fun_make_tree () {
    if ! [ -d "$L1_STORAGE"/s4 ] ; then
	mkdir -p "$L1_STORAGE"/s4/cameras
    fi
    while [ $L1_COUNT -lt $L1_CAMERAS ] ; do
	mkdir $L1_STORAGE/s4/cameras/$L1_COUNT
    while [ "$L1_COUNT" -lt "$L1_CAMERAS" ] ; do
	mkdir "$L1_STORAGE"/s4/cameras/"$L1_COUNT"
	L1_COUNT=$((L1_COUNT+1))
    done
    for CAM in "$L1_STORAGE"/s4/cameras/* ; do
	echo "false" > "$CAM"/run.status
	echo "true" > "$CAM"/stop.status
	cp ~/.s4/watch.sh "./watch$(basename $CAM).sh"
	cp ~/.s4/watch.sh "./watch$(basename "$CAM").sh"
    done
    unset L1_CAMERAS L1_STORAGE L1_COUNT
}


@@ 40,7 40,7 @@ fun_make_tree () {
# 1: number of cameras (1-99)
# 2: video recording resolution (640x480, 1280x720, etc)
fun_check_input () {
    L2_CAMERAS=$1
    L2_CAMERAS="$1"
    L2_REZ="$2"
    L_REZOK="FALSE"
    if [ "$L2_CAMERAS" -lt 1 ] || [ "$L2_CAMERAS" -gt 99 ] ; then


@@ 72,14 72,15 @@ fun_check_input () {
# 3: root video storage location
# 4: video recording framerate (60 for 60fps)
fun_edit_templates () {
    L3_CAMERAS=$1
    L3_REZ=$2
    L3_CAMERAS="$1"
    L3_REZ="$2"
    # this one liner escapes the '/' in the path, i.e. /mnt/nas becomes \/mnt\/nas
    L3_STORAGE=$(echo $3 | sed 's/\//\\\//g')
    L3_FRAMER8=$4
    L3_STORAGE=$(echo "$3" | sed 's/\//\\\//g')
    L3_FRAMER8="$4"
    L3_COUNT=0
    while [ $L3_COUNT -lt $L3_CAMERAS ] ; do
	printf ",s/XXX/$L3_COUNT\n,s/YYY/$L3_REZ\n,s/ZZZ/$L3_STORAGE\n,s/WWW/$L3_FRAMER8\nw\nq\n" \
    while [ "$L3_COUNT" -lt "$L3_CAMERAS" ] ; do
	printf ",s/XXX/%s\n,s/YYY/%s\n,s/ZZZ/%s\n,s/WWW/%s\nw\nq\n" \
	       "$L3_COUNT" "$L3_REZ" "$L3_STORAGE" "$L3_FRAMER8" \
	       | ed ./watch$L3_COUNT.sh > /dev/null 2>&1
	L3_COUNT=$((L3_COUNT+1))
    done


@@ 104,26 105,28 @@ fun_get_input () {
    read -r L_STORAGE
    echo "Configuring storage..."
    echo "Generating watch files..."
    if fun_check_input $L_CAMERAS $L_REZ ; then
    if fun_check_input "$L_CAMERAS" "$L_REZ" ; then
	L_WRITE=""
	if [ -s ~/.s4/config ] ; then
	    printf "> Config file already exists, did you want to overwrite it? [y/n]: "
	    read -r L_WRITE
	fi
	if [ "$L_WRITE" = "y" ] || [ "$L_WRITE" = "" ] ; then
	    echo "#!/bin/sh" > ~/.s4/config
	    echo "S4_CAMERAS=\"$L_CAMERAS\"" >> ~/.s4/config
	    echo "S4_REZ=\"$L_REZ\"" >> ~/.s4/config
	    echo "S4_STORAGE=\"$L_STORAGE\"" >> ~/.s4/config
	    {
		echo "#!/bin/sh"
		echo "S4_CAMERAS=\"$L_CAMERAS\""
		echo "S4_REZ=\"$L_REZ\""
		echo "S4_STORAGE=\"$L_STORAGE\""
	    } >> ~/.s4/config
	    # check if s4 folder exists in user provided path
	    if ! [ -d "$L_STORAGE/s4" ] ; then
		fun_make_tree $L_CAMERAS $L_STORAGE
		fun_make_tree "$L_CAMERAS" "$L_STORAGE"
	    fi
	    fun_edit_templates $L_CAMERAS $L_REZ $L_STORAGE $L_FRAMER8
	    fun_edit_templates "$L_CAMERAS" "$L_REZ" "$L_STORAGE" "$L_FRAMER8"
	    echo "OK"
	    unset L_CAMERAS L_REZ L_STORAGE L_FRAMER8 L_WRITE
	else
	    echo "Ok, exiting.."
	    echo "Ok, exiting early.. (config preserved)"
	    unset L_CAMERAS L_REZ L_STORAGE L_FRAMER8 L_WRITE
	    exit 1
	fi


@@ 191,7 194,7 @@ fun_list () {
    # will error earlier if not present, we can safely ignore the following SC2153 warning
    # shellcheck disable=SC2153
    for CAM in "$S4_STORAGE"/s4/cameras/* ; do
	echo "Camera $(basename $CAM) - Running Status: $(cat "$CAM"/run.status)"
	echo "Camera $(basename "$CAM") - Running Status: $(cat "$CAM"/run.status)"
    done
}