[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] How to decode MPEG?



I haven't done it myself, but this should work (it's going to be *slow* without some C code, though):

* Create a Surface stored in memory (SWSURFACE flag if I'm reading
the PyGame docs right), you will want to make this a power-of-2
size, as then PyGame will automatically scale it for you.
o Alternately, in your glTexSubImage2D call you can copy to a
sub-rectangle that matches the movie's dimensions (to
minimize scaling of the video)
* Set your software surface as the surface for the Movie object
* Create a Movie instance
o Decide what frame you want to display, or whether you just
want to tell it to play in real-time (i.e. so that it
matches the audio)
* When you want to render a frame:
o Get the image data as an array of R,G,B bytes
+ pygame.surfarray.array3d( movie_surface )
o Update your PyOpenGL texture to use the returned array as
its data
+ glTexSubImage2D( ... )

The reason it'll be slow is that you wind up copying the entire frame 3x (once to scale it, once to create the array, and again to the OpenGL implementation's texture memory).

Ideally you'd like to be able to tell the Movie to render directly to a surface/memory array that could be passed directly to PyOpenGL (i.e. without the copy to another array first), but that would require some work on both PyGame and PyOpenGL sides that I'm not likely to have any time soon, (you'd likely need some locking logic to make it stable in that case, btw).

Good luck, and let us know how it goes,
Mike

Gary Herron wrote:

I've got a Python/PyOpenGL application that now needs to display some
MPEG video streams. The project was not written with PyGame (although
I'd now like to borrow Guido's time machine and change that decision),
but is it possible to use PyGame's MPEG decoding ability in some kind
of stand-alone library-ish way to get the decoded pixels frame-by-frame.

I've got all the machinery necessary to periodically drop the pixels
of a frame into a OpenGL texture being mapped into my scene. All I
need is a way to get the pixels of successive frames at appropriate
times.
The Movie class is too high a level since it presumes all sorts of
PyGame setup and control of the screen, but is there a lower level API
to the decoding software?

Thanks,
Gary Herron

________________________________________________
 Mike C. Fletcher
 Designer, VR Plumber, Coder
 http://www.vrplumber.com
 http://blog.vrplumber.com