Search code examples
carraysvideoffmpegppm

Creating a video from data of server-side script


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:

  1. A three dimensional integer array in a C script is use to accumulate the image data. Dimensions are width, breadth and color (R, G and B).
  2. The array is written to a ppm-file.
  3. The next picture is accumulated and written and so on.
  4. With ffmpeg a script merges the ppm-files to a mp4-video.

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:

  • Is there a file format simple as ppm that uses the HEX code for colors instead of triplets? This would reduce the size of my array as well a the number of write operations.
  • Do I loose much time if I print every single value to the file with an fprintf operation instead of accumulating lines into a string? Or do compilers optimize this kind of sequences of writes?

Solution

  • 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....