From 46bbdf1e47548a462a87300319999eb42b3f311b Mon Sep 17 00:00:00 2001 From: Michael Lopez Date: Tue, 7 May 2024 22:54:25 +0200 Subject: [PATCH] #16 Supporting Last image as idle stream if exists --- .gitignore | 1 + camera.py | 44 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 7384a17..5cdf7c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__ venv .env +.DS_Store diff --git a/camera.py b/camera.py index 68ec026..9626bce 100644 --- a/camera.py +++ b/camera.py @@ -5,6 +5,7 @@ import os from device import Device from decouple import config +from pyaarlo.util import http_get DEBUG = config('DEBUG', default=False, cast=bool) @@ -154,12 +155,45 @@ async def _start_idle_stream(self): Start idle picture, writing to the proxy stream """ exit_code = 1 + idle_stream_command = ['ffmpeg', '-re', '-stream_loop', '-1', '-i', 'idle.mp4', + '-c:v', 'copy', + '-c:a', 'libmp3lame', '-ar', '44100', '-b:a', '8k', + '-bsf', 'dump_extra', '-f', 'mpegts', 'pipe:'] + + image_path = "/tmp/{}.jpg".format(self.name) + last_image = http_get(self._arlo.last_image, filename=image_path) + + # Using last camera's thumbnail as idle stream if exists + if last_image: + # Converting last_image to a video for loop_stream (less CPU consumsion) + convert = await asyncio.create_subprocess_exec( + *['ffmpeg', + '-framerate', '1', + '-i', image_path, + '-c:v', 'libx264', '-r', '30', '-vf', 'scale=640:-2', "/tmp/{}.mp4".format(self.name) + ], + stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE if DEBUG else subprocess.DEVNULL + ) + + if DEBUG: + asyncio.create_task( + self._log_stderr(convert, 'idle_stream') + ) + + convert_exit_code = await convert.wait() + if convert_exit_code == 0: + idle_stream_command = ['ffmpeg', + '-re', '-stream_loop', '-1', + '-i', "/tmp/{}.mp4".format(self.name), + '-c:v', 'copy', '-shortest', + '-f', 'mpegts', 'pipe:' + ] + while exit_code > 0: self.stream = await asyncio.create_subprocess_exec( - *['ffmpeg', '-re', '-stream_loop', '-1', '-i', 'idle.mp4', - '-c:v', 'copy', - '-c:a', 'libmp3lame', '-ar', '44100', '-b:a', '8k', - '-bsf', 'dump_extra', '-f', 'mpegts', 'pipe:'], + *idle_stream_command, stdin=subprocess.DEVNULL, stdout=self.proxy_writer, stderr=subprocess.PIPE if DEBUG else subprocess.DEVNULL @@ -202,6 +236,8 @@ async def _start_stream(self): asyncio.create_task( self._log_stderr(self.stream, 'live_stream') ) + else: + logging.debug(f"{self.name}: No stream available.") async def _stream_timeout(self): await asyncio.sleep(self.timeout)