Pygame - Playing Movie

Pygame has discontinued support for video files in its latest version. However, earlier versions on Python 2.7 distributions, it can be still used. For this section, Pygame 1.9.2 and Python 2.7.18 has been used.

The module supports playback video and audio from basic encoded MPEG-1 video files. Movie playback happens in background threads, which makes playback easy to manage. the pygame.mixerpygame module for loading and playing sounds module must be uninitialized if the movie’s sound is to be played.

To begin with obtain a Movie object by following syntax −

movie ='sample.mpg')

The Movie class provides following methods to control playback. start playback of a movie stop movie playback temporarily stop and resume playback advance the movie playback position restart the movie playback get the current vide playback time the total length of the movie in seconds get the resolution of the video check if the movie file contains audio set the audio playback volume set the video target Surface

Following code plays a .MPG file on the Pygame display window. −

import pygame

FPS = 60
clock = pygame.time.Clock()
movie ='sample_640x360.mpg')
screen = pygame.display.set_mode(movie.get_size())
movie_screen = pygame.Surface(movie.get_size()).convert()


playing = True
while playing:
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         playing = False

Kickstart Your Career

Get certified by completing the course

Get Started