From cf0de2d4bc2345cab4dba6d1de4e642b64d664b8 Mon Sep 17 00:00:00 2001 From: Samuel ORTION Date: Sat, 20 Aug 2022 08:33:54 +0200 Subject: [PATCH] record: Add RTSP support (to be continued) --- .gitignore | 7 +++--- ...yzer.conf.example => birdnet.conf.example} | 0 config/stations/station.conf.example | 3 +++ daemon/birdnet_recording.sh | 23 ++++++++++++++++--- daemon/database/scripts/database.sh | 18 +++++++++------ 5 files changed, 38 insertions(+), 13 deletions(-) rename config/{analyzer.conf.example => birdnet.conf.example} (100%) create mode 100644 config/stations/station.conf.example diff --git a/.gitignore b/.gitignore index 3ce8b9c..68306b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ var/ /.venv/ -/analyzer/ .env @@ -8,6 +7,8 @@ species_list.txt push.sh -config/*.conf - +/config/*.conf +!/config/*.conf.example +/config/stations/*.conf +!/config/stations/*.conf.example .vscode/ \ No newline at end of file diff --git a/config/analyzer.conf.example b/config/birdnet.conf.example similarity index 100% rename from config/analyzer.conf.example rename to config/birdnet.conf.example diff --git a/config/stations/station.conf.example b/config/stations/station.conf.example new file mode 100644 index 0000000..292385a --- /dev/null +++ b/config/stations/station.conf.example @@ -0,0 +1,3 @@ +# Config file for a distant recording station +RTSP_URL=rtsp://host:1000/birdnet/stream +STATION_NAME=garden \ No newline at end of file diff --git a/daemon/birdnet_recording.sh b/daemon/birdnet_recording.sh index ea90911..0a16df9 100755 --- a/daemon/birdnet_recording.sh +++ b/daemon/birdnet_recording.sh @@ -27,12 +27,20 @@ record_loop() { done } +FFMPEG_OPTIONS="-nostdin -hide_banner -loglevel error -nostats -vn -acodec pcm_s16le -ac 1 -ar 48000 " -record() { +record_stream() { + local STREAM=$1 + local DURATION=$2 + local debug "Recording from $STREAM for $DURATION seconds" + ffmpeg $FFMPEG_OPTIONS -f -i ${DEVICE} -t ${DURATION} file:${CHUNK_FOLDER}/in/birdnet_$(date "+%Y%m%d_%H%M%S").wav +} + +record_device() { DEVICE=$1 DURATION=$2 debug "Recording from $DEVICE for $DURATION seconds" - ffmpeg -nostdin -hide_banner -loglevel error -nostats -f pulse -i ${DEVICE} -t ${DURATION} -vn -acodec pcm_s16le -ac 1 -ar 48000 -af "volume=$RECORDING_AMPLIFY" file:${CHUNK_FOLDER}/in/birdnet_$(date "+%Y%m%d_%H%M%S").wav + ffmpeg $FFMPEG_OPTIONS -f pulse -i ${DEVICE} -t ${DURATION} -af "volume=$RECORDING_AMPLIFY" file:${CHUNK_FOLDER}/in/birdnet_$(date "+%Y%m%d_%H%M%S").wav } config_filepath="./config/analyzer.conf" @@ -53,4 +61,13 @@ if [[ -z $AUDIO_DEVICE ]]; then exit 1 fi -record_loop $AUDIO_DEVICE $RECORDING_DURATION \ No newline at end of file +if [[ $AUDIO_RECORDING = "true" ]]; then + record_loop $AUDIO_DEVICE $RECORDING_DURATION & +fi + +if [[ $AUDIO_STATIONS = "true" ]]; then + for station in $(ls ./config/stations/*.conf); do + source $station + record_stream $STATION_URL $RECORDING_DURATION & + done +fi \ No newline at end of file diff --git a/daemon/database/scripts/database.sh b/daemon/database/scripts/database.sh index b979b2b..b2be1f2 100755 --- a/daemon/database/scripts/database.sh +++ b/daemon/database/scripts/database.sh @@ -1,5 +1,5 @@ #! /usr/bin/env bash -# SQLite library to deal with BirdNET-stream database +# SQLite library to deal with BirdNET-stream observations database set -e @@ -10,27 +10,31 @@ source ./config/analyzer.conf DATABASE=${DATABASE:-"./var/db.sqlite"} +query() { + sqlite3 -cmd ".timeout 1000" $DATABASE "$1" +} + get_location_id() { - sqlite3 -cmd ".timeout 1000" $DATABASE "SELECT location_id FROM location WHERE latitude=$1 AND longitude=$2" + query "SELECT location_id FROM location WHERE latitude=$1 AND longitude=$2" } get_taxon_id() { - sqlite3 -cmd ".timeout 1000" $DATABASE "SELECT taxon_id FROM taxon WHERE scientific_name='$1'" + query "SELECT taxon_id FROM taxon WHERE scientific_name='$1'" } insert_taxon() { - sqlite3 -cmd ".timeout 1000" $DATABASE "INSERT INTO taxon (scientific_name, common_name) VALUES (\"$1\", \"$2\")" + query $DATABASE "INSERT INTO taxon (scientific_name, common_name) VALUES (\"$1\", \"$2\")" } insert_location() { - sqlite3 -cmd ".timeout 1000" $DATABASE "INSERT INTO location (latitude, longitude) VALUES ($1, $2)" + query "INSERT INTO location (latitude, longitude) VALUES ($1, $2)" } insert_observation() { - sqlite3 -cmd ".timeout 1000" $DATABASE "INSERT INTO observation (audio_file, start, end, taxon_id, location_id, confidence, date) VALUES ('$1', '$2', '$3', '$4', '$5', '$6', '$7')" + query "INSERT INTO observation (audio_file, start, end, taxon_id, location_id, confidence, date) VALUES ('$1', '$2', '$3', '$4', '$5', '$6', '$7')" } # Check if the observation already exists in the database observation_exists() { - sqlite3 -cmd ".timeout 1000" $DATABASE "SELECT EXISTS(SELECT observation_id FROM observation WHERE audio_file='$1' AND start='$2' AND end='$3' AND taxon_id='$4' AND location_id='$5')" + query "SELECT EXISTS(SELECT observation_id FROM observation WHERE audio_file='$1' AND start='$2' AND end='$3' AND taxon_id='$4' AND location_id='$5')" } \ No newline at end of file