Building a video player can help you enjoy your favorite videos in a customized theme and style. you may make video playback smoother, design your app’s buttons and menus, and add whatever functionality you want.

This project will also give you hands-on experience in building cross-platform desktop apps, processing multimedia, and handling events. Discover how you can make a video media player using Tkinter, VLC, and the datetime module.

Start Screen of Video Media Player

The Tkinter, VLC, and Datetime Module

Tkinter allows you to create desktop applications. It offers a variety of widgets like buttons, labels, and text boxes that make it easier to develop applications likea simple GUI calendar, a calculator, or ato-do list manager. To install Tkinter, open a terminal and run:

Thepython-vlcmodule is a Python binding for the VLC (VideoLAN Client) media player library. You can use this module to implement thefeatures of VLCand build your own customized media player. To install VLC, run:

Video Playing on Video Media Player

Thedatetimemodule is built into Python and provides classes and functions to represent different dates, times, intervals, and zones.

Building the Structure of the Video Media Player

You can find this project’s source code in itsGitHub repository.

Import the required modules. Define a class,MediaPlayerApp. Define the constructor method and call it to initialize the main application window. Set the title, the dimensions, and the background color of the video media player. Call theinitialize_playermethod.

Define a method,initialize_player. Create an instance of the VLC media player to interact with its functionalities. Using this instance, create a media player object that you’re able to use to manage the media playback. Initialize a variable,current_fileto keep track of the currently playing video. Set the playback states and call thecreate_widgetsmethod.

Define thecreate_widgetsmethod. Create a canvas widget and pass the parent element to place it in, its background color, width, and height. Create aSelect Filebutton to select the video file you want to play. Set the parent element, the text it should display, the font styles, and the command it should run when you click it.

Create a label to display the time elapsed and the duration of the video. Set the parent element, the text, the font styles, the font color, and the background color. Create a frame to control the video playback and give it a background color.

Define thePlaybutton, thePausebutton, theStopbutton, theFast Forwardbutton, and theRewindbutton. Create a video progress bar widget. Set the parent element you want to place it in, the method to update the video playback position, the background color, and the thickness.

Organize all these elements with appropriate padding in both directions.

Building the Video Media Player Functionality

Define a method,select_file. Open a file dialog box to select a video file with.mp4or.aviextension. If you select any file, load its path and update the time label with its duration. Start playing the selected video.

Define a method,get_duration_strthat you will use to calculate the total duration of the video. If the application is playing a video, get its duration in milliseconds and convert it into theHH:MM:SSformat. If there is no video playing, return00:00:00as the default value.

Define a method,play_video. If a video is not playing, create a new media object using the selected file path. Associate the media with the canvas created earlier and initiate the video playback. Update theplaying_videostate toTrue.

Define a method,fast_forward. If a video is playing, get the current time elapsed and add 10,000 milliseconds to it. Set the new playback time. Similarly, define a method,rewindthat subtracts 10,000 milliseconds.

Define a method,pause_video. If you had started any video playback and paused it, call theplaymethod to resume it. Otherwise, call thepausemethod and update the UI accordingly in both cases.

Define a method,stop.If a video is playing, stop it, and reset the time label. Define a method,set_video_position. If a video is playing, retrieve the total duration and calculate the desired position in milliseconds. Set the video playback time to the calculated position.

Define a method,update_video_progress. If a video is playing, retrieve the total duration, and the current playback time, and calculate the progress percentage. Update the progress bar using this calculated value. Format the current time and total duration in theHH:MM:SSformat.

Schedule this method to run again after 1,000 milliseconds. This creates a loop that continuously updates the video progress and time labels while the video is playing.

Define a class,VideoProgressBarthat inherits fromtk.Scalewidget. Define a constructor that sets the initial state and behavior of the progress bar. Set theshowvalueoption toFalseto avoid displaying the current value.

Initialize the progress with a range of 0 to 100. Set the orientation, length, command it has to run, and the customization to the progress bar. Bind an event to the progress bar such that when you click it, it executes theon_clickmethod.

Define a method,on_click. Check if the progress bar is not disabled and calculates the new value based on the position of the click. Update the progress bar value accordingly.

Create an instance of theMediaPlayerAppclass, and call theupdate_video_progressmethod. Themainloop()function tells Python to run the Tkinter event loop and listen for events until you close the window.

Testing Different Features of the Video Media Player

On running the program, the video media player pops up. It contains theSelect Filebutton, the time labels, buttons to control video playback, and a video progress bar.

When you choose a video, it will play automatically from the beginning, updating the start time and the duration of the time labels.

On hitting thePausebutton, the video pauses, and changes to theResumebutton. On clicking theFast Forwardbutton, the video skips ahead by 10 seconds.

Similarly, on hitting theRewindbutton, it goes back 10 seconds. On pressing theStopbutton, the video playback halts. You can drag or click any area on the progress bar to move to any part of the video and the time label reads the time elapsed.

Enhancing the Video Media Player Application

You can enhance this video media player by adding an option to load and display subtitles. You could also consider features like changing the aspect ratio, controlling the volume, and looping part of the video.

To implement these features, you can explore the Pygame module. Pygame is versatile, easy to use, and integrates well with Tkinter. The library allows customization, has interactive features, and can run on any platform.