This commit is contained in:
Paulus Schoutsen 2018-12-12 11:44:50 +01:00 committed by Paulus Schoutsen
parent e98476e026
commit a0bc96c20d

View file

@ -59,15 +59,21 @@ async def async_setup_platform(hass, config, async_add_entities,
def extract_image_from_mjpeg(stream):
"""Take in a MJPEG stream object, return the jpg from it."""
data = bytes()
data_start = b"\xff\xd8"
data_end = b"\xff\xd9"
for chunk in stream:
end_idx = chunk.find(data_end)
if end_idx != -1:
return data[data.find(data_start):] + chunk[:end_idx + 2]
data = b''
for chunk in stream:
data += chunk
jpg_end = data.find(b'\xff\xd9')
if jpg_end == -1:
continue
jpg_start = data.find(b'\xff\xd8')
if jpg_start == -1:
continue
return data[jpg_start:jpg_end + 2]
class MjpegCamera(Camera):