-
Notifications
You must be signed in to change notification settings - Fork 2
/
upload.sh
90 lines (80 loc) · 3.27 KB
/
upload.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
trap "echo SIGINT received, exiting...; exit 0" INT
echo " ██╗████████╗███████╗███████╗██████╗ ██████╗"
echo " ██║╚══██╔══╝██╔════╝██╔════╝╚════██╗██╔══██╗"
echo " ██║ ██║ █████╗ █████╗ █████╔╝██║ ██║"
echo "██ ██║ ██║ ██╔══╝ ██╔══╝ ╚═══██╗██║ ██║"
echo "╚█████╔╝ ██║ ███████╗███████╗██████╔╝██████╔╝"
echo " ╚════╝ ╚═╝ ╚══════╝╚══════╝╚═════╝ ╚═════╝ "
echo ""
echo "This script sends snapshots of RTSP and MJPEG streams to Prusa Connect."
echo ""
: "${PRUSA_URL:=https://webcam.connect.prusa3d.com/c/snapshot}"
: "${RTSP_URLS:=}"
: "${CAMERA_URLS:=}"
: "${TOKENS:=}"
if [ -n "$RTSP_URLS" ]; then
echo "Detected use of RTSP_URLS environment variable. Use CAMERA_URLS instead."
CAMERA_URLS=$RTSP_URLS
fi
CAMERA_URLS=$(echo "$CAMERA_URLS" | tr -d ' ')
TOKENS=$(echo "$TOKENS" | tr -d ' ')
FRAME_CAPTURE_DELAY=${FRAME_CAPTURE_DELAY:-1}
CAMERA_CYCLE_DELAY=${CAMERA_CYCLE_DELAY:-10}
CONNECTION_TIMEOUT_DELAY=${CONNECTION_TIMEOUT_DELAY:-5}
IFS="," read -ra CAMERA_URLS <<< "$CAMERA_URLS"
IFS="," read -ra TOKENS <<< "$TOKENS"
FINGERPRINTS=()
for i in $(seq 1 ${#CAMERA_URLS[@]}); do
FINGERPRINTS+=($(printf "camera%010d" $i))
done
echo "Input variables:"
for i in "${!CAMERA_URLS[@]}"; do
echo "Camera $((i + 1)), URL: ${CAMERA_URLS[$i]}, ${TOKENS[$i]}"
done
while true; do
for i in "${!CAMERA_URLS[@]}"; do
echo "Processing camera: $((i + 1))"
echo "URL: ${CAMERA_URLS[$i]}"
echo "Token: ${TOKENS[$i]}"
echo "Fingerprint: ${FINGERPRINTS[$i]}"
echo "------"
if [[ ${CAMERA_URLS[$i]} == *"rtsp"* ]]; then
ffmpeg \
-loglevel error \
-y \
-rtsp_transport tcp \
-i "${CAMERA_URLS[$i]}" \
-f image2 \
-vframes 1 \
-pix_fmt yuvj420p \
-timeout "$CONNECTION_TIMEOUT_DELAY" \
output_$i.jpg
else
ffmpeg \
-loglevel error \
-y \
-i "${CAMERA_URLS[$i]}" \
-f image2 \
-vframes 1 \
-pix_fmt yuvj420p \
-timeout "$CONNECTION_TIMEOUT_DELAY" \
output_$i.jpg
fi
if [ $? -eq 0 ]; then
curl -X PUT "$PRUSA_URL" \
-H "accept: */*" \
-H "content-type: image/jpg" \
-H "fingerprint: ${FINGERPRINTS[$i]}" \
-H "token: ${TOKENS[$i]}" \
--data-binary "@output_$i.jpg" \
--no-progress-meter \
--compressed \
--max-time "$CONNECTION_TIMEOUT_DELAY"
else
echo "FFmpeg returned an error for camera $((i + 1))."
fi
sleep "$FRAME_CAPTURE_DELAY"
done
sleep "$CAMERA_CYCLE_DELAY"
done