首页 > 解决方案 > 如何使用 kivy v2.0.0 播放视频?

问题描述

我想用 Kivy 2.0.0 和 Python 3.6 播放视频,但运行此代码会给我一个加载错误,因为 Kivy 的视频播放器无法识别该文件。如何让 Kivy 的视频播放器识别文件?

from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.videoplayer import VideoPlayer
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.video import Video
from kivy.lang.builder import *
from kivy.app import App
import kivy
import os
kivy.require('2.0.0')

# Main page code
class MainPage(Screen):
    """ This is the code for the Main Screen """

    # Initialization
    def __init__(self, **var_args):

        # Constructor for the Main Page class
        super(MainPage, self).__init__(**var_args)

        # Layout
        self.layout = FloatLayout()

        # Video player
        self.filename = '/'

        self.player = VideoPlayer(source=self.filename,state='play',options={'allow_stretch': True})

        self.layout.add_widget(self.player)
    
        # Add the layout to the screen
        self.add_widget(self.layout)

# Main app class
class Video_Viewer(App):
    """ Main build """

    # Main build
    def build(self, **kwargs):

        # Create the screen manager
        self.manager = ScreenManager()
    
        # Add screens to screen manager
        self.manager.add_widget(MainPage(name='Main'))

        # Set the screen to the main sceren
        self.manager.current = 'Main'

        # Return the current screen of the screen manager
        return self.manager

# Run the file
if __name__ == '__main__':
    Video_Viewer().run()

这是由于代码而发生的错误:

[ERROR  ] [Image       ] Error loading <C:/Users/My Laptop/Downloads/video.mp4>

标签: pythonpython-3.xvideokivy

解决方案


尝试安装ffpyplayer

python -m pip install ffpyplayer


推荐阅读