NBMrec/src/nbmrec.sh

86 lines
1.9 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# NBMrec
# nocturnal bird migration recorder daemon
# by Samuel ORTION
RECORD_DIR="/mnt/DATA/media/records"
RECORD_DATE=$(date +"%Y-%m-%d")
PREFIX="nbmrec"
RECORD_FILENAME="${PREFIX}_$(date +"%Y%m%d_%H%M%S").wav"
RECORD_FILEPATH="${RECORD_DIR}/${RECORD_DATE}/${RECORD_FILENAME}" 
START_TIME="22:00"
END_TIME="06:00"
START_TIMESTAMP=$(date -d "$START_TIME" "+%s")
END_TIMESTAMP=$(date -d "$END_TIME + 1 day" "+%s")
RECORD_DURATION=$(( $START_TIMESTAMP - $END_TIMESTAMP ))
VERBOSE=false
# Load conf file if any
if [ -f /usr/local/etc/nbmrec/nbmrec.conf ]; then
source /usr/local/etc/nbmrec/nbmrec.conf
fi
RECORD_DURATION=$(( $(date -d "$END_TIME + 1 day" "+%s") - $(date -d "$START_TIME" "+%s") ))
usage() {
echo "Usage: $0 [ -d <duration> ] [ -f <filename> ]"
echo " -d <duration> : duration of the recording in seconds or 'night' for all night recording"
echo " -f <filename> : filename of the recording"
echo " -h : display this help"
echo " -v : switch to VERBOSE mode"
}
# Get command line options
while getopts ":hd:f:" opt; do
case $opt in
h)
usage
;;
d)
RECORD_DURATION=$OPTARG
if [ "$RECORD_DURATION" == "night" ]; then
RECORD_DURATION=32400
fi
;;
f)
RECORD_FILEPATH=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
main() {
# Create output folder
if $VERBOSE
then
echo "Creating record output folder"
fi
mkdir -p "$RECORD_DIR/$RECORD_DATE/"
# Perform recording
if $VERBOSE
then
echo "Recording $RECORD_FILENAME"
fi
while [[ $(date "+%s") -le $END_TIMESTAMP ]]; do
rec -b 16 -r 48000 -c 1 -t w64 $RECORD_FILEPATH trim 0 $RECORD_DURATION > /tmp/nbmrec-rec.log
done
if $VERBOSE
then
echo "Done"
fi
}
main
exit 0