Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter inserting video into window [duplicate]

I currently have this code for opening up a .mp4 file from a tkinter program.

import os
from tkinter import *

app = Tk()
app.title('Video Player')

Fcanvas = Canvas(bg="black", height=600, width=170)


def snd1():
    os.system("C:\\Users\Burky\\Desktop\\Videos\\PermsAndCombsVideo.mp4")

var = IntVar()

rb1 = Radiobutton(app, text= "Play Video", variable = var, value=1, command=snd1)
rb1.pack(anchor = W)
Fcanvas.pack()
app.mainloop()

This is good, although the video opens up within quick-time player and is not embedded within the window, is there a way to implement this into the main window instead of it opening up in the quick-time player?

thanks

like image 592
GJB Avatar asked Oct 28 '25 09:10

GJB


1 Answers

Here's one way to do it. This will continuously update the labels image to be the frames of the video specified. You'll have to handle sound if you want that too. This should get you started.

import tkinter as tk, threading
import imageio
from PIL import Image, ImageTk

video_name = "test.mkv" #This is your video file path
video = imageio.get_reader(video_name)

def stream(label):

    for image in video.iter_data():
        frame_image = ImageTk.PhotoImage(Image.fromarray(image))
        label.config(image=frame_image)
        label.image = frame_image

if __name__ == "__main__":

    root = tk.Tk()
    my_label = tk.Label(root)
    my_label.pack()
    thread = threading.Thread(target=stream, args=(my_label,))
    thread.daemon = 1
    thread.start()
    root.mainloop()
like image 83
Pythonista Avatar answered Oct 29 '25 22:10

Pythonista