Lokesh
3 min readJun 9, 2021

--

Create a Live Streaming App using Python

Steps To Complete This Task:

  • We will Create a Server and client.
  • The server will send the video to the client and the client will accept.
  • We will capture video using the OpenCV library on the server-side.
  • Then I Will serialize the frame to bytes @ the server-side.
  • Then we will pack the serialized data using the struct module
  • And finally will send message or bytes(str type) to the client
  • On the client-side, we will receive the bytes data
  • Then we will use the struct module to unpack data
  • Then we will de-serialize the data frame using again pickle module and will show the video on the client-side.

Let’s jump right to code.(All the Descriptions about every code snippets I have mentioned as comment lines in the code itself, so please have a look at them if you have any doubt further going with me.)

First I Will write Server.py:

# This code is for the server
# Lets import the libraries
import socket, cv2, pickle, struct, imutils
# Socket Create
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host_name = socket.gethostname()
host_ip = socket.gethostbyname(host_name)
print('HOST IP:', host_ip)
port = 9999
socket_address = (host_ip, port)
# Socket Bind
server_socket.bind(socket_address)
# Socket Listen
server_socket.listen(5)
print("LISTENING AT:", socket_address)
# Socket Accept
while True:
client_socket, addr = server_socket.accept()
print('GOT CONNECTION FROM:', addr)
if client_socket:
vid = cv2.VideoCapture(0)
while (vid.isOpened()):
img, frame = vid.read()
frame = imutils.resize(frame, width=320)
a = pickle.dumps(frame) #serialize frame to bytes
message = struct.pack("Q", len(a)) + a # pack the serialized data
# print(message)
try:
client_socket.sendall(message) #send message or data frames to client
except Exception as e:
print(e)
raise Exception(e)
cv2.imshow('TRANSMITTING VIDEO', frame) # will show video frame on server side.
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
client_socket.close()

Let’s write code for the client(Client.py):

import socket, pickle, struct
import cv2
# create socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host_ip = '192.168.99.1' # paste your server ip address here
port = 9999
client_socket.connect((host_ip, port)) # a tuple
data = b""
payload_size = struct.calcsize("Q") # Q: unsigned long long integer(8 bytes)
#Business logic to receive data frames, and unpak it and de-serialize it and show video frame on client side
while True:
while len(data) < payload_size:
packet = client_socket.recv(4 * 1024) # 4K, range(1024 byte to 64KB)
if not packet: break
data += packet # append the data packet got from server into data variable
packed_msg_size = data[:payload_size] #will find the packed message size i.e. 8 byte, we packed on server side.
data ​= data[payload_size:] # Actual frame data
msg_size = struct.unpack("Q", packed_msg_size)[0] # meassage size
# print(msg_size)
while len(data) < msg_size:
data += client_socket.recv(4 * 1024) # will receive all frame data from client socket
frame_data = data[:msg_size] #recover actual frame data
data ​= data[msg_size:]
frame = pickle.loads(frame_data) # de-serialize bytes into actual frame type
cv2.imshow("RECEIVING VIDEO", frame) # show video frame at client side
key = cv2.waitKey(1) & 0xFF
if key == ord('q'): # press q to exit video
break
client_socket.close()RESULTS:

--

--