반응형

Create a Video Chat/Video Steaming App using Python

https://medium.com/geekculture/creating-video-chat-app-using-python-9da0a9c386ba

 

Create a Video Chat/Video Steaming App using Python

Due to the pandemic the only way to stay connected through the internet. But due to such a huge activity in Advertisement department, data…

medium.com

Server.py

from pyfiglet import Figlet
os.system("clear")
pyf = Figlet(font='puffy')
a = pyf.renderText("Video Chat App without Multi-Threading")
b = pyf.renderText("Server")
os.system("tput setaf 3")
print(a)
import socket, cv2, pickle,struct
# 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(1)
print("Listening at:",socket_address)
# Socket Accept
while True:
 client_socket,addr = server_socket.accept()
 print('Connected to:',addr)
 if client_socket:
  vid = cv2.VideoCapture(0)
  
  while(vid.isOpened()):
   ret,image = vid.read()
   img_serialize = pickle.dumps(image)
   message = struct.pack("Q",len(img_serialize))+img_serialize
   client_socket.sendall(message)
   
   cv2.imshow('Video from Server',image)
   key = cv2.waitKey(10) 
   if key ==13:
    client_socket.close()

Client.py

from pyfiglet import Figlet
os.system("clear")
pyf = Figlet(font='puffy')
a = pyf.renderText("Video Chat App without Multi-Threading")
b = pyf.renderText("Client")
os.system("tput setaf 3")
print(a)
import socket,cv2, pickle,struct
# create socket
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#  server ip address here
host_ip = '<IP>' 
port = 9999
client_socket.connect((host_ip,port)) 
data = b""
metadata_size = struct.calcsize("Q")
while True:
 while len(data) < metadata_size:
  packet = client_socket.recv(4*1024) 
  if not packet: break
  data += packet
 packed_msg_size = data[:metadata_size]
 data = data[metadata_size:]
 msg_size = struct.unpack("Q",packed_msg_size)[0]
 
 while len(data) < msg_size:
  data += client_socket.recv(4*1024)
  frame_data = data[:msg_size]
  data  = data[msg_size:]
  frame = pickle.loads(frame_data)
  cv2.imshow("Receiving Video ",frame)
  key = cv2.waitKey(10) 
  if key  == 13:
   break
client_socket.close()
반응형

'프로그래밍 > Python' 카테고리의 다른 글

[python] Top 10 Python Libraries  (0) 2023.10.26
[python] Stack in Python  (0) 2023.10.24
[python] PyAudio  (0) 2023.10.20
[Python] savefig 0.0.4  (0) 2023.10.17
[python] pdf to png, 해상도 높게 저장하기  (0) 2023.10.04

+ Recent posts