From 3ccc8bd65c9e72cbbe87821e6354d068830983cc Mon Sep 17 00:00:00 2001 From: Nick Kaijaks Date: Mon, 2 Aug 2021 17:57:10 +0100 Subject: [PATCH] More robust Telegram video handling (#1769) --- firmware_mod/bin/telegram | 81 +++++++++++++-------- firmware_mod/config/rtspserver.conf.dist | 2 +- firmware_mod/scripts/detectionOn.sh | 20 +++-- firmware_mod/scripts/telegram-bot-daemon.sh | 16 +++- 4 files changed, 79 insertions(+), 40 deletions(-) diff --git a/firmware_mod/bin/telegram b/firmware_mod/bin/telegram index 894a1fa..c5f8163 100755 --- a/firmware_mod/bin/telegram +++ b/firmware_mod/bin/telegram @@ -18,45 +18,66 @@ sendMessage() { } sendFile() { - echo "Sending file: $1" - $CURL -s \ - -X POST \ - https://api.telegram.org/bot$apiToken/sendDocument \ - -F chat_id="$userChatId" \ - -F document=@"$1" + if [ -r $1 ] + then + echo "Sending file: $1" + $CURL -s \ + -X POST \ + https://api.telegram.org/bot$apiToken/sendDocument \ + -F chat_id="$userChatId" \ + -F document=@"$1" + else + echo "File not found: $1" + fi } sendPhoto() { - caption="$(hostname)-$(date +"%d%m%Y_%H%M%S")" - echo "Sending Photo: $1 $caption" >> /tmp/telegram.log - $CURL -s \ - -X POST \ - https://api.telegram.org/bot$apiToken/sendPhoto \ - -F chat_id="$userChatId" \ - -F photo="@${1}" \ - -F caption="${caption}" + if [ -r $1 ] + then + caption="$(hostname)-$(date +"%d%m%Y_%H%M%S")" + echo "Sending Photo: $1 $caption" >> /tmp/telegram.log + $CURL -s \ + -X POST \ + https://api.telegram.org/bot$apiToken/sendPhoto \ + -F chat_id="$userChatId" \ + -F photo="@${1}" \ + -F caption="${caption}" + else + echo "File not found: $1" + fi } sendVideo() { - caption="$(hostname)-$(date +"%d%m%Y_%H%M%S")" - echo "Sending Video: $1 $caption" >> /tmp/telegram.log - $CURL -s \ - -X POST \ - https://api.telegram.org/bot$apiToken/sendVideo \ - -F chat_id="$userChatId" \ - -F video="@${1}" \ - -F caption="${caption}" + if [ -r $1 ] + then + bytes=$(busybox stat -c %s $1) + caption="$(hostname)-$(date +"%d%m%Y_%H%M%S")" + echo "Sending Video: $1 $caption (${bytes}bytes)" >> /tmp/telegram.log + $CURL -s \ + -X POST \ + https://api.telegram.org/bot$apiToken/sendVideo \ + -F chat_id="$userChatId" \ + -F video="@${1}" \ + -F caption="${caption}" + else + echo "File not found: $1" + fi } sendAnimation() { - caption="$(hostname)-$(date +"%d%m%Y_%H%M%S")" - echo "Sending Animation: $1 $caption" >> /tmp/telegram.log - $CURL -s \ - -X POST \ - https://api.telegram.org/bot$apiToken/sendAnimation \ - -F chat_id="$userChatId" \ - -F animation="@${1}" \ - -F caption="${caption}" + if [ -r $1 ] + then + caption="$(hostname)-$(date +"%d%m%Y_%H%M%S")" + echo "Sending Animation: $1 $caption" >> /tmp/telegram.log + $CURL -s \ + -X POST \ + https://api.telegram.org/bot$apiToken/sendAnimation \ + -F chat_id="$userChatId" \ + -F animation="@${1}" \ + -F caption="${caption}" + else + echo "File not found: $1" + fi } [ "$what" == "m" ] && sendMessage $data diff --git a/firmware_mod/config/rtspserver.conf.dist b/firmware_mod/config/rtspserver.conf.dist index b83c112..4667404 100644 --- a/firmware_mod/config/rtspserver.conf.dist +++ b/firmware_mod/config/rtspserver.conf.dist @@ -49,7 +49,7 @@ FRAMERATE_NUM=25 VIDEOFORMAT=2 # AudioFormat -# Can be: OPUS | MP3 | PCM | PCMU +# Can be: OPUS | MP3 | PCM | PCMU | OFF AUDIOFORMAT=MP3 # Audio sampling rate AUDIOINBR=16000 diff --git a/firmware_mod/scripts/detectionOn.sh b/firmware_mod/scripts/detectionOn.sh index 6b08c0d..2a26ac8 100644 --- a/firmware_mod/scripts/detectionOn.sh +++ b/firmware_mod/scripts/detectionOn.sh @@ -211,7 +211,7 @@ filename=$(date "$filename_pattern") /system/sdcard/bin/getimage > "$snapshot_tempfile" debug_msg "Got snapshot_tempfile=$snapshot_tempfile" -#Next send picture alerts in the background +# Next send picture alerts in the background send_snapshot & @@ -355,13 +355,19 @@ if [ "$send_telegram" = true ]; then include /system/sdcard/config/telegram.conf if [ "$telegram_alert_type" = "video" -o "$telegram_alert_type" = "video+image" ] ; then - debug_msg "Send telegram video" if [ "$video_use_rtsp" = true ]; then - #Convert file to mp4 and remove audio stream so video plays in telegram app - /system/sdcard/bin/avconv -i "$video_tempfile" -c:v copy -an "$video_tempfile"-telegram.mp4 - /system/sdcard/bin/telegram v "$video_tempfile"-telegram.mp4 - rm "$video_tempfile"-telegram.mp4 + if [ "$AUDIOFORMAT" = "PCMU" ] || [ "$AUDIOFORMAT" = "OFF" ] ; then + # Convert file to mp4 and remove audio stream so video plays in telegram app + debug_msg "Send telegram video" + /system/sdcard/bin/avconv -i "$video_tempfile" -c:v copy -an "$video_tempfile"-telegram.mp4 + /system/sdcard/bin/telegram v "$video_tempfile"-telegram.mp4 + rm "$video_tempfile"-telegram.mp4 else + # avconv can't strip audio it doesn't understand + debug_msg "Send telegram video (only viable for external playback)" + /system/sdcard/bin/telegram v "$video_tempfile" + fi + else /system/sdcard/bin/avconv -i "$video_tempfile" "$video_tempfile-lo.mp4" /system/sdcard/bin/telegram v "$video_tempfile-lo.mp4" rm "$video_tempfile-lo.mp4" @@ -396,7 +402,7 @@ for i in /system/sdcard/config/userscripts/motiondetection/*; do fi done -# Wait for all background jobs to finish before existing and deleting tempfile +# Wait for all background jobs to finish before exiting and deleting tempfile debug_msg "Waiting for background jobs to end:" for jobpid in $(jobs -p); do wait "$jobpid" diff --git a/firmware_mod/scripts/telegram-bot-daemon.sh b/firmware_mod/scripts/telegram-bot-daemon.sh index 37fa85a..3ed793b 100644 --- a/firmware_mod/scripts/telegram-bot-daemon.sh +++ b/firmware_mod/scripts/telegram-bot-daemon.sh @@ -11,6 +11,10 @@ JQ="/system/sdcard/bin/jq" [ -z $apiToken ] && echo "api token not configured yet" && exit 1 [ -z $userChatId ] && echo "chat id not configured yet" && exit 1 +status() { + $TELEGRAM m "Motion detection `motion_detection status`\nNight mode `night_mode status`\nAlert type `get_config /system/sdcard/config/motion.conf telegram_alert_type`" +} + sendShot() { /system/sdcard/bin/getimage > "/tmp/telegram_image.jpg" &&\ $TELEGRAM p "/tmp/telegram_image.jpg" @@ -52,10 +56,16 @@ videoAlerts() { $TELEGRAM m "Video alerts on motion detection enabled" } +imageThenVideoAlerts() { + rewrite_config /system/sdcard/config/motion.conf telegram_alert_type "video+image" + $TELEGRAM m "Image then video alerts on motion detection enabled" +} + respond() { cmd=$1 [ $chatId -lt 0 ] && cmd=${1%%@*} case $cmd in + /status) status;; /mem) sendMem;; /shot) sendShot;; /on) detectionOn;; @@ -65,7 +75,8 @@ respond() { /textalerts) textAlerts;; /imagealerts) imageAlerts;; /videoalerts) videoAlerts;; - /help | /start) $TELEGRAM m "######### Bot commands #########\n# /mem - show memory information\n# /shot - take a snapshot\n# /on - motion detection on\n# /off - motion detection off\n# /nighton - night mode on\n# /nightoff - night mode off\n# /textalerts - Text alerts on motion detection\n# /imagealerts - Image alerts on motion detection\n# /videoalerts - Video alerts on motion detection";; + /dualalerts) imageThenVideoAlerts;; + /help | /start) $TELEGRAM m "######### Bot commands #########\n# /mem - show memory information\n# /status - show current camera status\n# /shot - take a snapshot\n# /on - motion detection on\n# /off - motion detection off\n# /nighton - night mode on\n# /nightoff - night mode off\n# /textalerts - Text alerts on motion detection\n# /imagealerts - Image alerts on motion detection\n# /videoalerts - Video alerts on motion detection\n# /dualalerts - Image snapshot then video alerts on motion detection";; /*) $TELEGRAM m "I can't respond to '$cmd' command" esac } @@ -109,7 +120,8 @@ main() { if [ "$chatId" != "$userChatId" ]; then username=$(echo "$json" | $JQ -r ".result[0].$messageAttr.from.username // \"\"") firstName=$(echo "$json" | $JQ -r ".result[0].$messageAttr.from.first_name // \"\"") - $TELEGRAM m "Received message from unauthorized chat id: $chatId\nUser: $username($firstName)\nMessage: $cmd" + # Uncomment to get notified of attempted chat spam + # $TELEGRAM m "Received message from unauthorized chat id: $chatId\nUser: $username($firstName)\nMessage: $cmd" else respond $cmd fi;