Improve handling; Fix errors

This commit is contained in:
Mathias Koehler 2023-04-29 17:45:45 +02:00
parent d9564a8c38
commit 9f81008a74
Signed by: interru
GPG key ID: E01E350F5E2A884A

View file

@ -1,49 +1,61 @@
#!/usr/bin/env bash
set -euo pipefail
if ! command -v inotifywait >/dev/null 2>&1; then
echo "Please install inotify_utils"
exit 1
echo "Please install inotify_utils"
exit 1
fi
declare -A STREAMS
streaming() {
while true; do
echo "Streaming $1"
#echo ffmpeg -loglevel -8 -f v4l2 -framerate 30 -input_format mjpeg -i "/dev/$1" -f libndi_newtek -y "$1"
~/ndi/ffmpeg/ffmpeg -f v4l2 -framerate 30 -input_format mjpeg -i "/dev/$1" -pix_fmt uyvy422 -f libndi_newtek -y "$1.mov"
echo "Failed $1; Restarting"
sleep 1
done
failed() {
echo "Streaming $1 failed"
tail -n 5 "/tmp/$1_log"
}
streaming() {
echo "Streaming $1"
echo "Logfile: /tmp/$1_log"
(set -o xtrace; ffmpeg \
-f v4l2 \
-framerate 30 \
-i "/dev/$1" \
-pix_fmt uyvy422 \
-f libndi_newtek \
-y "$1" \
> "/tmp/$1_log" \
2>&1
) || failed "$1"
}
check_initial_devices() {
for file in /dev/video*; do
echo "Found ${file##*/}"
(streaming "${file##*/}") &
file="${file##*/}"
echo "Found $file"
(streaming "$file") &
STREAMS[$file]=$!
echo "${file##*/} PID ${STREAMS[$file]}"
echo "$file PID ${STREAMS[$file]}"
done
}
listen_for_new_devices() {
inotifywait -m /dev --format "%f" -e create |
while read -r file; do
echo "Found $file"
if [[ -v "STREAMS[$file]" && "$file" =~ video\d+$ ]]; then
(streaming "$file") &
STREAMS[$file]=$!
echo "$file PID ${STREAMS[$file]}"
if [[ "$file" =~ video[0-9]+$ ]]; then
echo "Found $file"
if ! ps -p "${STREAMS[$file]}"> /dev/null; then
unset "STREAMS[$file]"
fi
if [[ ! -v "STREAMS[$file]" ]]; then
(streaming "$file") &
STREAMS[$file]=$!
echo "$file PID ${STREAMS[$file]}"
fi
fi
done
}
check_initial_devices
listen_for_new_devices