Page 1 of 1

Video sequences from your Pygame display

#1 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 607
  • View blog
  • Posts: 1,927
  • Joined: 27-December 13

Posted 15 January 2017 - 12:54 PM

Video sequences from your Pygame display

You may have noticed that I occationally link to small video clip demonstrating my pygame display.
In this snippet I'll show how those videos are made.

As a teaser, take a look at this link (unfortunately MediaFire's servers are currently quite slow, so it may take several minutes to download – I migth migrate to another platform): http://www.mediafire...matrix_rain.avi
mod: alternatively:

The video is (surprise) made from a series of individual screen dumps.
The individual pictures are later collected and converted to a video file.
To generate the screen dumps and name them individually, I use a small module called 'video'.
Inside the module a Python generator takes care of the file name generation (sorry for the redundant expression):
import pygame

def make_video(screen):

    _image_num = 0

    while True:
        _image_num += 1
        str_num = "000" + str(_image_num)
        file_name = "image" + str_num[-4:] + ".jpg", file_name)
        print("In generator ", file_name)  # delete, just for demonstration
        pygame.time.wait(1000)  # delete, just for demonstration

When the generator is first initiated (see below), the name of the display (here: screen) is passed and a counter '_image_num' is initiated.
When the generator is later called from main (by use of next(), see below) it enters its endless loop creating file names in the format ”image0001.jpg” where the number part grows by one in each pass.
When the generator code gets to the 'yield' statement, it passes control back to the main code and only resumes upon a 'next()' command in the main.

Line 13 and 14 are only in for demonstration purpose, you should delete them before use in real code.

The main code looks like this:
from video import make_video
import pygame

screen = pygame.display.set_mode((400, 300))
screen.fill((200, 100, 50))

save_screen = make_video(screen)  # initiate the video generator
video = False  # at start: video not active

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

        # toggle video on/off by clicking 'v' on keyboard #
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_v:
            video = not video

    # game code here #


    if video:
        next(save_screen)  # call the generator
        print("IN main")  # delete, just for demonstration

In line 1 the generator is imported.
In line 8 the generator is initiated, a local variable 'save_screen' now points to the generator; the generator gets the display name ('screen') as argument and initiates as described above.
In line 9, a local boolean is set to False; later we can toggle this between True and False and start/stop the video dump.
Line 11 is the game loop used in most games and recreational programs. I hope line 12-14 is known.
Line 17/18 toggles the 'video' between True and False by clicking on 'v' on the keyboard.
Line 24/25 calls the generator by use of the keyword 'next' with 'save_screen' as argument.

If you download the two code snippets, please remember to place then in the same directory and name the first one 'video'.

When you run the main code (and click on 'v'), you will get nice prints in the console demonstrating that the code flips between main and module. After running the code, you will find a number of image files in the directory, named 'image0001.jpg, … , image####.jpg'.

All you need to do now is convert the pictures into a video. There are many programs you can find on the internet to do this conversion, both free and paid. Some uses compression and some not. Some allows various kind of subsequent manipulation. I don't have an overview and am not an expert in this.
To do a small 10 seconds video, however, I use a free program called MakeAVI. It is very simple and needs no further intro.

To share the videos you need to upload them somewhere and make public a link to the location. Until now I have used MediaFire for this, but many sites offer this, Github, Sourceforge, Dropbox, .. You'll find you own way.

Please remember to share some good game moments with us here on DIC - or use this to demonstrate a problem you need help with.

Is This A Good Question/Topic? 0
  • +

Replies To: Video sequences from your Pygame display

#2 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 607
  • View blog
  • Posts: 1,927
  • Joined: 27-December 13

Posted 24 January 2017 - 09:26 AM

Just a small clarification:
The file extension usable with are:

.bmp (uncompressed)
.tga (uncompressed)
.png (compressed)
.jpeg (compressed)

Many more types, however, can be loaded.

If the file extension is not recognized as one of the four mentioned, Pygame will default to .tga
In the tutorial I have mistakenly used .jpg which doesn't lead to an error but ends up as .tga
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1