Skip to content

Instantly share code, notes, and snippets.

@tito
Last active June 28, 2023 20:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tito/52cf2b545858358cd3d2a034337fd9fe to your computer and use it in GitHub Desktop.
Save tito/52cf2b545858358cd3d2a034337fd9fe to your computer and use it in GitHub Desktop.
Kivy/ffpyplayer transparent/alpha background video
"""
Kivy/ffpyplayer example to read transparent video
=================================================
Right now, VP9/ffmpeg encoder supports alpha channel, under the yuva420p
pixel format. But for decoding, the default libvp9 used by ffmpeg doesn't
support it, so we need to ask for libvpx-vp9 instead.
I was using a set of png image as a source, and got a VP9+alpha video with::
$ ffmpeg -r 60 -f image2 -s 2000x1500 -start_number 56 \
-i "perso2expressions%04d.png" -pix_fmt yuva420p -an out.webm
It tells it's a 60 FPS video, the filename starts from
"perso2expressions0056.png".
The Kivy code demonstrate that the background is actually showed.
"""
from os import environ
environ["KIVY_VIDEO"] = "ffpyplayer"
from kivy.app import App
from kivy.factory import Factory as F
from kivy.lang import Builder
import kivy.core.video.video_ffpyplayer as video_ffpyplayer
orig_media_player = video_ffpyplayer.MediaPlayer
def patch_media_player(*largs, **kwargs):
kwargs["ff_opts"]["vcodec"] = "libvpx-vp9"
return orig_media_player(*largs, **kwargs)
video_ffpyplayer.MediaPlayer = patch_media_player
KV = """
BoxLayout:
canvas.before:
Color:
rgb: 1, 0, 0
Rectangle:
size: self.size
Video:
id: video
source: "out.webm"
state: "play"
"""
class TestTransparentVideo(App):
def build(self):
self.root = Builder.load_string(KV)
self.video = self.root.ids.video
if __name__ == "__main__":
TestTransparentVideo().run()
@rostykerei
Copy link

which kivy and ffpyplayer version did you use? it doesn't work on the latest kivy 2.2.1 and ffpyplayer 4.5.0. thanks!

@jsdir
Copy link

jsdir commented Jun 28, 2023

@rostykerei did you ever figure this out?

@rostykerei
Copy link

rostykerei commented Jun 28, 2023

@jsdir yes, but you'd need to patch kivy.core.video.video_ffpyplayer and comment out the line self._out_fmt = 'yuv420p'. The latest player has a special handling of yuv420p pixel format, but doesn't handle alpha layer.

@jsdir
Copy link

jsdir commented Jun 28, 2023

@rostykerei this worked, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment