简体   繁体   中英

python kivy add background video mp4

Is it possible to play a video(mp4) as a background in kivy? 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....

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

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

[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....:(

Is it possible to play a video(mp4) as a background in kivy?

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

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. The link for these 2 files should the Builder.load_file()..

For example..

...
from kivy.uix.anchorlayout import AnchorLayout

Builder.load_file("widgets.kv")

class MyWidget(Widget):
    pass
...

Hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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