From 29dcd4c999ad750adc82b9877162e8b65ae63b03 Mon Sep 17 00:00:00 2001 From: Samuel ORTION Date: Sat, 13 Aug 2022 11:33:40 +0200 Subject: [PATCH] Fix install sed and systemd services,working on bullseye test --- .gitignore | 4 +- README.md | 2 +- TODO | 0 daemon/birdnet_clean.sh | 13 +++-- daemon/birdnet_recording.sh | 49 +++++++++++++------ .../birdnet_analyzis.service} | 3 +- .../{ => templates}/birdnet_recording.service | 7 +-- install.sh | 47 +++++++++++++++--- www/nginx.conf | 21 ++++++++ 9 files changed, 114 insertions(+), 32 deletions(-) create mode 100644 TODO rename daemon/systemd/{birdnet_analysis.service => templates/birdnet_analyzis.service} (67%) rename daemon/systemd/{ => templates}/birdnet_recording.service (65%) create mode 100644 www/nginx.conf diff --git a/.gitignore b/.gitignore index ce80226..ecdd97f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ var/ .env -species_list.txt \ No newline at end of file +species_list.txt + +push.sh \ No newline at end of file diff --git a/README.md b/README.md index 4534669..f0e4ec6 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ BirdNET-stream record sound on Linux computer and analyze it with the help of th On debian based system, you can install BirdNET-stream with the following command: ```bash -curl -sL https://raw.githubusercontent.com/birdnet-stream/birdnet-stream/master/install.sh | bash +curl -sL https://raw.githubusercontent.com/UncleSamulus/BirdNET-stream/main/install.sh | bash ``` For finer control, or to adapt to your system, you can follow the instructions in the [INSTALL.md](./INSTALL.md) file. diff --git a/TODO b/TODO new file mode 100644 index 0000000..e69de29 diff --git a/daemon/birdnet_clean.sh b/daemon/birdnet_clean.sh index 2b67a07..b9efb00 100755 --- a/daemon/birdnet_clean.sh +++ b/daemon/birdnet_clean.sh @@ -50,11 +50,14 @@ junk() { junk="$junk $(find ${CHUNK_FOLDER}/out -type d -empty)" # Get all empty record directories treatement_folder=$(find "${CHUNK_FOLDER}/out" -type d ! -empty) - for folder in $treatement_folder; do - if ! $(mem $folder $junk) && $(no_bird_in_model_output $folder); then - junk="$junk $folder" - fi - done + if [[ ! -z ${treatement_folder} ]]; then + for folder in $treatement_folder; do + echo $folder + if ! $(mem $folder $junk) && $(no_bird_in_model_output $folder); then + junk="$junk $folder" + fi + done + fi echo "$junk" } diff --git a/daemon/birdnet_recording.sh b/daemon/birdnet_recording.sh index 25b3511..0dc09b6 100755 --- a/daemon/birdnet_recording.sh +++ b/daemon/birdnet_recording.sh @@ -1,9 +1,38 @@ #! /usr/bin/env bash -record_chunk() { +DEBUG=${DEBUG:-0} + +export PULSE_RUNTIME_PATH="/run/user/$(id -u)/pulse/" + +debug() { + if [ $DEBUG -eq 1 ]; then + echo "$1" + fi +} + +check_folder() { + if [[ ! -d "${CHUNK_FOLDER}" ]]; then + debug "CHUNK_FOLDER does not exist: ${CHUNK_FOLDER}" + debug "Creating recording dir" + mkdir -p "${CHUNK_FOLDER}/in" + fi +} + +record_loop() { DEVICE=$1 DURATION=$2 - ffmpeg -f pulse -i ${DEVICE} -t ${DURATION} -vn -acodec pcm_s16le -ac 1 -ar 48000 file:${CHUNK_FOLDER}/in/birdnet_$(date "+%Y%m%d_%H%M%S").wav + debug "New recording loop." + while true; do + record $DEVICE $DURATION + done +} + + +record() { + DEVICE=$1 + DURATION=$2 + debug "Recording from $DEVICE for $DURATION seconds" + echo "" | ffmpeg -nostdin -f pulse -i ${DEVICE} -t ${DURATION} -vn -acodec pcm_s16le -ac 1 -ar 48000 file:${CHUNK_FOLDER}/in/birdnet_$(date "+%Y%m%d_%H%M%S").wav } config_filepath="./config/analyzer.conf" @@ -15,6 +44,8 @@ else exit 1 fi +check_folder + [ -z $RECORDING_DURATION ] && RECORDING_DURATION=15 if [[ -z $AUDIO_DEVICE ]]; then @@ -22,16 +53,4 @@ if [[ -z $AUDIO_DEVICE ]]; then exit 1 fi -check_folder() { - if [[ ! -d "${CHUNK_FOLDER}" ]]; then - echo "CHUNK_FOLDER does not exist: ${CHUNK_FOLDER}" - echo "Creating recording dir" - mkdir -p "${CHUNK_FOLDER}/in" - fi -} - -check_folder - -while true; do - record_chunk $AUDIO_DEVICE $RECORDING_DURATION -done +record_loop $AUDIO_DEVICE $RECORDING_DURATION \ No newline at end of file diff --git a/daemon/systemd/birdnet_analysis.service b/daemon/systemd/templates/birdnet_analyzis.service similarity index 67% rename from daemon/systemd/birdnet_analysis.service rename to daemon/systemd/templates/birdnet_analyzis.service index e9416f6..33d618c 100644 --- a/daemon/systemd/birdnet_analysis.service +++ b/daemon/systemd/templates/birdnet_analyzis.service @@ -6,7 +6,8 @@ Description=BirdNET-stream Analyzis [Service] User=$USER Group=$USER -ExecStart=/home/$USER/BirdNET-Analyzer/deamon/birdnet_analysis.sh +WorkingDirectory=$DIR +ExecStart=bash ./daemon/birdnet_analyzis.sh Restart=always RestartSec=3 Type=simple diff --git a/daemon/systemd/birdnet_recording.service b/daemon/systemd/templates/birdnet_recording.service similarity index 65% rename from daemon/systemd/birdnet_recording.service rename to daemon/systemd/templates/birdnet_recording.service index 1244017..6e38f10 100644 --- a/daemon/systemd/birdnet_recording.service +++ b/daemon/systemd/templates/birdnet_recording.service @@ -4,9 +4,10 @@ Description=BirdNET-stream recording [Service] -User=1000 -Group=1000 -ExecStart="bash /home/$USER/BirdNET-stream/daemon/birdnet_recording.sh" +User=$USER +Group=$DIR +WorkingDirectory=$DIR +ExecStart=bash ./daemon/birdnet_recording.sh" Restart=always RestartSec=3 Type=simple diff --git a/install.sh b/install.sh index 3d61c1a..406b62c 100755 --- a/install.sh +++ b/install.sh @@ -1,5 +1,5 @@ #! /usr/bin/env bash -set -X +# set -x set -e DEBUG=${DEBUG:-0} @@ -23,7 +23,6 @@ debug() { } install_requirements() { - if requirements=$1 # Install requirements missing_requirements="" @@ -40,6 +39,12 @@ install_requirements() { # Install BirdNET-stream install_birdnetstream() { + # Check if repo is not already installed + workdir=$(pwd) + if [ -d "$workdir/BirdNET-stream" ]; then + debug "BirdNET-stream is already installed" + return + fi # Clone BirdNET-stream debug "Cloning BirdNET-stream from $REPOSITORY" git clone --recurse-submodules $REPOSITORY @@ -56,17 +61,47 @@ install_birdnetstream() { # Install systemd services install_birdnetstream_services() { + cd BirdNET-stream + DIR=$(pwd) + GROUP=$USER + echo $DIR debug "Setting up BirdNET stream systemd services" - sudo ln -s $PWD/BirdNET-stream/daemon/systemd/birdnet_recording.service /etc/systemd/system/birdnet_recording.service - sudo ln -s $PWD/BirdNET-stream/daemon/systemd/birdnet_analyzis.service /etc/systemd/system/birdnet_analyzis.service + services="birdnet_recording.service birdnet_analyzis.service" + for service in $services; do + sudo cp daemon/systemd/templates/$service /etc/systemd/system/ + variables="DIR USER GROUP" + for variable in $variables; do + sudo sed -i "s|\$$variable|${!variable}|g" /etc/systemd/system/$service + done + done sudo systemctl daemon-reload - sudo systemctl enable --now birdnet_recording.service birdnet_analyzis.service + sudo systemctl enable --now $services +} + +install_web_interface() { + debug "Setting up web interface" + install_requirements "nginx php php-fpm composer nodejs npm" + cd BirdNET-stream + cd www + debug "Creating nginx configuration" + cp nginx.conf /etc/nginx/sites-available/birdnet-stream.conf + sudo ln -s /etc/nginx/sites-available/birdnet-stream.conf /etc/nginx/sites-enabled/birdnet-stream.conf + sudo systemctl enable --now nginx + sudo systemctl restart nginx + debug "Retrieving composer dependencies" + composer install + debug "Installing nodejs dependencies" + sudo npm install -g yarn + yarn build + debug "Building assets" + debug "Web interface is available" } main() { + # update install_requirements $REQUIREMENTS install_birdnetstream install_birdnetstream_services } -main \ No newline at end of file +main diff --git a/www/nginx.conf b/www/nginx.conf new file mode 100644 index 0000000..fda2b40 --- /dev/null +++ b/www/nginx.conf @@ -0,0 +1,21 @@ +server { + listen 80; + server_name birdnet.example.com; + root /var/www/html; + + access_log /var/log/nginx/birdnet/birdnet-access.log; + error_log /var/log/nginx/birdnet/birdnet-error.log error; + + index index.html index.htm index.php; + + location / { + try_files $uri $uri/ /index.php$is_args$args; + } + + location ~ \.php$ { + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass unix:/run/php-fpm/www.sock; + fastcgi_index index.php; + include fastcgi.conf; + } +}