Beispiel #1
0
    def __init__(self, hass, camera):
        """Initialize."""
        super().__init__()

        self._camera = camera
        self._name = camera._friendly_name
        self._state = camera._state
        self._ssid = camera._ssid
        self._local_ip = camera._ip
        self._ssid = camera._ssid
        self._device_mac = camera._device_mac
        self._device_model = camera._device_model
        self._username = "******"
        self._password = "******"
        self.is_streaming = False
        self._ffmpeg = hass.data[DATA_FFMPEG]
        self._ffmpeg_arguments = DEFAULT_FFMPEG_ARGUMENTS
        self._ffmpeg_arguments_image = DEFAULT_FFMPEG_ARGUMENTS_IMAGE
        self._ffmpeg_image_frame = ImageFrame(self._ffmpeg.binary,
                                              loop=hass.loop)
        self._ffmpeg_stream = CameraMjpeg(self._ffmpeg.binary, loop=hass.loop)
        self._last_image = None
        self._last_image_url = None
        self._local_rtsp_port = camera._local_rtsp_port
        self._stream_url = f"rtsp://{self._username}:{self._password}@{self._local_ip}:{self._local_rtsp_port}/live"
        self.access_tokens = self.update_tokens()
Beispiel #2
0
    async def async_camera_image(self):
        """Return a frame from the camera stream."""
        ffmpeg = ImageFrame(self._ffmpeg.binary, loop=self.hass.loop)

        image = await asyncio.shield(
            ffmpeg.get_image(self._rtsp_stream, output_format=IMAGE_JPEG))
        return image
Beispiel #3
0
 async def async_camera_image(self):
     ffmpeg = ImageFrame(self._ffmpeg.binary)
     streaming_url = self.getStreamSource()
     image = await asyncio.shield(
         ffmpeg.get_image(streaming_url, output_format=IMAGE_JPEG,)
     )
     return image
Beispiel #4
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        image = None

        if self.device.capabilities.snapshot:
            try:
                image = await self.device.device.get_snapshot(
                    self.profile.token, self._basic_auth)
            except ONVIFError as err:
                LOGGER.error(
                    "Fetch snapshot image failed from %s, falling back to FFmpeg; %s",
                    self.device.name,
                    err,
                )

        if image is None:
            ffmpeg = ImageFrame(self.hass.data[DATA_FFMPEG].binary)
            image = await asyncio.shield(
                ffmpeg.get_image(
                    self._stream_uri,
                    output_format=IMAGE_JPEG,
                    extra_cmd=self.device.config_entry.options.get(
                        CONF_EXTRA_ARGUMENTS),
                ))

        return image
Beispiel #5
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        from haffmpeg.tools import ImageFrame, IMAGE_JPEG
        ffmpeg = ImageFrame(self._manager.binary, loop=self.hass.loop)

        image = await asyncio.shield(ffmpeg.get_image(
            self._input, output_format=IMAGE_JPEG,
            extra_cmd=self._extra_arguments), loop=self.hass.loop)
        return image
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        from haffmpeg.tools import ImageFrame, IMAGE_JPEG
        ffmpeg = ImageFrame(self._manager.binary, loop=self.hass.loop)

        image = await asyncio.shield(ffmpeg.get_image(
            self._input, output_format=IMAGE_JPEG,
            extra_cmd=self._extra_arguments), loop=self.hass.loop)
        return image
Beispiel #7
0
 async def async_camera_image(self, width=None, height=None):
     ffmpeg = ImageFrame(self._ffmpeg.binary)
     streaming_url = self.getStreamSource()
     image = await asyncio.shield(
         ffmpeg.get_image(
             streaming_url,
             output_format=IMAGE_JPEG,
             extra_cmd=self._extra_arguments,
         ))
     return image
Beispiel #8
0
async def isRtspStreamWorking(hass, host, username, password):
    _ffmpeg = hass.data[DATA_FFMPEG]
    ffmpeg = ImageFrame(_ffmpeg.binary)
    username = urllib.parse.quote_plus(username)
    password = urllib.parse.quote_plus(password)
    streaming_url = f"rtsp://{username}:{password}@{host}:554/stream1"
    image = await asyncio.shield(
        ffmpeg.get_image(streaming_url, output_format=IMAGE_JPEG,)
    )
    return not image == b""
Beispiel #9
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""

        #self._hass.async_create_task(async_reader_job(self._named_pipe, self._reader))

        from haffmpeg.tools import ImageFrame, IMAGE_JPEG
        ffmpeg = ImageFrame(self._ffmpeg_manager.binary, loop=self.hass.loop)

        image = await asyncio.shield(
            ffmpeg.get_image(self._named_pipe, output_format=IMAGE_JPEG))
        return image
Beispiel #10
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        self.renew_live_stream_session()

        from haffmpeg.tools import ImageFrame, IMAGE_JPEG
        ffmpeg = ImageFrame(self._ffmpeg.binary, loop=self.hass.loop)
        image = await asyncio.shield(ffmpeg.get_image(
            self._live_stream_session.live_stream_url,
            output_format=IMAGE_JPEG,
            extra_cmd=self._ffmpeg_arguments))
        return image
Beispiel #11
0
    async def async_camera_image(self):
        """Return a frame from the camera stream."""
        ffmpeg = ImageFrame(self._ffmpeg.binary)

        image = await asyncio.shield(
            ffmpeg.get_image(
                self._rtsp_stream,
                output_format=IMAGE_JPEG,
                extra_cmd=self._ffmpeg_arguments,
            ))
        return image
Beispiel #12
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        self.renew_live_stream_session()

        from haffmpeg.tools import ImageFrame, IMAGE_JPEG
        ffmpeg = ImageFrame(self._ffmpeg.binary, loop=self.hass.loop)
        image = await asyncio.shield(ffmpeg.get_image(
            self._live_stream_session.live_stream_url,
            output_format=IMAGE_JPEG,
            extra_cmd=self._ffmpeg_arguments), loop=self.hass.loop)
        return image
Beispiel #13
0
 async def async_camera_image(self):
     url = await self.stream_source()
     if url:
         ffmpeg = ImageFrame(self._manager.binary)
         self._last_image = await asyncio.shield(
             ffmpeg.get_image(
                 url,
                 output_format=IMAGE_JPEG,
                 extra_cmd=self._extra_arguments,
                 timeout=60,
             ))
     return self._last_image
Beispiel #14
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        await self.hass.async_add_executor_job(self.renew_live_stream_session)

        ffmpeg = ImageFrame(self._ffmpeg.binary, loop=self.hass.loop)
        image = await asyncio.shield(
            ffmpeg.get_image(
                self._live_stream_session.live_stream_url,
                output_format=IMAGE_JPEG,
                extra_cmd=self._ffmpeg_arguments,
            ))
        return image
Beispiel #15
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        from haffmpeg.tools import ImageFrame, IMAGE_JPEG

        url = await self.hass.async_add_job(self.get_latest_video_url)
        if url != self._last_url:
            ffmpeg = ImageFrame(self._manager.binary, loop=self.hass.loop)
            self._last_image = await asyncio.shield(ffmpeg.get_image(
                url, output_format=IMAGE_JPEG,
                extra_cmd=self._extra_arguments), loop=self.hass.loop)
            self._last_url = url

        return self._last_image
Beispiel #16
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        from haffmpeg.tools import ImageFrame, IMAGE_JPEG

        _LOGGER.debug("Retrieving image from camera '%s'", self._name)

        ffmpeg = ImageFrame(
            self.hass.data[DATA_FFMPEG].binary, loop=self.hass.loop)

        image = await asyncio.shield(ffmpeg.get_image(
            self._input, output_format=IMAGE_JPEG,
            extra_cmd=self._ffmpeg_arguments), loop=self.hass.loop)
        return image
Beispiel #17
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        url = await self._get_latest_video_url()
        if url and url != self._last_url:
            ffmpeg = ImageFrame(self._manager.binary)
            self._last_image = await asyncio.shield(
                ffmpeg.get_image(
                    url, output_format=IMAGE_JPEG, extra_cmd=self._extra_arguments
                ),
            )
            self._last_url = url

        return self._last_image
Beispiel #18
0
    def __init__(self, ffmpeg, device, coordinator):
        """Initialize."""
        super().__init__(device, coordinator)
        Camera.__init__(self)

        self._async_unsub_dispatcher_connect = None
        self._ffmpeg = ffmpeg
        self._ffmpeg_arguments = DEFAULT_FFMPEG_ARGUMENTS
        self._ffmpeg_image_frame = ImageFrame(ffmpeg.binary)
        self._ffmpeg_stream = CameraMjpeg(ffmpeg.binary)
        self._last_image = None
        self._last_image_url = None
        self._stream_url = None
Beispiel #19
0
def cli(ffmpeg, source, format_img, output, extra):
    """FFMPEG capture frame as image."""
    loop = asyncio.get_event_loop()

    stream = ImageFrame(ffmpeg_bin=ffmpeg, loop=loop)
    future = asyncio.ensure_future(
        stream.get_image(input_source=source, output_format=format_img, extra_cmd=extra)
    )
    image = loop.run_until_complete(future)

    if image is not None:
        with open(output, "wb") as fh_img:
            fh_img.write(image)
Beispiel #20
0
async def async_get_image(
    hass: HomeAssistant,
    input_source: str,
    output_format: str = IMAGE_JPEG,
    extra_cmd: str | None = None,
) -> bytes | None:
    """Get an image from a frame of an RTSP stream."""
    manager = hass.data[DATA_FFMPEG]
    ffmpeg = ImageFrame(manager.binary)
    image = await asyncio.shield(
        ffmpeg.get_image(input_source, output_format=output_format, extra_cmd=extra_cmd)
    )
    return image
Beispiel #21
0
    def __init__(self, hass, camera):
        """Initialize."""
        super().__init__()

        self._async_unsub_dispatcher_connect = None
        self._camera = camera
        self._ffmpeg = hass.data[DATA_FFMPEG]
        self._ffmpeg_arguments = DEFAULT_FFMPEG_ARGUMENTS
        self._ffmpeg_image_frame = ImageFrame(self._ffmpeg.binary)
        self._ffmpeg_stream = CameraMjpeg(self._ffmpeg.binary)
        self._last_image = None
        self._last_image_url = None
        self._stream_url = None
Beispiel #22
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        from haffmpeg.tools import ImageFrame, IMAGE_JPEG
        ffmpeg = ImageFrame(self._ffmpeg.binary, loop=self.hass.loop)

        if self._video_url is None:
            return

        image = await asyncio.shield(
            ffmpeg.get_image(self._video_url,
                             output_format=IMAGE_JPEG,
                             extra_cmd=self._ffmpeg_arguments))
        return image
Beispiel #23
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""

        _LOGGER.debug("Retrieving image from camera '%s'", self._name)

        ffmpeg = ImageFrame(self.hass.data[DATA_FFMPEG].binary, loop=self.hass.loop)

        image = await asyncio.shield(
            ffmpeg.get_image(
                self._input, output_format=IMAGE_JPEG, extra_cmd=self._ffmpeg_arguments
            )
        )
        return image
Beispiel #24
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        ffmpeg = ImageFrame(self._ffmpeg.binary)

        if self._video_url is None:
            return

        image = await asyncio.shield(
            ffmpeg.get_image(
                self._video_url,
                output_format=IMAGE_JPEG,
            ))
        return image
Beispiel #25
0
def cli(ffmpeg, source, format_img, output, extra):
    """FFMPEG capture frame as image."""
    loop = asyncio.get_event_loop()

    stream = ImageFrame(ffmpeg_bin=ffmpeg, loop=loop)
    future = asyncio.ensure_future(
        stream.get_image(input_source=source,
                         output_format=format_img,
                         extra_cmd=extra))
    image = loop.run_until_complete(future)

    if image is not None:
        with open(output, "wb") as fh_img:
            fh_img.write(image)
Beispiel #26
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        from haffmpeg.tools import ImageFrame, IMAGE_JPEG

        url = await self.hass.async_add_job(self.get_latest_video_url)
        if url != self._last_url:
            ffmpeg = ImageFrame(self._manager.binary, loop=self.hass.loop)
            self._last_image = await asyncio.shield(
                ffmpeg.get_image(url,
                                 output_format=IMAGE_JPEG,
                                 extra_cmd=self._extra_arguments))
            self._last_url = url

        return self._last_image
Beispiel #27
0
    async def async_camera_image(self) -> bytes | None:
        """Return a still image response from the camera."""
        await self.opp.async_add_executor_job(self.renew_live_stream_session)
        live_stream_url = await self.opp.async_add_executor_job(
            getattr, self._live_stream_session, "live_stream_url")

        ffmpeg = ImageFrame(self._ffmpeg.binary)
        image: bytes | None = await asyncio.shield(
            ffmpeg.get_image(
                live_stream_url,
                output_format=IMAGE_JPEG,
                extra_cmd=self._ffmpeg_arguments,
            ))
        return image
Beispiel #28
0
async def async_get_image(
    opp: OpenPeerPower,
    input_source: str,
    output_format: str = IMAGE_JPEG,
    extra_cmd: str | None = None,
):
    """Get an image from a frame of an RTSP stream."""
    manager = opp.data[DATA_FFMPEG]
    ffmpeg = ImageFrame(manager.binary)
    image = await asyncio.shield(
        ffmpeg.get_image(input_source,
                         output_format=output_format,
                         extra_cmd=extra_cmd))
    return image
Beispiel #29
0
    async def async_camera_image(self):
        """Return bytes of camera image."""
        _LOGGER.debug("Handling image from camera %s", self.device.name)
        stream_uri = await self.stream_source()

        ffmpeg = ImageFrame(self.hass.data[DATA_FFMPEG].binary,
                            loop=self.hass.loop)
        image = await asyncio.shield(
            ffmpeg.get_image(
                stream_uri,
                output_format=IMAGE_JPEG,
                extra_cmd=self.device.config_entry.options.get(
                    CONF_EXTRA_ARGUMENTS),
            ))

        return image
Beispiel #30
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        from haffmpeg.tools import ImageFrame, IMAGE_JPEG

        if not self._input:
            await self.hass.async_add_executor_job(self.obtain_input_uri)
            if not self._input:
                return None

        ffmpeg = ImageFrame(
            self.hass.data[DATA_FFMPEG].binary, loop=self.hass.loop)

        image = await asyncio.shield(ffmpeg.get_image(
            self._input, output_format=IMAGE_JPEG,
            extra_cmd=self._ffmpeg_arguments), loop=self.hass.loop)
        return image
Beispiel #31
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        image = None

        if self.device.capabilities.snapshot:
            if self._snapshot_uri is None:
                self._snapshot_uri = await self.device.async_get_snapshot_uri(
                    self.profile)

            auth = None
            if self.device.username and self.device.password:
                auth = HTTPDigestAuth(self.device.username,
                                      self.device.password)

            def fetch():
                """Read image from a URL."""
                try:
                    response = requests.get(self._snapshot_uri,
                                            timeout=5,
                                            auth=auth)
                    if response.status_code < 300:
                        return response.content
                except requests.exceptions.RequestException as error:
                    LOGGER.error(
                        "Fetch snapshot image failed from %s, falling back to FFmpeg; %s",
                        self.device.name,
                        error,
                    )

                return None

            image = await self.hass.async_add_executor_job(fetch)

        if image is None:
            ffmpeg = ImageFrame(self.hass.data[DATA_FFMPEG].binary,
                                loop=self.hass.loop)
            image = await asyncio.shield(
                ffmpeg.get_image(
                    self._stream_uri,
                    output_format=IMAGE_JPEG,
                    extra_cmd=self.device.config_entry.options.get(
                        CONF_EXTRA_ARGUMENTS),
                ))

        return image
Beispiel #32
0
    async def async_camera_image(self,
                                 width: int | None = None,
                                 height: int | None = None) -> bytes | None:
        """Return a still image response from the camera."""
        """Ignore width and height when the image is fetched from url."""
        """Camera component will resize it."""
        image = None

        if self._still_image_url:
            auth = None
            if self._user or self._password:
                auth = HTTPBasicAuth(self._user, self._password)

            def fetch():
                """Read image from a URL."""
                try:
                    response = requests.get(self._still_image_url,
                                            timeout=HTTP_TIMEOUT,
                                            auth=auth)
                    if response.status_code < 300:
                        return response.content
                except requests.exceptions.RequestException as error:
                    _LOGGER.error(
                        "Fetch snapshot image failed from %s, falling back to FFmpeg; %s",
                        self._name,
                        error,
                    )

                return None

            image = await self.hass.async_add_executor_job(fetch)

        if image is None:
            stream_source = await self.stream_source()
            if stream_source:
                ffmpeg = ImageFrame(self.hass.data[DATA_FFMPEG].binary)
                image = await asyncio.shield(
                    ffmpeg.get_image(stream_source,
                                     output_format=IMAGE_JPEG,
                                     extra_cmd=self._extra_arguments,
                                     width=width,
                                     height=height))

        return image
Beispiel #33
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""
        _LOGGER.debug("Retrieving image from camera '%s'", self._name)
        image = None

        if self._snapshot is not None:
            auth = None
            if self._username and self._password:
                auth = HTTPDigestAuth(self._username, self._password)

            def fetch():
                """Read image from a URL."""
                try:
                    response = requests.get(self._snapshot,
                                            timeout=5,
                                            auth=auth)
                    if response.status_code < 300:
                        return response.content
                except requests.exceptions.RequestException as error:
                    _LOGGER.error(
                        "Fetch snapshot image failed from %s, falling back to FFmpeg; %s",
                        self._name,
                        error,
                    )

                return None

            image = await self.hass.async_add_job(fetch)

        if image is None:
            # Don't keep trying the snapshot URL
            self._snapshot = None

            ffmpeg = ImageFrame(self.hass.data[DATA_FFMPEG].binary,
                                loop=self.hass.loop)
            image = await asyncio.shield(
                ffmpeg.get_image(
                    self._input,
                    output_format=IMAGE_JPEG,
                    extra_cmd=self._ffmpeg_arguments,
                ))

        return image
Beispiel #34
0
    async def async_camera_image(self):
        """Return a still image response from the camera."""

        try:
            host = self.host.async_render()
        except TemplateError as exc:
            _LOGGER.error("Error parsing template %s: %s", self.host, exc)
            return self._last_image

        url = await self.hass.async_add_executor_job(self.get_latest_video_url,
                                                     host)
        if url != self._last_url:
            ffmpeg = ImageFrame(self._manager.binary, loop=self.hass.loop)
            self._last_image = await asyncio.shield(
                ffmpeg.get_image(url,
                                 output_format=IMAGE_JPEG,
                                 extra_cmd=self._extra_arguments))
            self._last_url = url

        return self._last_image
async def async_get_image(
    hass: HomeAssistant,
    input_source: str,
    output_format: str = IMAGE_JPEG,
    extra_cmd: str | None = None,
    width: int | None = None,
    height: int | None = None,
) -> bytes | None:
    """Get an image from a frame of an RTSP stream."""
    manager = hass.data[DATA_FFMPEG]
    ffmpeg = ImageFrame(manager.binary)

    if width and height and (extra_cmd is None or "-s" not in extra_cmd):
        size_cmd = f"-s {width}x{height}"
        if extra_cmd is None:
            extra_cmd = size_cmd
        else:
            extra_cmd += " " + size_cmd

    image = await asyncio.shield(
        ffmpeg.get_image(input_source, output_format=output_format, extra_cmd=extra_cmd)
    )
    return image