I have not implemented a solution to this particular problem personally, but I will try to propose it. Of course, any of the solutions will slow down your program dramatically, but technically it is possible. So it's probably worth a try so you can use pygame.image.tostring() for your current frame, and then just write (add) each frame to the file. Thus, this will be your video in raw format. Now, if you need a specific video format, you just need to convert it, the best way is to use ffmpeg (you can call it before exiting the game with certain parameters from your game using subprocess.Popen("ffmpeg ...") ) . The only problem here is that the user who wants to write this way must have ffmpeg installed on the system so that ffmpeg can be called from the shell command line. The advantage of this approach is that it should be quite fast and does not use a lot of additional memory, since you only encode once (for example, when the user stops recording or leaves the game) from the binary stream.
Edit : this is just a broad description, of course, you should also learn the wright options for ffmpeg, such as number of frames, size, etc. and, of course, delete the raw data, as it can be a really large file.
source share