Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Playing a video in tkinter
#7
A video has a resolution. A video recorded at 720p resolution is has frames that are 1280 x 720 pixels in size (921,600 pixels). When I play a 720p video using your program, that is the size of the video that I see. Since my screen resolution is 1920 x 1080 pixels (2,073,600 pixels), the image takes up 45% of the screen.

When I set the video to record at 240p resolution it has frames that are 320 x 240 pixels in size (76,800 pixels). This results in a smaller window. In fact, it should make a window that is only 320 x 240 pixels in size (about 4% of my laptop screen).

I modified your code to increase the video size, resizing each image.
    def update(self):
        for frame in self.video.iter_data():
            if not self.play_video:
                break
            image = Image.fromarray(frame).resize((640, 520))  # <- 2x the default size of 320 x 260
            image = ImageTk.PhotoImage(image)
            self.video_label.config(image=image)
            self.video_label.image = image
            self.root.update()
        self.video.close()
You could do something similar to change the of your video images.

In addition to resolution, a video has a frame rate. The only settings I have available for recording video on my laptop is 30 fps. When I play back the 720p video the frame rate looks about right. Your program must be taking about 1/30 of a second to read the frame, convert to an image, and set the image in the label. When I play the 260p video it runs much too fast. It takes less time to read and process the smaller frames, so the program plays more frames per second.

What you need is something that throttles your program so it only reads and displays 30 frames per second (or whatever the frame rate should be for your video). The tkVideoPlayer that menator mentions will do this. If you want to stick with using imageio, you need to either write code to do this, or import a tool that will do it for you (like the pyav package I mentioned in my previous post). Writing code to do this yourself might be fun. Here's a simple and not very good attempt.
import tkinter as tk
from PIL import Image, ImageTk
import imageio


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(33, self.update)  # call update again after 1/30 of a second.
            try:
                frame = next(self.frame_iter)
                image = Image.fromarray(frame)
                image = ImageTk.PhotoImage(image)
                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 = tk.Tk()
    root.title("Video Player")
    video_path = r"...\video.mp4"
    player = VideoPlayer(root, video_path)
    root.protocol("WM_DELETE_WINDOW", player.stop)
    root.mainloop()
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,533 Oct-16-2023, 02:09 PM
Last Post: phpjunkie
  Shapes over video in tkinter finndude 1 2,011 Oct-04-2022, 06:14 PM
Last Post: deanhystad
  Tkinter Drawing/Annotating on live video KDog 4 4,426 Jan-24-2022, 03:18 PM
Last Post: Minimaxer
  run code while playing video almulder 0 2,623 Aug-26-2018, 07:19 PM
Last Post: almulder
  Python + OpenCV + Tkinter playing video help Hanban 1 20,584 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