Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411512 Posts in 69376 Topics- by 58431 Members - Latest Member: Bohdan_Zoshchenko

April 27, 2024, 11:42:09 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityTownhallForum IssuesArchived subforums (read only)TutorialsPlay videos in Pygame (other than MPG)
Pages: [1]
Print
Author Topic: Play videos in Pygame (other than MPG)  (Read 7465 times)
SuperDisk
Level 2
**



View Profile
« on: November 11, 2013, 01:04:39 PM »

Pygame's built-in video module is a piece of junk. It's based off of an SMPEG library that, if I recall, is very old and not maintained.

It also only plays MPG (if you're lucky you'll get video, most of the time only sound), so if you're really determined to play videos through Pygame, hooking in Pyglet is the way I figured it out.

Import Pyglet, and create a player and load a video as such:

Code:
import pyglet
window = pyglet.window.Window(visible=False)
player = pyglet.media.Player()
player.queue(pyglet.media.load(filename))
player.play()

Then, create the window's draw function:

Code:
@window.event
def on_draw():
    #We have to convert the Pyglet media player's image to a Pygame surface
    rawimage = player.get_texture().get_image_data()
    pixels = rawimage.get_data('RGBA', rawimage.width * 4)
    video = pygame.image.frombuffer(pixels, (rawimage.width, rawimage.height), 'RGBA')

    #Blit the image to the screen
    screen.blit(video, (0, 0))

Now that everything is set up, a mainloop for Pyglet must be created.

Code:
while True:
    pyglet.clock.tick()

    for window in pyglet.app.windows:
        window.switch_to()
        window.dispatch_events()
        window.dispatch_event('on_draw')

You can include this code in your regular application loop.

That's it! pyglet.media.player has been able to play any video I've thrown at it, including webm and flv, so it can probably handle anything.

To control the video's flow (seek, pause, stop, etc), use the documentation here: http://www.pyglet.org/doc/api/pyglet.media.Player-class.html
Logged
mind_heist
Level 0
*


View Profile
« Reply #1 on: October 02, 2014, 01:18:08 PM »

Cool Stuff. I m trying to write something using pygame. I'll see if I can put this to use. BTW , Whats wrong with the SMPEG bases library that pygame provides ?
Logged
SuperDisk
Level 2
**



View Profile
« Reply #2 on: October 17, 2014, 06:15:11 PM »

It's so old that it doesn't even work half the time on MPG, the only format it supports.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic