”;
The VideoPlayer widget in Kivy library is a ready to use control to play a video file and control its playback and sound. It is a predefined layout with the playing area and buttons to play/stop and pause/resume playback. It also has a seekbar with which the play position can be moved to a desired place.
The VideoPlayer class is defined in the “kivy.uix.videoplayer” module.
from kivy.uix.videoplayer import VideoPlayer player = VideoPlayer(**kwargs)
You need to assign the source property of a VideoPlayer object with a String representing the video file to be played. Kivy supports different video formats including “mp4”, “avi” and “mpg”.
To start the video, you should set the state property to ”play”
from kivy.uix.videoplayer import VideoPlayer player = VideoPlayer(source = "test.mp4") player.state = ''play''
One of the important features of Kivy”s VideoPlayer is its ability to display text annotations at specific position in the video and for a certain duration. The annotations are provided in a file with “.jsa” extension, and with the same name as that of the video file.
The .jsa file is a JSON based file format. It contains the values for start position of the annotation, the duration in seconds for which it is displayed on the screen, and the annotation text.
[ {"start": 0, "duration": 2, "text": "Introduction"}, {"start": 10, "duration": 5, "text": "Hello World"}, ]
To keep the video playing in a loop, set the eos option to loop −
options={''eos'': ''loop''}
The VideoPlayer class defines the following properties −
-
source − The source of the video to read. source is a StringProperty representing the path of the video file to be played
-
state − A string, indicates whether to play, pause, or stop the video. state is an OptionProperty and defaults to ”stop”.
-
allow_fullscreen − By default, you can double-tap on the video to make it fullscreen. Set this property to False to prevent this behavior.
-
position − Position of the video between 0 and duration. The position defaults to -1 and is set to the real position when the video is loaded.
-
seek(percent, precise=True) − Change the position to a percentage (strictly, a proportion) of duration. The percent value should be a float or int and between 0-1. The precise parameter is a bool, defaults to True where precise seeking is slower, but seeks to exact requested percent.
-
thumbnail − Thumbnail of the video to show. If None, VideoPlayer will try to find the thumbnail from the source + ”.png”.
-
volume − Volume of the video in the range 0-1. 1 means full volume and 0 means mute.
-
annotation − The VideoPlayer uses a VideoPlayerAnnotation object to construct annotations, based on the JSON data stored in a JSA file.
Following keys are allowed to be used in JSA file −
-
start − The position at which the annotation is to be displayed
-
duration − Time for which the annotation label is displayed on the player window.
-
text − The text to be displayed as annotation.
-
bgcolor − [r, g, b, a] – background color of the text box
-
bgsource − ”filename” – background image used for the background text box
-
border − (n, e, s, w) – border used for the background image
Example
Th following code renders a video player widget on the application window −
from kivy.app import App from kivy.uix.videoplayer import VideoPlayer from kivy.core.window import Window Window.size = (720, 350) class MainApp(App): title = "Simple Video" def build(self): player = VideoPlayer( source="video.mp4", size_hint=(0.8, 0.8), options={''fit_mode'': ''contain''} ) player.state = ''play'' player.options = {''eos'': ''loop''} player.allow_stretch = True return player MainApp().run()
Output
Run the code and check the output −
”;