A
A
Andrew2018-04-16 13:45:23
Python
Andrew, 2018-04-16 13:45:23

How to create a video broadcast server?

How to create a video broadcast server? What is the best protocol to send video? How can I send a video from OpenCV to the browser?
I threw something, but for obvious reasons it does not work:

import socket
import cv2

RESPONSE  = "HTTP/1.1 200 OK\r\nServer: hrenoten\r\nAccept-Ranges: bytes\r\nContent-Type: application/octet-stream\r\nConnection: keep-alive\r\n\r\n"
BUFF_SIZE = 64*1024

#ip, port = tuple(input().split(' '))
ip, port = '127.0.0.1', 8000

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind( (ip, int(port)) )
sock.listen(10)

cap = cv2.VideoCapture(0)

conn, addr = sock.accept()
print('New connection: ' + addr[0])

while not conn.recv(BUFF_SIZE):
    pass
while True:
    ret, img = cap.read()
    byteImg = img.tostring()
    #А ещё я не понимаю нужны-ли HTTP заголовки
    conn.send(RESPONSE.encode('utf-8') + byteImg)

print(addr[0] + ' is closed')
conn.close()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
qlkvg, 2018-04-16
@null_object

https://gist.github.com/n3wtron/4624820 - the simplest option, just your case. If you are bored and lack adventure, read about web sockets.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question