Video streaming in Tkinter with Python

Video streaming in Tkinter

Hey programmer, This tutorial will help you with the concept of video streaming in Tkinter. We will achieve this by using a Frame and a Label widget and use some libraries of Python namely: Tkinter, PIL, and Imageio.

Using:

from tkinter import *
import imageio
from PIL import Image, ImageTk

Video streaming inside a frame in Tkinter

We will use the Imageio library to store a video and get its metadata and also its frames. Then we will use the PIL library to add that frame captured as an image in the Label() widget defined inside a Frame() widget.

The Stream() function:

def stream():
    try:
        image = video.get_next_data()
        frame_image = Image.fromarray(image)
        frame_image=ImageTk.PhotoImage(frame_image)
        l1.config(image=frame_image)
        l1.image = frame_image
        l1.after(delay, lambda: stream())
    except:
        video.close()
        return

The above function pulls each frame out of the video as an image then stores it in a variable and sets the image parameter in a Label. The Stream() function is recursively called. This is done using the .after method()  to register callbacks.

Follow the steps below:

  1. Define the Tk window as the root variable.
  2. Next, we create a Frame and a Label with the frame as its parent, the latter will be used later to display each frame of the video as an image.
  3. Then we define a variable to store the path of the video and call the .get_reader() method of imageio library with the previous variable as a parameter to read that video.
  4. Next, We calculate the FPS of the video by .get_meta_data()[‘fps’] method and do some calculations to the result for accuracy.
  5. In the next step, we call the stream() function, which streams the video.
  6. End the program with a root.mainloop() to keep the process going recursively as long as the window runs.

The whole program looks like so:

from tkinter import *
import imageio
from PIL import Image, ImageTk
def stream():
    try:
        image = video.get_next_data()
        frame_image = Image.fromarray(image)
        frame_image=ImageTk.PhotoImage(frame_image)
        l1.config(image=frame_image)
        l1.image = frame_image
        l1.after(delay, lambda: stream())
    except:
        video.close()
        return   
########### Main Program ############

root = Tk()
root.title('Video in a Frame')
f1=Frame()
l1 = Label(f1)
l1.pack()
f1.pack()
video_name = "Futurama.mkv"   #Image-path
video = imageio.get_reader(video_name)
delay = int(1000 / video.get_meta_data()['fps'])
stream()
root.mainloop()

To learn more about Tkinter:

Introduction to Tkinter module in Python

2 responses to “Video streaming in Tkinter with Python”

  1. Srikant says:

    how can i change the speed of the video??

  2. Satyam Singh Niranjan says:

    Try to tinker with the ‘delay’ variable. The video.get_meta_data()[‘fps’] method extracts the FPS from the video, try using an integer instead of that method.

Leave a Reply