Created
March 25, 2025 19:02
-
-
Save bitmorse/a2fd0f069398a2f29d4466fd2e2b2b74 to your computer and use it in GitHub Desktop.
take a snapshot from mjpg-streamer, blacken for privacy and upload to azure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# --- Config --- | |
STORAGE_ACCOUNT_NAME="" | |
CONTAINER_NAME="" | |
SAS_TOKEN="" | |
UPTIME_PING="" | |
CUSTOMER="" | |
CAMNAME="" | |
# --- Safety Checks --- | |
LOCKFILE="/var/lock/u3.lock" | |
exec 200>"$LOCKFILE" || exit 1 | |
flock -n 200 || exit 1 | |
# --- Functions --- | |
blacken_regions() { | |
local input="$1" | |
# Check if ImageMagick is available | |
if command -v convert >/dev/null 2>&1; then | |
#get polygon from https://www.image-map.net/ | |
convert "$input" -fill black -draw "polygon 56,172 142,447 1000,126 856,97 453,92" "$input" 2>/dev/null || return 0 | |
return 0 | |
else | |
# No image editing tools available - skip blackening | |
return 0 | |
fi | |
} | |
upload_to_azure() { | |
local file_path="$1" | |
local blob_name="$2" | |
# Verify file exists and has content before trying to upload | |
[ ! -s "$file_path" ] && return 1 | |
curl -i -X PUT \ | |
--max-time 15 \ | |
-H "x-ms-version: 2019-12-12" \ | |
-H "x-ms-date: $(date -u '+%a, %d %b %Y %H:%M:%S GMT')" \ | |
-H "x-ms-blob-type: BlockBlob" \ | |
-H "Content-Length: $(wc -c < "$file_path")" \ | |
--data-binary @"$file_path" \ | |
"https://$STORAGE_ACCOUNT_NAME.blob.core.windows.net/$CONTAINER_NAME/$blob_name$SAS_TOKEN" | |
return $? | |
} | |
# --- Main --- | |
DATE=$(date -u +%d-%m-%Y) | |
TIME=$(date -u +%H_%M_%S) | |
SNAPSHOT_FILE="/tmp/snapshot_$TIME.jpg" | |
BLOB_NAME="$CUSTOMER/$DATE/$CAMNAME/snapshot_$TIME.jpg" | |
LATEST_NAME="$CUSTOMER/latest/$CAMNAME.jpg" | |
# Try to capture snapshot | |
if curl -s --max-time 10 "http://localhost:8080/?action=snapshot" > "$SNAPSHOT_FILE" && [ -s "$SNAPSHOT_FILE" ]; then | |
# Check if file is valid (non-zero size) | |
if [ ! -s "$SNAPSHOT_FILE" ]; then | |
# Empty file - restart mjpg-streamer | |
/etc/init.d/mjpg-streamer restart | |
rm -f "$SNAPSHOT_FILE" | |
exit 1 | |
fi | |
# Blacken regions (if possible) | |
blacken_regions "$SNAPSHOT_FILE" | |
# Only send heartbeat if capture succeeds | |
FILE_SIZE=$(wc -c < "$SNAPSHOT_FILE" 2>/dev/null || echo "0") | |
curl -s --max-time 5 "${UPTIME_PING}${FILE_SIZE}bytes" >/dev/null 2>&1 | |
# Upload to Azure | |
upload_to_azure "$SNAPSHOT_FILE" "$LATEST_NAME" | |
upload_to_azure "$SNAPSHOT_FILE" "$BLOB_NAME" | |
else | |
# Restart mjpg-streamer if capture fails | |
/etc/init.d/mjpg-streamer restart | |
rm -f "$SNAPSHOT_FILE" | |
exit 1 | |
fi | |
# Cleanup | |
rm -f "$SNAPSHOT_FILE" | |
exec 200>&- # Explicitly close file descriptor | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment