Difference between revisions of "Playing Video"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=Setup= Make sure you have a working pygame program, after the pygame.init() add the following: <syntaxhighlight lang=python> FPS = 60 clock = pygame.time.Clock() movie = pyg...")
 
Line 1: Line 1:
 +
=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 [https://github.com/Zulko/moviepy MoviePy].
 +
 
=Setup=
 
=Setup=
 
Make sure you have a working pygame program, after the pygame.init() add the following:
 
Make sure you have a working pygame program, after the pygame.init() add the following:

Revision as of 10:04, 5 July 2018

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.

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()