Open egueli opened 3 years ago
I tested on a Pi Zero W and alas some videos aren't being sent. Will investigate, in the meantime I'll turn this into a draft.
Thanks @egueli for yet another contribution! I think people would love to see this on the forum - we can gauge if there's interest for others to use it. 🙌
I think I solved the issues on the Pi Zero, so I un-drafted this PR and updated the description.
Is there any movement on these being folded into the main repo?
Description
This PR adds preliminary support for sending captured videos to a Telegram channel via a bot.
Bot usage
First setup:
Create a new bot via the BotFather bot and the
/newbot
command. Then copy the HTTP API token.Create a new private Telegram group. The bot will send all captured videos there.
Invite an ID bot like IDBot to the channel. It will join and leave immediately, then send you a message with the group ID.
Enter your MyNaturewatchCamera machine via SSH, and edit the configuration file:
and add two more key-value pairs:
Note that the first value is surrounded by quotes and the other is not.
Now restart the app with
and check the logs
If the line
Telegram publisher started
appears in the logs, the bot is running.Remember to enable video capture in the webpage. Once a video is captured, after two minutes the bot will publish it on that Telegram group.
Implementation details
Once a video is captured, it is "shrunk" with
ffmpeg
and sent via Telegram API, i.e. decode the original video and re-encode with a lower bandwidth. Shrinking is needed because the original video files are a bit too large to be shared comfortably, and may cause timeouts with Telegram APIs.The shrinking and sending take a couple minutes, so they're done on a separate thread to not starve the detection algorithm. If more videos are captured during shrinking, videos will be queued.
The code around
ChangeDetector
andFileSaver
has been reworked a bit to use exceptions instead of returningNone
, and keep the detection running if something happens. This is needed to prevent publishing if e.g. there's no more free space.Shrinking on Pi Zero
Video shrinking works differently if done on the Pi Zero. Because it has less CPU power, the encoding takes a bit too long (at least 15 minutes). So the hardware encoder is used instead, and the image size is smaller, bringing the shrink time to around 3 minutes (if also hardware decoding is enabled, see below). Alas the image quality is worse than with the software encoder, so the latter is used on a board with four CPU cores, like the Pi 3/4.
Using hardware video encoder
The shrinking process can make use of hardware decoding, to take less time. This is only available if the Pi is configured to reserve at least 192MB of RAM to the GPU. By default, only 128MB are reserved so the (slower) software decoding is used. See the Pi documentation to change the amount of RAM.
Unlike hardware encoding, hardware decoding doesn't seem to affect image quality.
TODO
Dear PR reviewers, please advice if you'd like to merge the PR as it is or you'd like any of the following to be present as well:
Type of change
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration.
Tested on the following devices:
Checklist: