Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python 3 Tkinter Project to Play MP4 Video Files With Advanced Controls Using tkvideoplayer Library GUI Desktop App

Posted on October 25, 2022

 

 

Welcome folks today in this blog post we will be playing video files inside the tkinter application of python using the tkvideoplayer package. All the full source code of the application is shown below.

 

 

Get Started

 

 

First of all guys you need to install this tkvideoplayer library using the below command of pip

 

pip install tkvideoplayer

 

 

After you install this library make an app.py file inside the root directory and copy paste the below code

 

 

app.py

 

 

Playing a Local MP4 Video File

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
import tkinter as tk
from tkVideoPlayer import TkinterVideo
 
root = tk.Tk()
 
videoplayer = TkinterVideo(master=root, scaled=True)
videoplayer.load(r"video.mp4")
videoplayer.pack(expand=True, fill="both")
 
videoplayer.play() # play the video
 
root.mainloop()

 

 

As you can see guys in the above example we are simply first of all importing the tkvideoplayer library and then we are playing the video file which is called video.mp4 inside the tkinter window using the load() method which is there inside this library. First of all as you can see we are initializing the constructor of this library passing the two options which is the master option which is set to the root window and also the second property which sets the scaling property to true. And then we are playing the video using the play() method which is there inside this library. Now if you execute the below app.py file and run you will see the below video playing

 

 

python app.py

 

 

 

 

 

As you can see only the video is playing there is no option to stop the video or control the video using the controls we have in simple video player. Now we need to add the video controls as well in the next step.

 

 

Tkinter Advanced Video Player With Controls

 

 

Now guys we will be having a complete video player example with controls also. Again create app.py file and copy paste the below code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from tkinter import *
from tkinter.filedialog import askopenfile
from tkVideoPlayer import TkinterVideo
 
window = Tk()
window.title("Tkinter Play Videos in Video Player")
window.geometry("700x450")
window.configure(bg="orange red")
 
 
def open_file():
    file = askopenfile(mode='r', filetypes=[
                       ('Video Files', ["*.mp4"])])
    if file is not None:
        global filename
        filename = file.name
        global videoplayer
        videoplayer = TkinterVideo(master=window, scaled=True, pre_load=False)
        videoplayer.load(r"{}".format(filename))
        videoplayer.pack(expand=True, fill="both")
        videoplayer.play()
 
 
 
def playAgain():
    print(filename)
    videoplayer.play()
 
def StopVideo():
    print(filename)
    videoplayer.stop()
 
def PauseVideo():
    print(filename)
    videoplayer.pause()
    
 
# center this label
lbl1 = Label(window, text="Tkinter Video Player", bg="orange red",
             fg="white", font="none 24 bold")
lbl1.config(anchor=CENTER)
lbl1.pack()
 
openbtn = Button(window, text='Open', command=lambda: open_file())
openbtn.pack(side=TOP, pady=2)
 
playbtn = Button(window, text='Play Video', command=lambda: playAgain())
playbtn.pack(side=TOP, pady=3)
 
stopbtn = Button(window, text='Stop Video', command=lambda: StopVideo())
stopbtn.pack(side=TOP, padx=4)
 
pausebtn = Button(window, text='Pause Video', command=lambda: PauseVideo())
pausebtn.pack(side=TOP, padx=5)
 
 
window.mainloop()

 

 

As you can see guys we are importing the required libraries at the very top. And also we are importing the tkinter file dialog library to allow the user to select their own dynamic video to play at the time of running the app. We no longer have to provide the static video. User can select their own video file using this file dialog popup window. And also we have the different label widgets appearing on the screen and also we have different buttons which controls the video. Like you can start the video, stop the video and also you can pause the video as well at any time by pressing the respective buttons. As you can see we have binded all the buttons with methods that we need to define in the next step. These methods will trigger once you hit the buttons.

 

 

 

 

Now we need to add the code inside the app.py to write the functions which are there once to hit the buttons or controls.

 

 

Taking Video Files as User Input Using File Dialog

 

 

Now guys in the below code we are taking the user input of video using the file dialog popup window

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
def open_file():
    file = askopenfile(mode='r', filetypes=[
                       ('Video Files', ["*.mp4"])])
    if file is not None:
        global filename
        filename = file.name
        global videoplayer
        videoplayer = TkinterVideo(master=window, scaled=True)
        videoplayer.load(r"{}".format(filename))
        videoplayer.pack(expand=True, fill="both")
        videoplayer.play()

 

 

As you can see guys we are only accepting the .mp4 extension of the video files. You can even add some more extensions to accept inside the array. And then we are checking if the file exists or not which is selected by the user and then we are extracting the name of the file which is selected by the user and then we are passing the name of video file to the load() method and then we are playing the video using the play() method.

 

 

Adding Code For Video Controls (Stop,Pause and Play Again) Video

 

 

Now we will be adding the code for the actual controls which will control the video which includes playing the video again, pausing the video and stopping the video. The actual methods are shown below

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
def playAgain():
    print(filename)
    videoplayer.play()
 
def StopVideo():
    print(filename)
    videoplayer.stop()
 
def PauseVideo():
    print(filename)
    videoplayer.pause()

 

 

As you can see we are calling the pause(), stop() and play() methods which is available inside the tkvideoplayer package to implement the respective methods.

 

 

Full Source Code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from tkinter import *
from tkinter.filedialog import askopenfile
from tkVideoPlayer import TkinterVideo
 
window = Tk()
window.title("Tkinter Play Videos in Video Player")
window.geometry("700x450")
window.configure(bg="orange red")
 
 
def open_file():
    file = askopenfile(mode='r', filetypes=[
                       ('Video Files', ["*.mp4"])])
    if file is not None:
        global filename
        filename = file.name
        global videoplayer
        videoplayer = TkinterVideo(master=window, scaled=True)
        videoplayer.load(r"{}".format(filename))
        videoplayer.pack(expand=True, fill="both")
        videoplayer.play()
 
 
 
def playAgain():
    print(filename)
    videoplayer.play()
 
def StopVideo():
    print(filename)
    videoplayer.stop()
 
def PauseVideo():
    print(filename)
    videoplayer.pause()
    
 
# center this label
lbl1 = Label(window, text="Tkinter Video Player", bg="orange red",
             fg="white", font="none 24 bold")
lbl1.config(anchor=CENTER)
lbl1.pack()
 
openbtn = Button(window, text='Open', command=lambda: open_file())
openbtn.pack(side=TOP, pady=2)
 
playbtn = Button(window, text='Play Video', command=lambda: playAgain())
playbtn.pack(side=TOP, pady=3)
 
stopbtn = Button(window, text='Stop Video', command=lambda: StopVideo())
stopbtn.pack(side=TOP, padx=4)
 
pausebtn = Button(window, text='Pause Video', command=lambda: PauseVideo())
pausebtn.pack(side=TOP, padx=5)
 
 
window.mainloop()

Recent Posts

  • Android Java Project to Crop,Scale & Rotate Images Selected From Gallery and Save it inside SD Card
  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Android Java Project to Embed Google Maps & Add Markers Using Maps SDK
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme