Efficiently write a movie directly from np.array u

2019-05-16 22:52发布

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.

1条回答
乱世女痞
2楼-- · 2019-05-16 23:04

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.

查看更多
登录 后发表回答