Developing a Live Video Streaming Application using Socket Programming with Python

Amima Shifa
Nerd For Tech
Published in
4 min readJun 14, 2021
Business vector created by upklyak

Introduction :

Today, anyone with a fast enough Internet connection can stream high-definition movies or make a video call over the Internet. This is possible because of a technology called streaming.

Streaming is the continuous transmission of audio or video files from a server to a client. In simpler terms, streaming is what happens when consumers watch TV or listen to podcasts on Internet-connected devices. With streaming, the media file being played on the client device is stored remotely, and is transmitted a few seconds at a time over the Internet.

In the current scenario video streaming has become more popular due to the pandemic in order to socialize, overlook, connect, learn and so much more.

Objective :

To build a live video streaming application without audio using socket programming with python.

What is Socket Programming ?

Sockets and the socket API are used to send messages across a network. They provide a form of inter-process communication (IPC). The network can be a logical, local network to the computer, or one that’s physically connected to an external network, with its own connections to other networks. The most common example is the internet.

The most common type of socket applications are client-server applications, where one side acts as the server and waits for connections from clients, this is the type of live video streaming application we are going to build.

What type of sockets are suitable for our objective?

The type of sockets that are most suitable are TCP sockets as the Transmission Control Protocol (TCP):

  • Is reliable: packets dropped in the network are detected and retransmitted by the sender.
  • Has in-order data delivery: data is read by your application in the order it was written by the sender.
TCP Socket Flow

Let’s get to building the live video streaming application

Requirements -

  • IDE : Jupyter Notebook
  • Language : Python

As per the TCP flow we require two different files one for the client and another for the server. So there is some basic set up required for both of them and unless specified there is no difference as such.

Step 1 : Install and import the following libraries in both the files

You may need to install opencv-python and imutils specifically in oder to import them.

  • socket: To get socket module from python
    cv2: To import Computer Vision module from python
    pickle: for serializing and de-serializing python object structures.
    struct: to convert native Python data types such as strings and numbers into a string of bytes and vice versa
    imutils: Image processing operations

Step 2 : Create sockets

Creating a Client side socket
Creating a Server side socket

Step 3 : Stream video

In the server side we use accept() as it blocks and waits for an incoming connection. When a client connects, it returns a new socket object representing the connection and a tuple holding the address of the client. The tuple will contain (host, port) for IPv4 connections or (host, port, flowinfo, scopeid) for IPv6. Then we have to serialize frame to bytes, pack the serialized data and send this data and print the error message in case an error occurs and finally show the video frame.

Server accepting client’s request

The client creates a socket object, connects to the server and calls s.recv() to read the server’s reply . In the client side we receive data frames for the video streaming but in form of data packets which we have to unpack and de-serialize and then show the actual video frame.

Client Side

Result :

Screenshot from the video stream

Conclusion :

Hence, in this way we can create a live video streaming application without audio which can be used for purposes like surveillance, or may be to group study without disturbance etc.

For Reference :

GitHub Repository Link : https://github.com/AmimaShifa/Live-Video-Streaming-Application

--

--