使用管道直接从np.array高效地编写电影 [英] Efficiently write a movie directly from np.array using pipes

查看:102
本文介绍了使用管道直接从np.array高效地编写电影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个4D的numpy电影帧数组.我正在寻找一种以给定的帧速率将其写入电影的函数.我已在自己的操作系统上安装了FFMPEG,并尽可能地从这些答案中看到,最有效的方法是通过管道.

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.

但是,我对管道的使用经验很少,并且上面链接中的解释对我来说意义不大.此外,几乎没有答案实际上是实现管道的,因此 使用的是Mencoder,而不是FFMPEG.我对FFMPEG经验不足,所以不确定如何修改mencoder的命令字符串以使其在FFMPEG中工作.

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.

我会喜欢的东西:

具有以下形式的功能:

animate_np_array(4d_array,帧速率)-> output.mp4(或其他视频编解码器)

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

哪个实现管道将帧依次发送到FFMPEG,我可以将其复制粘贴到现有代码中.

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

此外,绝对有必要使此函数不要实际绘制任何帧,因为对matplotlib.imshow()函数的调用(如我最常用的用法)会大大降低速度.

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.

推荐答案

ImageIO API 提供了一种行之有效的简单方法:

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.

这篇关于使用管道直接从np.array高效地编写电影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆