简体   繁体   English

python kivy 添加背景视频mp4

[英]python kivy add background video mp4

Is it possible to play a video(mp4) as a background in kivy?是否可以在 kivy 中播放视频(mp4)作为背景? I am using the below code, but does not load up the video.....我正在使用下面的代码,但没有加载视频.....

__author__ = 'kshk'
import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import  GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.video import Video

class Dashboard(GridLayout):
    def __init__(self, **kwargs):
        super(Dashboard,self).__init__(**kwargs)
        self.cols = 2
        self.add_widget(Label(text="Login"))
        self.username = TextInput(multiline=False)
        self.add_widget(self.username)
        self.add_widget(Label(text="Password"))
        self.password = TextInput(password=True,multiline=False)
        self.add_widget(self.password)
        self.video = Video(source="/tmpt/driver.mp4")
        self.add_widget(self.video)

class MyApp(App):
    def build(self):
        return Dashboard()

if __name__ == '__main__':
    MyApp().run()

update:更新:

log:日志:

/usr/bin/python /home/kshk/PycharmProjects/KSHK-PYQT1/main.py
[INFO              ] Kivy v1.8.0
[INFO              ] [Logger      ] Record log in /home/kshk/.kivy/logs/kivy_14-07-13_58.txt
[INFO              ] [Factory     ] 157 symbols loaded
[DEBUG             ] [Cache       ] register <kv.lang> with limit=None, timeout=Nones
[DEBUG             ] [Cache       ] register <kv.image> with limit=None, timeout=60s
[DEBUG             ] [Cache       ] register <kv.atlas> with limit=None, timeout=Nones
[INFO              ] [Image       ] Providers: img_tex, img_dds, img_pygame, img_pil, img_gif 
[DEBUG             ] [Cache       ] register <kv.texture> with limit=1000, timeout=60s
[DEBUG             ] [Cache       ] register <kv.shader> with limit=1000, timeout=3600s
[INFO              ] [Text        ] Provider: pygame
[DEBUG             ] [Cache       ] register <kv.loader> with limit=500, timeout=60s
[INFO              ] [Loader      ] using a thread pool of 2 workers
[DEBUG             ] [Cache       ] register <textinput.label> with limit=None, timeout=60.0s
[DEBUG             ] [Cache       ] register <textinput.width> with limit=None, timeout=60.0s
[DEBUG             ] [Video       ] Ignored <pygst> (import error)
[DEBUG             ] [Video       ] Ignored <ffmpeg> (import error)
[DEBUG             ] [Video       ] Ignored <pyglet> (import error)
[INFO              ] [Video       ] Provider: null(['video_pygst', 'video_ffmpeg', 'video_pyglet'] ignored)
[DEBUG             ] [App         ] Loading kv </home/kshk/PycharmProjects/KSHK-PYQT1/my.kv>
[DEBUG             ] [App         ] kv </home/kshk/PycharmProjects/KSHK-PYQT1/my.kv> not found
[DEBUG             ] [Window      ] Ignored <egl_rpi> (import error)
[INFO              ] [Window      ] Provider: pygame(['window_egl_rpi'] ignored)
libpng warning: iCCP: known incorrect sRGB profile
[WARNING           ] [WinPygame   ] Video: failed (multisamples=2)
[WARNING           ] [WinPygame   ] trying without antialiasing
[DEBUG             ] [Window      ] Display driver x11
[DEBUG             ] [Window      ] Actual window size: 800x600
[DEBUG             ] [Window      ] Actual color bits r8 g8 b8 a0
[DEBUG             ] [Window      ] Actual depth bits: 24
[DEBUG             ] [Window      ] Actual stencil bits: 8
[DEBUG             ] [Window      ] Actual multisampling samples: 0
[INFO              ] [GL          ] OpenGL version <2.1 Mesa 10.1.5>
[INFO              ] [GL          ] OpenGL vendor <VMware, Inc.>
[INFO              ] [GL          ] OpenGL renderer <Gallium 0.4 on llvmpipe (LLVM 3.4, 256 bits)>
[INFO              ] [GL          ] OpenGL parsed version: 2, 1
[INFO              ] [GL          ] Shading version <1.30>
[INFO              ] [GL          ] Texture max size <8192>
[INFO              ] [GL          ] Texture max units <16>
[DEBUG             ] [Shader      ] Fragment compiled successfully
[DEBUG             ] [Shader      ] Vertex compiled successfully
[DEBUG             ] [ImagePygame ] Load </usr/lib/python2.7/site-packages/kivy/data/glsl/default.png>
[INFO              ] [Window      ] virtual keyboard not allowed, single mode, not docked
[DEBUG             ] [Atlas       ] Load </usr/lib/python2.7/site-packages/kivy/data/../data/images/defaulttheme.atlas>
[DEBUG             ] [Atlas       ] Need to load 1 images
[DEBUG             ] [Atlas       ] Load </usr/lib/python2.7/site-packages/kivy/data/../data/images/defaulttheme-0.png>
[DEBUG             ] [ImagePygame ] Load </usr/lib/python2.7/site-packages/kivy/data/../data/images/defaulttheme-0.png>
[INFO              ] [OSC         ] using <multiprocessing> for socket
[DEBUG             ] [Base        ] Create provider from mouse
[DEBUG             ] [Base        ] Create provider from probesysfs
[DEBUG             ] [ProbeSysfs  ] using probsysfs!
[INFO              ] [Base        ] Start application main loop
[INFO              ] [GL          ] NPOT texture support is available
[INFO              ] [Base        ] Leaving application in progress...

UPDATE: now i am trying to use AnchorLayout and add video to background and in the front use GridLayout....更新:现在我正在尝试使用 AnchorLayout 并将视频添加到背景中,并在前面使用 GridLayout ....

main.py主文件

__author__ = 'kshk'
import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import  GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.video import Video
from kivy.uix.widget import Widget
from kivy.uix.anchorlayout import AnchorLayout

class MyWidget(Widget):
    pass

class WidgetsApp(App):
    def build(self):
        return MyWidget()
if __name__ == '__main__':
    WidgetsApp().run()

widgets.kv小部件.kv

#File widgets.kv
#:kivy 1.8.0
<MyWidget>:
AnchorLayout:
    Video:
        source: 'driver.avi'
        size: 100,100
    GridLayout:
        Button:
            text: 'Hello'
            pos: 0,100
            size: 100, 50
            color: .8, .9, 0, 1
            font_size: 32
        Button:
            text: 'World!'
            pos: 100,0
            size: 100, 50
            color: .8, .9, 0, 1
            font_size: 32

All i get is a black screen!...我得到的只是黑屏!...

update:更新:

I pin-pointed out to this error...any idea how to get this plugin?我指出了这个错误……知道如何获得这个插件吗? I have gstreamer installed already.我已经安装了 gstreamer。

[DEBUG             ] [VideoPyGst  ] Load <driver.mp4>
** Message: don't know how to handle video/x-h264, stream-format=(string)byte-stream, alignment=(string)au, level=(string)4.2, profile=(string)main, width=(int)1920, height=(int)1080, framerate=(fraction)2997/125, pixel-aspect-ratio=(fraction)1/1, parsed=(boolean)true
[ERROR             ] [VideoPyGst  ] There is no codec present that can handle the stream's type.
[DEBUG             ] [VideoPyGst  ] gstplaybasebin.c(2323): prepare_output (): /GstPlayBin:playbin
[ERROR             ] [VideoPyGst  ] GStreamer encountered a general stream error.
[DEBUG             ] [VideoPyGst  ] qtdemux.c(3891): gst_qtdemux_loop (): /GstPlayBin:playbin/GstDecodeBin:decodebin0/GstQTDemux:qtdemux0:
streaming stopped, reason not-linked
[INFO              ] [Base        ] Leaving application in progress...

UPDATE:更新:

works on mac, with no issues, so looks like plugin issues on fedora....:(在 mac 上工作,没有问题,所以看起来像 Fedora 上的插件问题....:(

Is it possible to play a video(mp4) as a background in kivy?是否可以在 kivy 中播放视频(mp4)作为背景?

Yes, just put the video behind whatever else you want to display.是的,只需将视频放在您想要显示的任何其他内容之后。 Your example won't do this, as it's part of the gridlayout, you probably want to instead do something like use an AnchorLayout to put two children in the same place (this isn't the only way but it's simple).您的示例不会这样做,因为它是 gridlayout 的一部分,您可能想做一些事情,例如使用 AnchorLayout 将两个孩​​子放在同一个地方(这不是唯一的方法,但很简单)。

AnchorLayout:
    Video:
        source: 'whatever'
    GridLayout:
        # ... <- your normal stuff goes here

As for why your video doesn't display, I don't know.至于为什么你的视频不显示,我不知道。 Are you sure the filepath is valid?您确定文件路径有效吗? Do you get any logs about it in the terminal?你在终端中得到任何关于它的日志吗?

All i get is a black screen!...我得到的只是黑屏!...

Answering to this blackscreen problem, maybe you didnt link the python file with the .kv file.回答这个黑屏问题,也许你没有将 python 文件与 .kv 文件链接起来。 The link for these 2 files should the Builder.load_file()..这两个文件的链接应该是 Builder.load_file()..

For example..例如..

...
from kivy.uix.anchorlayout import AnchorLayout

Builder.load_file("widgets.kv")

class MyWidget(Widget):
    pass
...

Hope this help.希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM