Playing a movie file

Whether it's showing your game studio's logo sequence at the start of the game, or it's telling an important part of your story using cutscenes—sometimes you need to play full motion video. That's why in this recipe you will learn how to load a video and replay it in the game window.

Getting ready

This sample uses the project structure created in Setting up the game structure found in Chapter 1. You also need to add a directory called videos to the project's source tree to keep your assets organized. Make sure that the videos directory is on Panda3D's content search path!

Of course, you need to provide a video file for playback. Panda3D uses the FFmpeg programming library for decoding video data, so it should be able to process any codec supported by FFmpeg. The sample code assumes you are using an AVI file.

How to do it...

To play a movie file, complete these tasks:

  1. Copy your video file to the videos directory and rename it to movie.avi.
  2. Open the Application.py file and add the following code:
    from direct.showbase.ShowBase import ShowBase
    from panda3d.core import *
    loadPrcFileData("", "audio-library-name p3openal_audio")
    class Application(ShowBase):
    def __init__(self):
    ShowBase.__init__(self)
    cm = CardMaker("plane")
    cm.setFrame(-1, 1, -1, 1)
    plane = render2d.attachNewNode(cm.generate())
    movie = loader.loadTexture("movie.avi")
    sound = loader.loadSfx("movie.avi")
    plane.setTexture(movie)
    plane.setTexScale(TextureStage.getDefault(), movie.getTexScale())
    movie.setLoop(0)
    movie.synchronizeTo(sound)
    sound.play()
    
  3. Hit the F6 key to run the application and watch the video play:
How to do it...

How it works...

First of all, we need to properly configure Panda3D to use OpenAL for audio output, using the loadPrcFileData() function. This is very important, because otherwise our code would not work!

To get the movie clip to play on the screen, we first use a CardMaker to create the quad the video will be put on as a dynamically updated texture image. We reparent plane to render2d, so it is rendered to Panda3D's 2D drawing layer.

Next, we load the video file into a texture object and set it as our target quad's texture image. Additionally, the texture matrix of plane is modified using setTexScale(). We have to do this because if the pixel width and height of our video is not a power of two, the movie texture Panda3D creates internally will be set to the nearest power-of-two measures. When we put video frames into this texture, it will cause a noticeable amount of stretching and/or squashing, distorting the video and making it appear rather odd.

Finally, we turn off looping the video over and over again and instruct the engine to synchronize the video to the audio data of the video. Note how we use the loadSfx() method to open the movie file a second time to retrieve the audio data!

The last line may appear a bit odd at first. Why would we want to start playing the sound if we want to see the video? The answer to this question can be found just one line above, where we ensure the audio to be synchronized with the video stream. This also causes movie playback to be bound to starting and stopping the sound.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset