1
0
mirror of https://github.com/EliasKotlyar/Xiaomi-Dafang-Hacks.git synced 2025-09-02 20:52:41 +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,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

View File

@@ -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

View File

@@ -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"

View File

@@ -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;