Sure! I can help you write a basic video player in Python with GUI using the Tkinter library. However, since Python's built-in libraries have limited support for playing video files, we'll need to use an external library called python-vlc to handle the video playback. python-vlc is a binding for the popular VLC media player.
To get started, make sure you have VLC media player installed on your system. You can download it from the official website (https://www.videolan.org/vlc/index.html).
Next, let's install the python-vlc library by running the following command in your terminal:
pip install python-vlc
Once you have everything set up, you can use the following code as a starting point for your video player:
import tkinter as tk
import vlc
class VideoPlayer:
def __init__(self, root):
self.root = root
self.root.title("Video Player")
# Create VLC player instance
self.instance = vlc.Instance()
self.player = self.instance.media_player_new()
# Create video frame
self.video_frame = tk.Frame(root)
self.video_frame.pack()
# Create control buttons
self.play_button = tk.Button(root, text="Play", command=self.play)
self.play_button.pack(side=tk.LEFT)
self.pause_button = tk.Button(root, text="Pause", command=self.pause)
self.pause_button.pack(side=tk.LEFT)
self.stop_button = tk.Button(root, text="Stop", command=self.stop)
self.stop_button.pack(side=tk.LEFT)
# Create file selection button
self.select_file_button = tk.Button(root, text="Select File", command=self.select_file)
self.select_file_button.pack(side=tk.LEFT)
def select_file(self):
file_path = tk.filedialog.askopenfilename(title="Select Video File",
filetypes=(("Video files", "*.mp4 *.avi"), ("All files", "*.*")))
if file_path:
self.play_video(file_path)
def play_video(self, file_path):
media = self.instance.media_new(file_path)
self.player.set_media(media)
# Set the window ID where to render VLC's video output
self.player.set_hwnd(self.video_frame.winfo_id())
# Play the video
self.player.play()
def play(self):
self.player.play()
def pause(self):
self.player.pause()
def stop(self):
self.player.stop()
if __name__ == "__main__":
root = tk.Tk()
player = VideoPlayer(root)
root.mainloop()
In this code, we create a VideoPlayer class that handles the video playback. It includes methods for selecting a video file, playing, pausing, and stopping the video. We use the tkinter library to create the GUI elements and the python-vlc library to handle the video playback.
To run the video player, save the code in a Python file (e.g., video_player.py) and execute it using the Python interpreter:
python video_player.py
This will open a window with control buttons for playing, pausing, and stopping the video. You can also use the "Select File" button to choose a video file to play.
Please note that this is a basic implementation, and you may need to add additional features or error handling based on your specific requirements.