From 09a2404fc383c14f08df0dc19d6bc5db4ce15fca Mon Sep 17 00:00:00 2001 From: Mathias Koehler Date: Sat, 29 Apr 2023 19:27:05 +0200 Subject: [PATCH] Check if v4l2 device has video inputs --- stream.sh | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/stream.sh b/stream.sh index a2d930a..1a928f1 100755 --- a/stream.sh +++ b/stream.sh @@ -5,6 +5,11 @@ if ! command -v inotifywait >/dev/null 2>&1; then exit 1 fi +if ! command -v v4l2-ctl >/dev/null 2>&1; then + echo "Please install v4l-utils" + exit 1 +fi + declare -A STREAMS failed() { @@ -28,13 +33,21 @@ streaming() { ) || failed "$1" } -check_initial_devices() { - for file in /dev/video*; do - file="${file##*/}" - echo "Found $file" +check() { + file=$1 + echo "Found $file" + + if v4l2-ctl -n -d "/dev/$file" | grep -q 'Input'; then (streaming "$file") & STREAMS[$file]=$! echo "$file PID ${STREAMS[$file]}" + fi +} + +check_initial_devices() { + for file in /dev/video*; do + file="${file##*/}" + check "$file" done } @@ -42,16 +55,13 @@ listen_for_new_devices() { inotifywait -m /dev --format "%f" -e create | while read -r file; do 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]}" + check "$file" fi fi done