Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Playing a video in tkinter
#11
import tkinter as tk
from PIL import Image, ImageTk
import imageio
 
root = tk.Tk()
class VideoPlayer:
    def __init__(self, root, video_path):
        self.root = root
        self.video_label = tk.Label(root)
        self.video_label.pack()
        self.video = imageio.get_reader(video_path)
        self.play_video = True
        self.frame_iter = self.video.iter_data()
        self.update()
 
    def update(self):
        if self.play_video:
            self.root.after(50, self.update)  # call update again after 1/20 of a second.
            try:
                frame = next(self.frame_iter)
                image = Image.fromarray(frame).resize((1000, 250))
                self.video_label.config(image=image)
                self.video_label.image = image
                self.root.update()
            except StopIteration:
                self.play_video = False
        else:
            self.video.close()
            root.destroy()
 
    def stop(self):
        self.play_video = False
 
 
if __name__ == "__main__":
    root.title("Video Player")
    video_path = r"C:\Users\RPC-K\Videos\vlc-record-2024-07-24-18h10m00s-windows 95 maze screensaver 022 speedrun  [WORLD RECORD].mp4-.mp4"
    player = VideoPlayer(root, video_path)
    root.protocol("WM_DELETE_WINDOW", player.stop)
    root.mainloop()
[

NOTE: I had to get rid of the music because I was getting other issues.
JUST A FRIENDLY REMINDER, I AM INFACT A YOUNG CHILD SO PLEASE FORGIVE ME IF I LACK OF ANY KNOWLEDGE THAT MAY SEEM OBVIOUS...I AM STILL DEVELOPING MY SKILLS IN PYTHON - YOURS SINCERELY, Moon
😋
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How would I be able to detect a media player app playing a video. phpjunkie 2 1,374 Oct-16-2023, 02:09 PM
Last Post: phpjunkie
  Shapes over video in tkinter finndude 1 1,786 Oct-04-2022, 06:14 PM
Last Post: deanhystad
  Tkinter Drawing/Annotating on live video KDog 4 4,096 Jan-24-2022, 03:18 PM
Last Post: Minimaxer
  run code while playing video almulder 0 2,516 Aug-26-2018, 07:19 PM
Last Post: almulder
  Python + OpenCV + Tkinter playing video help Hanban 1 19,764 May-08-2017, 01:25 AM
Last Post: Joseph_f2

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020