5

I have a 4D numpy array of movie frames. I'm looking for a function to write them to a movie, at a given framerate. I have FFMPEG installed on my OS, and as I can see from these answers, the most efficient way to do so is via pipes.

However, I have very little experience using pipes, and the explanations in the link above make little sense to me. Furthermore, very few of the answers seem actually implement pipes, and the one that does uses mencoder, not FFMPEG. I am relatively inexperienced with FFMPEG, so am not sure how to modify the command string from the mencoder answer to make it work in FFMPEG.

WHAT I WOULD LIKE:

A function of the following form:

animate_np_array(4d_array, framerate) -> output.mp4 (or other video codec)

Which implements pipes to send frames one after the other to FFMPEG, and which I can copy-paste into my existing code.

Furthermore, it is absolutely necessary that this function never actually plots any of the frames, as calls to the matplotlib.imshow() function (as I have most typically seen used) slow things down considerably.

Matt Billman
  • 472
  • 5
  • 19

1 Answers1

18

The ImageIO API offers a dead simple way to do this:

import imageio
imageio.mimwrite('output_filename.mp4', np_array , fps = [an int])

While I'm not sure if this uses pipes or not, it's blazingly fast.

Matt Billman
  • 472
  • 5
  • 19
  • 1
    Nice. For my situation, the np_array is of the shape 20x512x512, which contains 20 frames of 512x512 pixels of grayscale images. – user2165 May 25 '19 at 20:05
  • 6
    This requires both `imageio` and `imageio-ffmpeg` modules. `pip install imageio` `pip install imageio-ffmpeg` – Eduardo Pignatelli Jun 05 '19 at 09:02