Search code examples
pythonvideowebsocketdecodeframe

Decode h264 video frame from bytes received from a WebSocket


I'm trying to get frames from my home security camera (Provision-ISR).

So, I see when I open the web client, that the video frames are sent in a WebSocket.

I copy one of the frames,and I try to save it to file, but it's not working.

import numpy as np

from cv2 import cv2

frame_buffer = np.frombuffer(bytearray(frame), np.int16,int(len(frame) / 2))

cv2.imwrite("image.jpg",frame_buffer)

this is example of the hex editor

enter image description here


Solution

  • Solved! av.open(rawData, format="h264", mode='r') - do the decode

    def save_banch_of_frames(rawData):
        global count
        rawData.seek(0)
        container = av.open(rawData, format="h264", mode='r')
        for packet in container.demux():
            if packet.size == 0:
                continue
            for frame in packet.decode():
                cv2.imwrite(f"frames/file{count}.jpg", frame.to_ndarray(format="bgr24"))
                count += 1
    
    def check_is_keyframe(frame):
        frameData = io.BytesIO()
        frameData.write(frame)
        frameData.seek(0)
        container = av.open(frameData, format="h264", mode='r')
        for packet in container.demux():
            if packet.is_keyframe:
                return True
        return False
    
    
    data = get_frame_from_response(video_socket)
        while True:
            rawData = io.BytesIO()
            is_keyframe = False
    
            while not is_keyframe:
                rawData.write(data)
                data = get_frame_from_response(video_socket)
                is_keyframe = check_is_keyframe(data)
            save_banch_of_frames(rawData)