<p>user1118321's suggestion is the correct one but i'll elaborate a bit.</p>

<p>You run your simulation giving it a fixed time step between frames.  This time step would be 1/30th of a second (33.3333 ms) for instance if you wanted a 30 fps movie.</p>

<p>You let your simulation calculations and your rendering run, taking as long as they need to, and then write the rendered frame out to disk, with the frame number in the name (like frame0.bmp, frame1.bmp, etc).</p>

<p>After you have all these frames, you can use ffmpeg to combine them into a video of the desired frame rate.</p>

<p>If you want audio in your video (like sounds when things collide), you can actually keep an audio timeline (what audio events happened on which frames, or at what points in time) while doing your simulation, and then after your rendering is finished, you can generate the audio stream that goes along with your video, which ffmpeg will gladly combine with your video.</p>

<p>I've used this on a few projects in times past and it's worked great.  I also wrote up some more details about this process a while back:</p>

<p><a href="https://blog.demofox.org/2012/09/09/recording-perfectly-lagless-audio-synchronized-video-of-your-game/" rel="nofollow noreferrer">Recording lagless demo videos of a laggy game | The blog at the bottom of the sea</a></p>

CC BY-SA 3.0
2017-09-27T20:14:29.643Z
2018-04-24T07:46:19.220Z
2018-04-24T07:46:19.220Z
Alan Wolfe
<p>You could always write each frame out as an image and then use another tool to compress it into a movie that you playback. You don't say what type of system you're running on, but I believe that <a href="http://ffmpeg.org" rel="nofollow noreferrer">ffmpeg</a> could do it for you, as it's a cross-platform tool. It's hard to estimate how long the compression will take since it will depend on the content you're compressing. I'm not sure whether you can continuously feed it frames, or if you need to generate them all at once.</p>

CC BY-SA 3.0
2017-09-27T02:12:08.333Z
2017-09-27T02:12:08.333Z
user1118321
<p>I have implemented an N-body simulation using the Barnes-Hut optimisation in Python which runs at a not-unacceptable speed for N=10,000 bodies, but it's still too slow to watch real time. </p>

<p>A new frame is generated each time-step, and to display the frame we must first calculate the new positions of the bodies, and then draw them all. For N=10,000, it takes about 5 seconds to generate one frame (this is waaay too high as Barnes-Hut should be giving better results). The display is done through the pygame module.</p>

<p>I would thus like to record my simulation and replay it once after it's done at a higher speed.</p>

<p><strong>How can I accomplish this without slowing down the program or exceeding memory limitations?</strong></p>

<p>One potential solution is simply to save the pygame screen each timestep, but this is apparently very slow. </p>

<p>I thought also about storing the list of positions of the bodies generated each time step, and then redrawing all the frames once the situation finishes. Drawing a frame still takes some time, but not as much time as it takes to calculate the new positions.</p>

CC BY-SA 3.0
2017-09-27T01:19:36.940Z
TBH, the time needed to save out an image file is insignificant compared to 5 seconds. So, I'd just save out the images and stitch them together into a video later, if that's the easiest thing to do. Another possibility is to [stream the frames out to ffmpeg](https://computergraphics.stackexchange.com/a/5627/48) (answer is for C++, but the same idea should be adaptable to Python).
Nathan Reed
CC BY-SA 3.0
2017-09-27T10:14:08.267Z
@NathanReed You should post that as an answer.
Dan Hulme
CC BY-SA 3.0
2017-09-27T18:55:02.617Z
Possible duplicate of [OpenGL animation - turn into mp4 movie](https://computergraphics.stackexchange.com/questions/5606/opengl-animation-turn-into-mp4-movie)
Nathan Reed
CC BY-SA 3.0
2017-09-27T18:55:25.780Z
@DanHulme TBH, I think this question can probably be marked as a dupe of the one I linked. :)
Nathan Reed
CC BY-SA 3.0
2017-09-28T07:28:53.187Z
@NathanReed I'm inclined not. The other question seems to be about turning an apitrace recording into a video. That doesn't have the same concern about keeping a fixed time-step. Also, this questioner has some misconceptions about performance, which need addressing directly.
Dan Hulme
<simulation>
How to record N-body simulation as a video?
math_lover
# How to record N-body simulation as a video?

(Asked by: username_0 on 2017-09-26)

I have implemented an N-body simulation using the Barnes-Hut optimisation in Python which runs at a not-unacceptable speed for N=10,000 bodies, but it's still too slow to watch real time.

A new frame is generated each time-step, and to display the frame we must first calculate the new positions of the bodies, and then draw them all. For N=10,000, it takes about 5 seconds to generate one frame (this is waaay too high as Barnes-Hut should be giving better results). The display is done through the pygame module.

I would thus like to record my simulation and replay it once after it's done at a higher speed.

**How can I accomplish this without slowing down the program or exceeding memory limitations?**

One potential solution is simply to save the pygame screen each timestep, but this is apparently very slow.

I thought also about storing the list of positions of the bodies generated each time step, and then redrawing all the frames once the situation finishes. Drawing a frame still takes some time, but not as much time as it takes to calculate the new positions.

**Tags:** simulation

> **Comments:**
> **username_1**: TBH, the time needed to save out an image file is insignificant compared to 5 seconds. So, I'd just save out the images and stitch them together into a video later, if that's the easiest thing to do. Another possibility is to [stream the frames out to ffmpeg](https://computergraphics.stackexchange.com/a/5627/48) (answer is for C++, but the same idea should be adaptable to Python).  
> **username_2**: @username_1 You should post that as an answer.  
> **username_1**: Possible duplicate of [OpenGL animation - turn into mp4 movie](https://computergraphics.stackexchange.com/questions/5606/opengl-animation-turn-into-mp4-movie)  
> **username_1**: @username_2 TBH, I think this question can probably be marked as a dupe of the one I linked. :)  
> **username_2**: @username_1 I'm inclined not. The other question seems to be about turning an apitrace recording into a video. That doesn't have the same concern about keeping a fixed time-step. Also, this questioner has some misconceptions about performance, which need addressing directly.  

---

## 2 answers

### Answer by username_3 on 2017-09-27. Score: 3

username_4's suggestion is the correct one but i'll elaborate a bit.

You run your simulation giving it a fixed time step between frames. This time step would be 1/30th of a second (33.3333 ms) for instance if you wanted a 30 fps movie.

You let your simulation calculations and your rendering run, taking as long as they need to, and then write the rendered frame out to disk, with the frame number in the name (like frame0.bmp, frame1.bmp, etc).

After you have all these frames, you can use ffmpeg to combine them into a video of the desired frame rate.

If you want audio in your video (like sounds when things collide), you can actually keep an audio timeline (what audio events happened on which frames, or at what points in time) while doing your simulation, and then after your rendering is finished, you can generate the audio stream that goes along with your video, which ffmpeg will gladly combine with your video.

I've used this on a few projects in times past and it's worked great. I also wrote up some more details about this process a while back:

[Recording lagless demo videos of a laggy game | The blog at the bottom of the sea](https://blog.demofox.org/2012/09/09/recording-perfectly-lagless-audio-synchronized-video-of-your-game/)


---

### Answer by username_4 on 2017-09-27. Score: 1

You could always write each frame out as an image and then use another tool to compress it into a movie that you playback. You don't say what type of system you're running on, but I believe that [ffmpeg](http://ffmpeg.org) could do it for you, as it's a cross-platform tool. It's hard to estimate how long the compression will take since it will depend on the content you're compressing. I'm not sure whether you can continuously feed it frames, or if you need to generate them all at once.


---

