Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Playing a video in tkinter
#1
Hi, I've been trying to simply play a video in python using the python GUI. So far, it has been working as intended and the video has been playing fine with no interuptions. However, there seem to be some issues:

- No sound (no module is installed)
- Tiny window showing the video
- The video is sped up
- If I try to exit the program while the video is playing, it crashes
*Yes, I do know the reason why the sound isnt playing but I have tried my best to install different modules in order to gain sound and none have worked.
Below is the code:

I hope that I manage to fix these problems I am experiencing with your sincere help and advice. I thank you for your time, patience and kindness! Heart Smash
import tkinter as tk
from tkinter import Label
from PIL import Image, ImageTk
import imageio
import threading

class VideoPlayer:
    def __init__(self, root, video_path):
        self.root = root
        self.video_path = video_path
        self.video_label = Label(root)
        self.video_label.pack()
        self.video = imageio.get_reader(r"C:\Users\RPC-K\Downloads\windows 95 maze screensaver 022 speedrun  [WORLD RECORD].mp4")
        self.play_video = True
        self.thread = threading.Thread(target=self.update, args=())
        self.thread.start()

    def update(self):
        for frame in self.video.iter_data():
            if not self.play_video:
                break
            image = Image.fromarray(frame)
            image = ImageTk.PhotoImage(image)
            self.video_label.config(image=image)
            self.video_label.image = image
            self.root.update()
        self.video.close()

    def stop(self):
        self.play_video = False
        self.thread.join()

def on_close():
    player.stop()
    root.destroy()

if __name__ == "__main__":
    root = tk.Tk()
    root.title("Video Player")
    video_path = 'path_to_your_video.mp4'
    player = VideoPlayer(root, video_path)
    root.protocol("WM_DELETE_WINDOW", on_close)
    root.mainloop()
Gribouillis write Jul-23-2024, 04:44 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
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


Messages In This Thread
Playing a video in tkinter - by moon - Jul-23-2024, 04:18 PM
RE: Playing a video in tkinter - by Axel_Erfurt - Jul-23-2024, 04:30 PM
RE: Playing a video in tkinter - by moon - Jul-23-2024, 04:34 PM
RE: Playing a video in tkinter - by menator01 - Jul-23-2024, 06:26 PM
RE: Playing a video in tkinter - by deanhystad - Jul-23-2024, 06:58 PM
RE: Playing a video in tkinter - by moon - Jul-23-2024, 08:25 PM
RE: Playing a video in tkinter - by deanhystad - Jul-23-2024, 09:48 PM
RE: Playing a video in tkinter - by moon - Jul-24-2024, 01:16 PM
RE: Playing a video in tkinter - by moon - Jul-24-2024, 02:52 PM
RE: Playing a video in tkinter - by deanhystad - Jul-24-2024, 03:18 PM
RE: Playing a video in tkinter - by moon - Jul-25-2024, 04:04 PM

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,537 Oct-16-2023, 02:09 PM
Last Post: phpjunkie
  Shapes over video in tkinter finndude 1 2,025 Oct-04-2022, 06:14 PM
Last Post: deanhystad
  Tkinter Drawing/Annotating on live video KDog 4 4,434 Jan-24-2022, 03:18 PM
Last Post: Minimaxer
  run code while playing video almulder 0 2,629 Aug-26-2018, 07:19 PM
Last Post: almulder
  Python + OpenCV + Tkinter playing video help Hanban 1 20,621 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