Using libmpeg2 with RayLib

Alas there isn’t a simple PlayVideo function in RayLib (it’s far from a simple task). While I did find a simple single header mpg1 player, I couldn’t seem to get too much out of it… In the end I found the libmpeg2 library to work quite well.

The code to accompany this post (below) started off as one of the libmpeg2 examples, but instead of splattering 100’s of ppm files all over your hard drive, it updates a texture which is then used to render a background and as you can see (above) just for lol’s I couldn’t resist some 3d!

I’m pulling in my OS’s development package for libmpeg2 and as such its a dynamic dependency, however there is nothing stopping you statically linking to the library archive (.a) if you want to be sure it will run on systems where libmpeg2 might not be installed (it is available in many (if not all?) distros)

Rendering a frame of video each game loop obviously isn’t going to work, unless you want crowds of people speed walking! The mpeg frame periods are somewhat odd values, however I managed to relate the period to 1/60th of a second (for the programs frame rate). This was a fair bit of tearing my hair out with a calculator, a scant resource these days ! Looking at videos of people walking and a clock, I think I might have it right… but its not extensively tested.

The code to render to a texture takes in a chunk of data. While the example is taking data directly from a file, it is quite possible to preload the whole file and present chunks of data to the decoder from a buffer. After decoding each chunk it converts the data to RGB. Once there is enough data to provide a frame, this is then transferred to a texture.

While the mpeg update code is in the main loop, you should be able to quite easily abstract it into a function (hint you might want to make a struct for the variables its using and pass a pointer to the stuct to the function)

You can grab the code here


Leave a comment

Your email address will not be published. Required fields are marked *