如何正确为matplotlib.animation启用ffmpeg? [英] How do I properly enable ffmpeg for matplotlib.animation?
问题描述
到目前为止,我已经尝试过使用ffmpeg进行堆栈操作,因此我可以制作一个缩时视频。
I have covered a lot of ground on stack so far trying to get ffmpeg going so I can make a timelapse video.
我在CentOS 7机器上,正在运行 python3.7.0a0
。
I am on a CentOS 7 machine, running python3.7.0a0
.
python3
>>> import numpy as np
>>> np.__version__
'1.12.0'
>>> import matplotlib as mpl
>>> mpl.__version__
'2.0.0'
>>> import mpl_toolkits.basemap as base
>>> base.__version__
'1.0.7'
我发现此github要点关于安装ffmpeg的信息。我使用了铬源,并且安装时没有前缀
选项(使用默认选项)。
I found this github gist on installing ffmpeg. I used the chromium source, and installed without a prefix
option (using the default).
我已经确认ffmpeg已安装,尽管我对测试它是否有效一无所知。
I have confirmed that ffmpeg is installed, although I don't know anything about testing whether it works.
which ffmpeg
/usr/local/bin/ffmpeg
ffmpeg -version
ffmpeg version N-83533-gada281d Copyright (c) 2000-2017 the FFmpeg dev elopers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-11
configuration:
libavutil 55. 47.100 / 55. 47.100
libavcodec 57. 80.100 / 57. 80.100
libavformat 57. 66.102 / 57. 66.102
libavdevice 57. 2.100 / 57. 2.100
libavfilter 6. 73.100 / 6. 73.100
libswscale 4. 3.101 / 4. 3.101
libswresample 2. 4.100 / 2. 4.100
我尝试运行一些在网上找到的示例示例:
I tried to run a few sample examples I found online:
[1] http:// m atplotlib.org/examples/animation/basic_example_writer.html
[2] https://stackoverflow.com/a/23098090/3454650
在我尝试保存动画文件之前,一切正常。
Everything works fine up until I try to save the animation file.
[1]
anim.save('basic_animation.mp4', writer = FFwriter, fps=30, extra_args=['-vcodec', 'libx264'])
[2]
im_ani.save('im.mp4', writer=writer)
我发现此处 ffmpeg的路径可能是必需的,因此我将其添加到了测试脚本的顶部:
I found here that explictly setting the path to ffmpeg might be necessary so I added this to the top of the test scripts:
plt.rcParams['animation.ffmpeg_path'] = '/usr/local/bin/ffmpeg'
我尝试了一些其他调整代码,但总是得到相同的响应,而我不知道如何开始解密:
I tried a few more tweaks in the code but always get the same response, which I do not know how to begin deciphering:
Traceback (most recent call last):
File "testanim.py", line 27, in <module>
writer.grab_frame()
File "/usr/local/lib/python3.7/contextlib.py", line 100, in __exit__
self.gen.throw(type, value, traceback)
File "/usr/local/lib/python3.7/site-packages/matplotlib/animation.py", line 256, in saving
self.finish()
File "/usr/local/lib/python3.7/site-packages/matplotlib/animation.py", line 276, in finish
self.cleanup()
File "/usr/local/lib/python3.7/site-packages/matplotlib/animation.py", line 311, in cleanup
out, err = self._proc.communicate()
File "/usr/local/lib/python3.7/subprocess.py", line 836, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/local/lib/python3.7/subprocess.py", line 1474, in _communicate
selector.register(self.stdout, selectors.EVENT_READ)
File "/usr/local/lib/python3.7/selectors.py", line 351, in register
key = super().register(fileobj, events, data)
File "/usr/local/lib/python3.7/selectors.py", line 237, in register
key = SelectorKey(fileobj, self._fileobj_lookup(fileobj), events, data)
File "/usr/local/lib/python3.7/selectors.py", line 224, in _fileobj_lookup
return _fileobj_to_fd(fileobj)
File "/usr/local/lib/python3.7/selectors.py", line 39, in _fileobj_to_fd
"{!r}".format(fileobj)) from None
ValueError: Invalid file object: <_io.BufferedReader name=6>
我的配置中是否存在格式错误的内容?我搜索了一段时间的这个错误的谷歌,但没有找到任何有关动画/ ffmpeg的。任何帮助将不胜感激。
Is there something with my configuration that is malformed? I searched google for this error for some time but never found anything relevant to animations / ffmpeg. Any help would be greatly appreciated.
更新:
@LordNeckBeard在这里指向我: https://trac.ffmpeg.org/wiki/CompilationGuide/Centos
@LordNeckBeard pointed me here: https://trac.ffmpeg.org/wiki/CompilationGuide/Centos
我在安装x264编码依赖项时遇到了问题。 libavcodec / *。c中的某些文件(在 make
输出中)报告了对多个函数的未定义引用。在追赶大雁之后,发现了这个: https://mailman.videolan .org / pipermail / x264-devel / 2015-February / 010971.html
I ran into problems with installing the x264 encoding dependency. Some files in libavcodec/*.c (in the make
output) were reporting undefined references to several functions. After a wild goose chase found this: https://mailman.videolan.org/pipermail/x264-devel/2015-February/010971.html
要修复x264安装,我只是添加了一些 configure
标志:
To fix the x264 installation, I simply added some configure
flags:
./configure --enable-static --enable-shared --extra-ldflags="-lswresample -llzma"
更新:
UPDATE:
因此,解决了libx264问题后,所有安装均正常。我继续将ffmpeg二进制文件从 ffmpeg_build
文件夹复制到 / usr / local / bin / ffmpeg
中。
So everything installed fine after fixing the libx264 problems. I went ahead and copied the ffmpeg binary from the ffmpeg_build
folder into /usr/local/bin/ffmpeg
.
运行脚本后,我遇到了ffmpeg找不到libx264共享库的问题。我想我将不得不使用不同的前缀重新编译所有内容。我的直觉告诉我,在弄乱了所有内容之后,使用了一些已损坏的配置,周围有旧文件。
After running the script I was getting problems where ffmpeg could not find the libx264 shared object. I think I will have to recompile everything using different prefixes. My intuition tells me there are old files laying around after I have messed with everything, using some configuration that is broken.
所以我决定也许我应该尝试使用NUX : http://linoxide.com/linux-how-to/install -ffmpeg-centos-7 /
我使用新的rpm安装了ffmpeg,但无济于事。由于缺少共享对象,我仍然无法运行ffmpeg。
So I decided maybe I should just try to use NUX: http://linoxide.com/linux-how-to/install-ffmpeg-centos-7/ I installed ffmpeg using the new rpm, but to no avail. I still was not able to run ffmpeg because of a missing shared object.
最后,而不是将usiong文件复制到我的 / usr / local /中bin
文件夹,我直接从build bin目录运行ffmpeg。事实证明,这确实可以正常工作!
Finally, instead of usiong files copied into my /usr/local/bin
folder, I ran ffmpeg directly from the build bin directory. Turns out that this does work properly!
因此,从本质上讲,如果我想在全系统范围内安装ffmpeg,则需要再次使用非本地前缀从源代码进行手动编译。
So in essence, if I want to install ffmpeg system wide, I need to manually compile from sources again but using a nonlocal prefix.
推荐答案
我在您提供的输出中看不到 ffmpeg
特定信息;但是,您尝试使用libx264,但是您的ffmpeg配置丢失了-enable-gpl --enable-libx264
,而使用该编码器进行编码是必需的。您可以下载用于Linux的 ffmpeg
二进制文件, macOS或不支持libx264的Windows,然后将您的脚本指向它。
I see no ffmpeg
specific info in the output you provided; however you are attempting to use libx264, but your ffmpeg configure is missing --enable-gpl --enable-libx264
which are required to enable encoding with this encoder. You can download a ffmpeg
binary for Linux, macOS, or Windows that does support libx264 then point your script to it.
或者,使用上述配置选项编译 ffmpeg
。
这篇关于如何正确为matplotlib.animation启用ffmpeg?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!