My plan is to display the data that a server-side script generates in a video displayable on my web page. So far my approach is the following:
Basically this works, but of course faster would be nicer. I would appreciate proposals for fundamentally different approaches as well as help on the following details:
Depending on the required length of the video, animated gif might be a lot easier than a video format. You might want to have a look at the gif-Animation library on github https://extrapixel.github.io/gif-animation/
To the other (more C-related) questions: fprintf() does formatted output, which is definitely slower than binary output, thus the ppm (text) format is not specifically known for speed. You may want to look for a binary format (such as .gif above) and a matching library for that format. Should be much faster.
fprintf() buffers the output, so, apart from adding extra overhead, accumulating your data into a string would not be much faster.
EDIT: Given the size and running time you mentioned, your best bet is probably writing to a simple binary pixel format that ffmpeg is able to digest - .XWD or .PNG might be a good choice, as there are well-tested libraries available that allow you to draw to and store a device-dependant bitmap drawn by you to such files.
A binary format simply has to store much less data than an ASCII-based one - given that a byte is a byte in binary and at least two (maybe more according to format) in ASCII. And the conversion takes time....