When a people is detected, we start to take a video, then when no people is detected anymore we stop it.
prerequisite:
Proposals
Videos
We might record a video for the first x seconds (e.g 30 secs), to send to the resident quickly, and still record the whole video (until no more people are detected).
For the webapp, when we receive a notification of motion, we schedule a job in x seconds (e.g 30 secs), to retrieve the video, turn it into mp4 and send it to Telegram. ⚠️ If we detect a motion for less than x seconds, then the job should be canceled, or just do nothing because the video will be processed by the "no more motion" event. Indeed, when the webapp receive the "no more motion" event, it will process the video, and send it to Telegram if it's not too huge (too huge to define). This task might have to process 2 videos: the little and the whole.
To avoid to create tasks, and to synchronize camera software and webapp (e.g on the x seconds, 30 seconds), we can put the responsibility to the camera: when it split record, it send a mqtt message. Then the webapp receive it, retrieve the video and process it.
Benefits:
- The resident get a video quickly.
- We save quickly a video to the cloud. If the burglar decide to break the RPI, we still have a video.
How the webapp service will receive videos
We have to think how to send videos between different devices: PI 0 & the web app on PI4 for instance. For pictures, we use MQTT, it won't be possible for videos, this is too heavy.
- use rsync or something? As the PI 4 will be able to connect to every PI's on the network (#11). But it won't work for devices that doesn't have linux or something, like ESP devices. One question: do we really want to take videos when it's on a ESP or similar?
- use http. Is it suitable?
The main issue with HTTP is that, it is the responsibility of the camera software to send the video. If the service crashes, or get closed, we have to make sure that the video is sent. Thanks to the MQTT connectivity, if something goes wrong, the webapp will still have mqtt messages to react to: no more motion, disconnect. Even if the webapp goes offline, it will receive mqtt messages when it will be back.
Two possible strategies:
- Camera software send videos to the webapp.
- Webapp retrieve videos (thanks to mqtt messages that it will react to).
Issue with shared Docker Volume.
The smart-camera
produce a video, and then send a MQTT message to say to the python_process_mqtt
to create a Celery task to process the video. But! The worker can't find the video.
FileNotFoundError: [Errno 2] No such file or directory: 'MP4Box -add /usr/src/videos/f4e45230-d02e-4504-a972-eac5957c973f-0.h264 /usr/src/videos/f4e45230-d02e-4504-a972-eac5957c973f-0.mp4'
--
I checked if the video was in the folder, and it is:
docker-compose run rabbit_worker /bin/bash
user@675bdcfb87f8:/usr/src/app$ ls /usr/src/videos/f4e45230-d02e-4504-a972-eac5957c973f-0.h264
/usr/src/videos/f4e45230-d02e-4504-a972-eac5957c973f-0.h264
Even weirder, when I execute the command manually, it works:
MP4Box -add /usr/src/videos/f4e45230-d02e-4504-a972-eac5957c973f-0.h264 /usr/src/videos/f4e45230-d02e-4504-a972-eac5957c973f-0.mp4
AVC-H264 import - frame size 640 x 480 at 25.000 FPS
AVC Import results: 151 samples - Slices: 3 I 148 P 0 B - 0 SEI - 3 IDR
Saving to /usr/src/videos/f4e45230-d02e-4504-a972-eac5957c973f-0.mp4: 0.500 secs Interleaving
I tried to execute whoami
inside the call()
, it returns user
as expected.
🆗 The issue was the python subprocess call()
. I don't know why, but it doesn't work, even a simple ls /usr/src/videos
was not working. I switched to os.system()
with the exact same command, and it worked.
Send to Telegram
Telegram needs mp4 videos but PiCamera produces raw h264 videos. So we need to convert videos before we can send it.
The Pi captures video as a raw H264 video stream. Many media players will refuse to play it, or play it at an incorrect speed, unless it is "wrapped" in a suitable container format like MP4. The easiest way to obtain an MP4 file from the raspivid command is using MP4Box.
To convert videos, we will use MP4Box -add video.h264 video.mp4
from the package gpac
.
Sources:
Celery job to process video
⚠️ we might have issues with python subprocess call()
method inside a Celery job. We do it to executte MP4Box, to transform the raw h264 video to mp4.
Sources:
🆗 Finally we used os.system
but still, we could got this issue.
Retrieve the whole video
For the small video, it's not an issue because we send mqtt message. Same for the end of a motion. But what if the resident stop the alarm when a motion is recording... Well, I think that the alarm should not turn off if a motion is on. -> only for schedule. Thanks to this thoughts, I open a new issue #130 to improve the turn off feature. We have a flaw in the process.
Well, to avoid this issue, we have some options:
- in the smart-camera, send mqtt message before the
terminate()
call.
- in the web app, if we receive a disconnect message from the alarm, try to retrieve the video if we don't have it. It feels hacky to me... 🤔