Kivy 简明教程

Kivy - Video Player

Kivy 库中的 VideoPlayer 小部件是一个随时可用的控件,用于播放视频文件并控制其播放和声音。它是一种预定义的布局,具有播放区域和用于播放/停止和暂停/恢复播放的按钮。它还有一个进度条,可以用它将播放位置移动到所需位置。

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.

VideoPlayer 类在“kivy.uix.videoplayer”模块中定义。

The VideoPlayer class is defined in the "kivy.uix.videoplayer" module.

from kivy.uix.videoplayer import VideoPlayer
player = VideoPlayer(**kwargs)

您需要使用表示要播放的视频文件的一个字符串为 VideoPlayer 对象的 source 属性赋值。Kivy 支持包括“mp4”、“avi”和“mpg”在内的各种视频格式。

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".

要启动视频,您应将 state 属性设置为“play”。

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'

Kivy 的 VideoPlayer 的重要特性之一是能够在视频中的特定位置和特定持续时间内显示文本注释。注释保存在具有“.jsa”扩展名且与视频文件同名的文件中。

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.

jsa 文件是基于 JSON 的文件格式。它包含注释开始位置的值、在屏幕上显示的持续时间(以秒为单位)和注释文本。

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"},
]

若要让视频循环播放,请将 eos 选项设置为 loop −

To keep the video playing in a loop, set the eos option to loop −

options={'eos': 'loop'}

VideoPlayer 类定义了以下属性 −

The VideoPlayer class defines the following properties −

  1. source − The source of the video to read. source is a StringProperty representing the path of the video file to be played

  2. state − A string, indicates whether to play, pause, or stop the video. state is an OptionProperty and defaults to 'stop'.

  3. allow_fullscreen − By default, you can double-tap on the video to make it fullscreen. Set this property to False to prevent this behavior.

  4. 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.

  5. 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.

  6. thumbnail − Thumbnail of the video to show. If None, VideoPlayer will try to find the thumbnail from the source + '.png'.

  7. volume − Volume of the video in the range 0-1. 1 means full volume and 0 means mute.

  8. annotation − The VideoPlayer uses a VideoPlayerAnnotation object to construct annotations, based on the JSON data stored in a JSA file.

JSA 文件中允许使用以下键 −

Following keys are allowed to be used in JSA file −

  1. start − The position at which the annotation is to be displayed

  2. duration − Time for which the annotation label is displayed on the player window.

  3. text − The text to be displayed as annotation.

  4. bgcolor − [r, g, b, a] - background color of the text box

  5. bgsource − 'filename' - background image used for the background text box

  6. 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 −

kivy video player