In this Kivy Tutorial we are going to learn How to Play Video in Kivy, according to Kivy Documentation the Video widget is used to display video files and streams. Depending on your Video core provider, platform, and plugins, you will be able to play different formats. For example, the pygame video provider only supports MPEG1 on Linux and OSX. GStreamer is more versatile, and can read many video containers and codecs such as MKV, OGV, AVI, MOV, FLV (if the correct gstreamer plugins are installed). Our VideoBase implementation is used under the hood.
Before starting our coding you need to install these two libraries, if you don’t do this there will be error when you play the video in kivy.
1 2 |
pip install ffpyplayer pip install pillow |
OK now let’s create our coding for How to Play Video in Kivy, this is the complete code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from kivy.app import App from kivy.uix.video import Video from kivy.uix.widget import Widget class VideoWindow(App): def build(self): video = Video(source='myvideo.mp4') video.state = 'play' video.options = {'eos': 'loop'} video.allow_stretch = True return video if __name__ == "__main__": window = VideoWindow() window.run() |
OK in the above code, first of all i have imported the required modules from kivy, after that i have created my class the extends from the App class, and i have created the object of my video widget. and there are different attributes that you can use with kivy video widget, also make sure that you have already added a video in your working directory, as i have already added the video.
- duration: duration of the video. The duration defaults to -1, and is set to a real duration when the video is loaded.
duration
is aNumericProperty
and defaults to -1. - eos: boolean, indicates whether the video has finished playing or not (reached the end of the stream).
eos
is aBooleanProperty
and defaults to False. - loaded: boolean, indicates whether the video is loaded and ready for playback or not.
loaded
is aBooleanProperty
and defaults to False. - options: options to pass at Video core object creation.
Run the complete code and this is the result.