Video player by Python tkinter

Video player by Python tkinter

Creating a video player using Python's tkinter library involves a few steps. tkinter itself doesn't have built-in support for video playback, so we'll use the opencv-python library to handle video decoding and tkinter for the GUI components. Here's a basic example to get you started:

Prerequisites

Before you start, make sure you have the necessary libraries installed:

pip install opencv-python-headless pillow 
  • opencv-python-headless: OpenCV library for video playback.
  • pillow: Python Imaging Library (PIL) for image handling in tkinter.

Example Code

Here's a simple implementation of a video player using tkinter and opencv-python:

import tkinter as tk import cv2 from PIL import Image, ImageTk class VideoPlayer: def __init__(self, window, video_source): self.window = window self.window.title("Video Player") self.cap = cv2.VideoCapture(video_source) self.canvas = tk.Canvas(window, width=self.cap.get(cv2.CAP_PROP_FRAME_WIDTH), height=self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) self.canvas.pack() self.btn_play = tk.Button(window, text="Play", width=10, command=self.play_video) self.btn_play.pack(pady=10) self.btn_pause = tk.Button(window, text="Pause", width=10, command=self.pause_video) self.btn_pause.pack() self.delay = 15 # milliseconds self.update() self.window.mainloop() def play_video(self): self.paused = False def pause_video(self): self.paused = True def update(self): ret, frame = self.cap.read() if ret and not self.paused: self.photo = ImageTk.PhotoImage(image=Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))) self.canvas.create_image(0, 0, anchor=tk.NW, image=self.photo) self.window.after(self.delay, self.update) # Replace 'your_video_file.mp4' with your actual video file path app = VideoPlayer(tk.Tk(), video_source='your_video_file.mp4') 

Explanation:

  1. Imports: Import necessary libraries (tkinter, cv2 from OpenCV, Image, and ImageTk from PIL).

  2. VideoPlayer Class:

    • Initializes the tkinter window (window) and sets the title.
    • Opens the video file using cv2.VideoCapture.
    • Creates a Canvas widget in the window to display video frames.
    • Adds 'Play' and 'Pause' buttons to control video playback.
    • Sets up a periodic update (update method) using after method of tkinter to continuously update frames.
  3. play_video and pause_video Methods:

    • play_video sets self.paused to False to resume video playback.
    • pause_video sets self.paused to True to pause video playback.
  4. update Method:

    • Reads a frame from the video using cap.read().
    • Converts the frame from BGR format (used by OpenCV) to RGB format (used by PIL).
    • Updates the Canvas widget (self.canvas) with the new frame using create_image.
  5. Main Application:

    • Creates an instance of VideoPlayer with the main tkinter window (Tk()) and specifies the path to the video file ('your_video_file.mp4').

Notes:

  • Make sure to replace 'your_video_file.mp4' with the path to your actual video file.
  • This is a basic implementation. You can extend it with additional features like seeking, volume control, fullscreen mode, etc., based on your requirements.
  • Handling video playback can be resource-intensive. Ensure your system can handle video decoding in real-time.

This example provides a foundational setup for building a video player using tkinter and opencv-python in Python. Adjust and expand upon it as needed for your specific application and UI requirements.

Examples

  1. Python tkinter video player example

    • Description: Implementing a basic video player using Python's tkinter library.
    • Code:
      import tkinter as tk import cv2 from PIL import Image, ImageTk class VideoPlayer: def __init__(self, window, video_source): self.window = window self.window.title("Python Video Player") self.cap = cv2.VideoCapture(video_source) self.width = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH) self.height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT) self.canvas = tk.Canvas(window, width=self.width, height=self.height) self.canvas.pack() self.btn_play = tk.Button(window, text="Play", command=self.play_video) self.btn_play.pack(pady=10) self.btn_pause = tk.Button(window, text="Pause", command=self.pause_video) self.btn_pause.pack(pady=5) self.update() def play_video(self): self.paused = False def pause_video(self): self.paused = True def update(self): ret, frame = self.cap.read() if ret and not self.paused: self.photo = ImageTk.PhotoImage(image=Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))) self.canvas.create_image(0, 0, image=self.photo, anchor=tk.NW) self.window.after(15, self.update) # Usage example if __name__ == "__main__": root = tk.Tk() player = VideoPlayer(root, "video.mp4") root.mainloop() 
    • This example creates a tkinter window with buttons for play and pause. The VideoPlayer class uses OpenCV (cv2) to capture frames from a video file (video.mp4) and display them in a tkinter canvas.
  2. Python tkinter video player with controls

    • Description: Adding play, pause, stop, and seek controls to a tkinter video player.
    • Code:
      import tkinter as tk import cv2 from PIL import Image, ImageTk class VideoPlayer: def __init__(self, window, video_source): self.window = window self.window.title("Python Video Player") self.cap = cv2.VideoCapture(video_source) self.width = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH) self.height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT) self.canvas = tk.Canvas(window, width=self.width, height=self.height) self.canvas.pack() self.btn_play = tk.Button(window, text="Play", command=self.play_video) self.btn_play.pack(side=tk.LEFT, padx=5) self.btn_pause = tk.Button(window, text="Pause", command=self.pause_video) self.btn_pause.pack(side=tk.LEFT, padx=5) self.btn_stop = tk.Button(window, text="Stop", command=self.stop_video) self.btn_stop.pack(side=tk.LEFT, padx=5) self.scale_seek = tk.Scale(window, from_=0, to=self.cap.get(cv2.CAP_PROP_FRAME_COUNT), orient=tk.HORIZONTAL, command=self.seek_video) self.scale_seek.pack(fill=tk.X, padx=5) self.paused = False self.update() def play_video(self): self.paused = False def pause_video(self): self.paused = True def stop_video(self): self.cap.set(cv2.CAP_PROP_POS_FRAMES, 0) self.paused = True def seek_video(self, frame): self.cap.set(cv2.CAP_PROP_POS_FRAMES, int(frame)) def update(self): ret, frame = self.cap.read() if ret and not self.paused: self.photo = ImageTk.PhotoImage(image=Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))) self.canvas.create_image(0, 0, image=self.photo, anchor=tk.NW) self.scale_seek.set(self.cap.get(cv2.CAP_PROP_POS_FRAMES)) self.window.after(15, self.update) # Usage example if __name__ == "__main__": root = tk.Tk() player = VideoPlayer(root, "video.mp4") root.mainloop() 
    • This code extends the previous example by adding additional controls (btn_stop, scale_seek) for stopping the video, seeking to a specific frame, and updating the seek slider (Scale) dynamically.
  3. Python tkinter video player with volume control

    • Description: Adding volume control to a tkinter video player using OpenCV and PIL.
    • Code:
      import tkinter as tk import cv2 import numpy as np from PIL import Image, ImageTk class VideoPlayer: def __init__(self, window, video_source): self.window = window self.window.title("Python Video Player") self.cap = cv2.VideoCapture(video_source) self.width = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH) self.height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT) self.canvas = tk.Canvas(window, width=self.width, height=self.height) self.canvas.pack() self.scale_volume = tk.Scale(window, from_=0, to=100, orient=tk.HORIZONTAL, label="Volume", command=self.set_volume) self.scale_volume.pack(fill=tk.X, padx=5) self.paused = False self.update() def set_volume(self, volume): volume_level = float(volume) / 100 self.cap.set(cv2.CAP_PROP_VOLUME, volume_level) def update(self): ret, frame = self.cap.read() if ret and not self.paused: self.photo = ImageTk.PhotoImage(image=Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))) self.canvas.create_image(0, 0, image=self.photo, anchor=tk.NW) self.window.after(15, self.update) # Usage example if __name__ == "__main__": root = tk.Tk() player = VideoPlayer(root, "video.mp4") root.mainloop() 
    • This example adds a volume control (scale_volume) using tkinter's Scale widget to adjust the video player's volume dynamically. Note that cv2.CAP_PROP_VOLUME may not be directly supported on all systems.
  4. Python tkinter video player with fullscreen mode

    • Description: Enabling fullscreen mode for a tkinter-based video player using OpenCV and PIL.
    • Code:
      import tkinter as tk import cv2 from PIL import Image, ImageTk class VideoPlayer: def __init__(self, window, video_source): self.window = window self.window.title("Python Video Player") self.cap = cv2.VideoCapture(video_source) self.width = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH) self.height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT) self.canvas = tk.Canvas(window, width=self.width, height=self.height) self.canvas.pack() self.btn_fullscreen = tk.Button(window, text="Fullscreen", command=self.toggle_fullscreen) self.btn_fullscreen.pack() self.fullscreen = False self.update() def toggle_fullscreen(self): self.fullscreen = not self.fullscreen self.window.attributes('-fullscreen', self.fullscreen) self.update() def update(self): ret, frame = self.cap.read() if ret: if self.fullscreen: frame = cv2.resize(frame, (self.window.winfo_screenwidth(), self.window.winfo_screenheight())) self.photo = ImageTk.PhotoImage(image=Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))) self.canvas.create_image(0, 0, image=self.photo, anchor=tk.NW) self.window.after(15, self.update) # Usage example if __name__ == "__main__": root = tk.Tk() player = VideoPlayer(root, "video.mp4") root.mainloop() 
    • This code enables fullscreen mode (toggle_fullscreen method) for the tkinter video player. The btn_fullscreen button toggles between fullscreen and normal window modes using self.window.attributes('-fullscreen', self.fullscreen).

More Tags

seek listbox gis push-notification oracle-apex-5 uibezierpath casting symfony2-easyadmin show-hide floating-action-button

More Programming Questions

More Trees & Forestry Calculators

More Genetics Calculators

More Entertainment Anecdotes Calculators

More Internet Calculators