Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiently write a movie directly from np.array using pipes

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.

like image 395
Matt Billman Avatar asked Jun 16 '17 21:06

Matt Billman


1 Answers

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.

like image 67
Matt Billman Avatar answered Oct 27 '22 00:10

Matt Billman