Kivy - Videos



The Video widget in Kivy framework capable of displaying video files and streams. The video formats you can play with Video widget depend on the operating system, video provider installed along with plugins required if any. The GStreamer provider is capable of handling almost any video codecs such as mpg, avi, mp4, mov, etc.

The Video class is defined in the "kivy.uix.video" module.

from kivy.uix.video import Video

vid = Video(**args)

The only mandatory parameter required to the constructor is the source property - a string representing the path to a video file.

vid = Video(source = "test.mp4")

In order to start the video playback, you need to set its play property to True. You can pass this parameter in the constructor to start the video as soon as it is loaded, or set it to True/False as and when required.

# start playing the video at creation
video = Video(source='test.mp4', play=True)

# create the video, and start later
video = Video(source='test.mp4')

# and later
video.play = True

Other properties of Video class are listed below −

  • duration − Duration of the video. The duration defaults to "-1", and is set to a real duration when the video is loaded.

  • eos − Stands for "end of stream". A Boolean property indicates whether the video has finished playing or not (reached the end of the stream).

  • play − Indicates whether the video is playing or not. You can start/stop the video by setting this property to True or False.

  • position − Position of the video between 0 and duration. The position defaults to -1 and is set to a real position when the video is loaded.

  • seek() − Change the position to seek as a proportion of the total duration, must be between 0-1.

  • state − A string, indicating whether to play, pause, or stop the video −

# start playing the video at creation
video = Video(source='test.mp4', state='play')

# create the video, and start later
video = Video(source='test.mp4')

# and later
video.state = 'play'
  • volume − Volume of the video, in the range 0-1. 1 means full volume, 0 means mute.

Example

from kivy.app import App
from kivy.uix.videoplayer import VideoPlayer
from kivy.uix.video import Video
from kivy.core.window import Window

Window.size = (720,400)

class MainApp(App):
   title = "Simple Video"
   def build(self):
      player = Video(source = "earth.mp4",
         size_hint = (1,1),
         options={'fit_mode': 'contain'})
      player.state = 'play'
      player.options = {'eos': 'loop'}
      player.allow_stretch=True
      return player

MainApp().run()

Output

The video playback starts as you run the above code −

Kivy Videos
Advertisements