简体   繁体   中英

How can image format be determined from a byte stream?

I am trying to write a python script which joins a webcam multicast and stores each frame received as an image on the local hard drive. Typically, one may click on the link to the stream and the stream is played back in the browser, or in VLC media player.

The stream is supplied as a UDP multicast, to which I can subscribe with the following code (Python33, Windows 7):

import socket
import struct

MCAST_GRP = '(the ip)'
MCAST_PORT = (the port)

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', MCAST_PORT))
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)

sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)


while True:
    data = sock.recv(1316)

    print(data)

This is where I am stuck. data appears to be a byte stream in which I am hoping an image is encoded, but without knowing for sure which format the picture is in, I am having trouble proceeding to decode this stream and turn it into an actual image. Printing data shows output that is of this sort:

b'G\\x01\\x00\\x1c\\xc4 ...' b'G\\x01\\x00\\x18\\x87 ...' b'GA\\x015p\\x00\\xff\\xff ...'

How can I convert these byte streams to an actual image? Are there markers associated with jpg / png formats that I need to look for and isolate?

Thanks for the help in advance!

如果在流中也是文件(图像)的标题,则它包含有关格式的信息,例如, 有关BMP文件格式的信息,请参阅Wiki。

I am not an expert on python, but a bit of an expert on video streaming, and I suspect you might think of it in a wrong way from the very beginning. When you join multicast and receive the stream, it has little to do with static pictures. Video would normally be encoded according to H.264 standard, and frames are structured in so called group of pictures (GOP). GOP consists of I, P, and B frames, for example IPBBPBBPBBPBBPI, of which static pictures are I frames only. Normally, one would store I frames as sized down images to display them during FF or RW operations. To extract I frames from video stream you would normally use stand alone library, and the most usable I know - https://www.ffmpeg.org/

It will allow you decode the stream down to all 24,25, or whatever it is number of frames every second, in case you really want to store every picture in the video...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM