1
0
mirror of https://github.com/EliasKotlyar/Xiaomi-Dafang-Hacks.git synced 2025-09-03 13:12:44 +02:00

More robust Telegram video handling (#1769)

This commit is contained in:
Nick Kaijaks
2021-08-02 17:57:10 +01:00
committed by GitHub
parent 21f00d7445
commit 3ccc8bd65c
4 changed files with 79 additions and 40 deletions

View File

@@ -18,15 +18,22 @@ sendMessage() {
} }
sendFile() { sendFile() {
if [ -r $1 ]
then
echo "Sending file: $1" echo "Sending file: $1"
$CURL -s \ $CURL -s \
-X POST \ -X POST \
https://api.telegram.org/bot$apiToken/sendDocument \ https://api.telegram.org/bot$apiToken/sendDocument \
-F chat_id="$userChatId" \ -F chat_id="$userChatId" \
-F document=@"$1" -F document=@"$1"
else
echo "File not found: $1"
fi
} }
sendPhoto() { sendPhoto() {
if [ -r $1 ]
then
caption="$(hostname)-$(date +"%d%m%Y_%H%M%S")" caption="$(hostname)-$(date +"%d%m%Y_%H%M%S")"
echo "Sending Photo: $1 $caption" >> /tmp/telegram.log echo "Sending Photo: $1 $caption" >> /tmp/telegram.log
$CURL -s \ $CURL -s \
@@ -35,20 +42,31 @@ sendPhoto() {
-F chat_id="$userChatId" \ -F chat_id="$userChatId" \
-F photo="@${1}" \ -F photo="@${1}" \
-F caption="${caption}" -F caption="${caption}"
else
echo "File not found: $1"
fi
} }
sendVideo() { sendVideo() {
if [ -r $1 ]
then
bytes=$(busybox stat -c %s $1)
caption="$(hostname)-$(date +"%d%m%Y_%H%M%S")" caption="$(hostname)-$(date +"%d%m%Y_%H%M%S")"
echo "Sending Video: $1 $caption" >> /tmp/telegram.log echo "Sending Video: $1 $caption (${bytes}bytes)" >> /tmp/telegram.log
$CURL -s \ $CURL -s \
-X POST \ -X POST \
https://api.telegram.org/bot$apiToken/sendVideo \ https://api.telegram.org/bot$apiToken/sendVideo \
-F chat_id="$userChatId" \ -F chat_id="$userChatId" \
-F video="@${1}" \ -F video="@${1}" \
-F caption="${caption}" -F caption="${caption}"
else
echo "File not found: $1"
fi
} }
sendAnimation() { sendAnimation() {
if [ -r $1 ]
then
caption="$(hostname)-$(date +"%d%m%Y_%H%M%S")" caption="$(hostname)-$(date +"%d%m%Y_%H%M%S")"
echo "Sending Animation: $1 $caption" >> /tmp/telegram.log echo "Sending Animation: $1 $caption" >> /tmp/telegram.log
$CURL -s \ $CURL -s \
@@ -57,6 +75,9 @@ sendAnimation() {
-F chat_id="$userChatId" \ -F chat_id="$userChatId" \
-F animation="@${1}" \ -F animation="@${1}" \
-F caption="${caption}" -F caption="${caption}"
else
echo "File not found: $1"
fi
} }
[ "$what" == "m" ] && sendMessage $data [ "$what" == "m" ] && sendMessage $data

View File

@@ -49,7 +49,7 @@ FRAMERATE_NUM=25
VIDEOFORMAT=2 VIDEOFORMAT=2
# AudioFormat # AudioFormat
# Can be: OPUS | MP3 | PCM | PCMU # Can be: OPUS | MP3 | PCM | PCMU | OFF
AUDIOFORMAT=MP3 AUDIOFORMAT=MP3
# Audio sampling rate # Audio sampling rate
AUDIOINBR=16000 AUDIOINBR=16000

View File

@@ -211,7 +211,7 @@ filename=$(date "$filename_pattern")
/system/sdcard/bin/getimage > "$snapshot_tempfile" /system/sdcard/bin/getimage > "$snapshot_tempfile"
debug_msg "Got snapshot_tempfile=$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 & send_snapshot &
@@ -355,12 +355,18 @@ if [ "$send_telegram" = true ]; then
include /system/sdcard/config/telegram.conf include /system/sdcard/config/telegram.conf
if [ "$telegram_alert_type" = "video" -o "$telegram_alert_type" = "video+image" ] ; then if [ "$telegram_alert_type" = "video" -o "$telegram_alert_type" = "video+image" ] ; then
debug_msg "Send telegram video"
if [ "$video_use_rtsp" = true ]; then if [ "$video_use_rtsp" = true ]; then
#Convert file to mp4 and remove audio stream so video plays in telegram app 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/avconv -i "$video_tempfile" -c:v copy -an "$video_tempfile"-telegram.mp4
/system/sdcard/bin/telegram v "$video_tempfile"-telegram.mp4 /system/sdcard/bin/telegram v "$video_tempfile"-telegram.mp4
rm "$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 else
/system/sdcard/bin/avconv -i "$video_tempfile" "$video_tempfile-lo.mp4" /system/sdcard/bin/avconv -i "$video_tempfile" "$video_tempfile-lo.mp4"
/system/sdcard/bin/telegram v "$video_tempfile-lo.mp4" /system/sdcard/bin/telegram v "$video_tempfile-lo.mp4"
@@ -396,7 +402,7 @@ for i in /system/sdcard/config/userscripts/motiondetection/*; do
fi fi
done 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:" debug_msg "Waiting for background jobs to end:"
for jobpid in $(jobs -p); do for jobpid in $(jobs -p); do
wait "$jobpid" wait "$jobpid"

View File

@@ -11,6 +11,10 @@ JQ="/system/sdcard/bin/jq"
[ -z $apiToken ] && echo "api token not configured yet" && exit 1 [ -z $apiToken ] && echo "api token not configured yet" && exit 1
[ -z $userChatId ] && echo "chat id 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() { sendShot() {
/system/sdcard/bin/getimage > "/tmp/telegram_image.jpg" &&\ /system/sdcard/bin/getimage > "/tmp/telegram_image.jpg" &&\
$TELEGRAM p "/tmp/telegram_image.jpg" $TELEGRAM p "/tmp/telegram_image.jpg"
@@ -52,10 +56,16 @@ videoAlerts() {
$TELEGRAM m "Video alerts on motion detection enabled" $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() { respond() {
cmd=$1 cmd=$1
[ $chatId -lt 0 ] && cmd=${1%%@*} [ $chatId -lt 0 ] && cmd=${1%%@*}
case $cmd in case $cmd in
/status) status;;
/mem) sendMem;; /mem) sendMem;;
/shot) sendShot;; /shot) sendShot;;
/on) detectionOn;; /on) detectionOn;;
@@ -65,7 +75,8 @@ respond() {
/textalerts) textAlerts;; /textalerts) textAlerts;;
/imagealerts) imageAlerts;; /imagealerts) imageAlerts;;
/videoalerts) videoAlerts;; /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" /*) $TELEGRAM m "I can't respond to '$cmd' command"
esac esac
} }
@@ -109,7 +120,8 @@ main() {
if [ "$chatId" != "$userChatId" ]; then if [ "$chatId" != "$userChatId" ]; then
username=$(echo "$json" | $JQ -r ".result[0].$messageAttr.from.username // \"\"") username=$(echo "$json" | $JQ -r ".result[0].$messageAttr.from.username // \"\"")
firstName=$(echo "$json" | $JQ -r ".result[0].$messageAttr.from.first_name // \"\"") 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 else
respond $cmd respond $cmd
fi; fi;