Playing Video

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Video Format

Pygame only supports the .mpg file format (MPEG-1 video, MPEG-1 Audio Layer III (MP3) sound) so any videos will need to be converted. You could alternatively use MoviePy or Pyglet.

Setup

Make sure you have a working pygame program, after the pygame.init() add the following:

FPS = 60
clock = pygame.time.Clock()
movie = pygame.movie.Movie('MELT.MPG')
screen = pygame.display.set_mode(movie.get_size())
movie_screen = pygame.Surface(movie.get_size()).convert()
movie.set_display(movie_screen)

The frame rate and the clock are important because the video lasts for a specific amount of time, if you don't control the frame rate it will play the video as quickly as possible.

Start & Playing the Movie

The movie can be started using the play() method, but remember it needs to be drawn to the screen. So we need to include some sort of game loop:

movie.play()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            movie.stop()
             pygame.quit()

    screen.blit(movie_screen,(0,0))
    pygame.display.update()
    clock.tick(FPS)

No Sound

If you get no sound, it is because the pygame.mixer is initialised. So you can quit the pygame.mixer after the pygame.init():

pygame.mixer.quit()