英文:
How to check which writers are available for saving an animation
问题
我正在使用matplotlib制作一个随时间演变的系统动画。我想将动画保存为一个文件。我的默认选择是保存为`.mp4`文件,这意味着我应该像这样使用`ffmpeg`写入器:
```python
anim.save(filename="system_evolution.mp4", writer="ffmpeg", fps=30)
问题是,我正在与我的同学分享我的代码,他们的系统上可能没有安装ffmpeg
。在这种情况下,我想回退到使用pillow
保存动画为.gif
文件(大多数人使用Anaconda
安装Python,所以他们可能也安装了Pillow
)。我怎么能检查哪些写入器可用于保存动画呢?
我想要类似这样的东西:
if ffmpeg可用():
print("保存 system_evolution.mp4")
anim.save(filename="system_evolution.mp4", writer="ffmpeg", fps=30)
elif pillow可用():
print("保存 system_evolution.gif")
anim.save(filename="system_evolution.gif", writer="pillow", fps=30)
else:
print("请安装ffmpeg以保存mp4文件,或安装pillow以保存gif文件。")
我无法弄清楚如何实际检查ffmpeg
或pillow
是否可用,这意味着当我尝试保存.mp4
时程序会崩溃,因为没有安装ffmpeg
。如何进行这种检查?
<details>
<summary>英文:</summary>
I am using matplotlib to make an animation of the evolution of a system over time. I want to be able to save the animation as a file. My default choice is to save as an `.mp4` file, which means I should use the `ffmpeg` writer like this:
```python
anim.save(filename="system_evolution.mp4", writer="ffmpeg", fps=30)
The problem is that I am sharing my code with my classmates who don't necessarily have ffmpeg
installed on their systems. In this case, I would like to fall back to saving a .gif
of the animation using pillow
(most of them install Python using Anaconda
so they probably have Pillow
installed as well). How can I check which writers are available to use for saving the animation?
I would like to have something like this:
if ffmpeg_available():
print("Saving system_evolution.mp4")
anim.save(filename="system_evolution.mp4", writer="ffmpeg", fps=30)
elif pillow_available():
print("Saving system_evolution.gif")
anim.save(filename="system_evolution.gif", writer="pillow", fps=30)
else:
print("Please install either ffmpeg to save a mp4 or pillow to save a gif.")
I couldn't figure out how to actually check if ffmpeg
or pillow
are available, which means the program crashes when I try to save an .mp4
and ffmpeg
isn't installed. How can this be checked?
答案1
得分: 0
根据这个链接,这个模块中的writers
对象有一个名为is_available()
的方法,可以用来检查特定的写入器是否可用。
你可以像这样做:
import matplotlib.animation as animation
def ffmpeg_available():
return animation.writers.is_available('ffmpeg')
def pillow_available():
return animation.writers.is_available('pillow')
if ffmpeg_available():
print("Saving system_evolution.mp4")
anim.save(filename="system_evolution.mp4", writer="ffmpeg", fps=30)
elif pillow_available():
print("Saving system_evolution.gif")
anim.save(filename="system_evolution.gif", writer="pillow", fps=30)
else:
print("Please install either ffmpeg to save a mp4 or pillow to save a gif.")
英文:
According to this, the writers
object in this module has a method called is_available()
which can be used to check if a specific writer is available.
You can do sth like that
import matplotlib.animation as animation
def ffmpeg_available():
return animation.writers.is_available('ffmpeg')
def pillow_available():
return animation.writers.is_available('pillow')
if ffmpeg_available():
print("Saving system_evolution.mp4")
anim.save(filename="system_evolution.mp4", writer="ffmpeg", fps=30)
elif pillow_available():
print("Saving system_evolution.gif")
anim.save(filename="system_evolution.gif", writer="pillow", fps=30)
else:
print("Please install either ffmpeg to save a mp4 or pillow to save a gif.")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论