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 #!/usr/bin/env bash
set -euo pipefail
if ! command -v inotifywait >/dev/null 2>&1; then if ! command -v inotifywait >/dev/null 2>&1; then
echo "Please install inotify_utils" echo "Please install inotify_utils"
exit 1 exit 1
fi fi
declare -A STREAMS declare -A STREAMS
failed() {
streaming() { echo "Streaming $1 failed"
while true; do tail -n 5 "/tmp/$1_log"
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
} }
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() { check_initial_devices() {
for file in /dev/video*; do for file in /dev/video*; do
echo "Found ${file##*/}" file="${file##*/}"
(streaming "${file##*/}") & echo "Found $file"
(streaming "$file") &
STREAMS[$file]=$! STREAMS[$file]=$!
echo "${file##*/} PID ${STREAMS[$file]}" echo "$file PID ${STREAMS[$file]}"
done done
} }
listen_for_new_devices() { listen_for_new_devices() {
inotifywait -m /dev --format "%f" -e create | inotifywait -m /dev --format "%f" -e create |
while read -r file; do while read -r file; do
echo "Found $file" if [[ "$file" =~ video[0-9]+$ ]]; then
if [[ -v "STREAMS[$file]" && "$file" =~ video\d+$ ]]; then echo "Found $file"
(streaming "$file") &
STREAMS[$file]=$! if ! ps -p "${STREAMS[$file]}"> /dev/null; then
echo "$file PID ${STREAMS[$file]}" unset "STREAMS[$file]"
fi
if [[ ! -v "STREAMS[$file]" ]]; then
(streaming "$file") &
STREAMS[$file]=$!
echo "$file PID ${STREAMS[$file]}"
fi
fi fi
done done
} }
check_initial_devices check_initial_devices
listen_for_new_devices listen_for_new_devices