theusual
theusual

Reputation: 69

FFMPEG - Image to Video. Images from RAM? (python)

I challenged myself in making my own pathfinding algorithm in python and succeeded. I'm using PIL to open an image of a maze and when the process is finished, it creates a new image and adds color to the correct path.

While not beeing able to see the actual process from start to finish, I desided to create a picture of the original maze with the current progress, every step. When the process is over, I use FFMPEG to convert the images(png) to video(mp4).

Here's the problem: My last test used a 100x100 maze, it went trough 4840 steps(loops) and executed in 0.0019 seconds. When I saved an image of every step, it created 4840 .png images, which took about 12~ minutes and used a total of 28Mb~ (storage).

I'm using this command-line code:

arg = ffmpeg -framerate 30 -i img%00d.png -c:v libx264 -r 30 -pix_fmt yuv420p out.mp4 os.system(arg)

Is there a way to make a video from images saved in RAM, as this would be much faster? Or write the video file as I go, providing one image after the other from RAM? Thanks in advance!

EDIT: I have tried to do the example @Zulko posted with the imageio library, but I'm facing two problems: 1) The .mp4 file does not play back. I have tried several players. There is no thumbnail on the file either. 2) The process stops after adding image 725. The console is still open, but nothing happens after that point.

Upvotes: 1

Views: 3604

Answers (1)

Zulko
Zulko

Reputation: 3780

With imageio it should give something like this:

import imageio

writer = imageio.get_writer('some_file.mp4', fps=some_fps)

for step in steps:
    im = make_some_image_for_this(step)
    # ( 'im' should be a HxWx3 RGB numpy array I think )
    writer.append_data(im)
writer.close()

Upvotes: 3

Related Questions