T
T
The_XXI2019-08-16 18:30:07
Python
The_XXI, 2019-08-16 18:30:07

How to play video in PyGame?

Can't play video in pygame, pygame movie module is missing, video freezes via moviepy. Is there any way to play videos in pygame?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Astrohas, 2019-08-16
@Astrohas

There is also pyglet

import pygame, pyglet, ctypes

#setup pyglet & the video
path = r"C:\SomeVideo.avi"

player = pyglet.media.Player()
source = pyglet.media.load(path)
player.queue(source)
player.play()

#setup pygame
pygame.init()
pygame.display.set_mode((800,800), 0)
pygame.display.set_caption("Video in Pygame!")
screen = pygame.display.get_surface()
pygame.display.flip()

#blit the video in a standard pygame event loop
while True:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            sys.exit(0)
    screen.fill(0)
    
    player.dispatch_events()
    tex = player.get_texture()
    raw = tex.get_image_data().get_data('RGBA',tex.width*4)
    raw = ctypes.string_at(ctypes.addressof(raw), ctypes.sizeof(raw))
    img = pygame.image.frombuffer(raw, (tex.width, tex.height), 'RGBA')
    screen.blit(img, (0,0))
    
    pygame.display.flip()

The method is crutch, but you can try

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question