Skip to content

Commit

Permalink
#16 Supporting Last image as idle stream if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
YpNo committed May 15, 2024
1 parent 202f002 commit 46bbdf1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__
venv
.env
.DS_Store
44 changes: 40 additions & 4 deletions camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 46bbdf1

Please sign in to comment.