BirdNET-stream/daemon/birdnet_recording.sh

74 lines
1.8 KiB
Bash
Raw Normal View History

2022-08-12 18:01:01 +02:00
#! /usr/bin/env bash
2022-08-21 06:13:15 +02:00
DEBUG=${DEBUG:-1}
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
debug "New recording loop."
while true; do
2022-08-21 06:13:15 +02:00
record_device $DEVICE $DURATION
done
}
FFMPEG_OPTIONS="-nostdin -hide_banner -loglevel error -nostats -vn -acodec pcm_s16le -ac 1 -ar 48000 "
record_stream() {
local STREAM=$1
local DURATION=$2
local debug "Recording from $STREAM for $DURATION seconds"
2022-08-21 06:13:15 +02:00
ffmpeg $FFMPEG_OPTIONS -f -i ${DEVICE} -t ${DURATION} file:${CHUNK_FOLDER}/in/birdnet_$(date "+%Y%m%d_%H%M%S").wav
}
record_device() {
2022-08-12 18:01:01 +02:00
DEVICE=$1
DURATION=$2
debug "Recording from $DEVICE for $DURATION seconds"
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
2022-08-12 18:01:01 +02:00
}
2022-08-21 06:13:15 +02:00
config_filepath="./config/birdnet.conf"
2022-08-12 18:01:01 +02:00
if [ -f "$config_filepath" ]; then
source "$config_filepath"
else
echo "Config file not found: $config_filepath"
exit 1
fi
check_folder
2022-08-12 18:01:01 +02:00
[ -z $RECORDING_DURATION ] && RECORDING_DURATION=15
if [[ $AUDIO_RECORDING = "true" ]]; then
2022-08-21 06:13:15 +02:00
debug "Recording with on board device"
if [[ -z $AUDIO_DEVICE ]]; then
echo "AUDIO_DEVICE is not set"
exit 1
fi
record_loop $AUDIO_DEVICE $RECORDING_DURATION
fi
if [[ $AUDIO_STATIONS = "true" ]]; then
for station in $(ls ./config/stations/*.conf); do
source $station
2022-08-21 06:13:15 +02:00
record_stream $STATION_URL $RECORDING_DURATION
done
2022-08-21 06:13:15 +02:00
fi