如何检查有哪些可用的写入程序来保存动画

huangapple go评论71阅读模式
英文:

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文件。")

我无法弄清楚如何实际检查ffmpegpillow是否可用,这意味着当我尝试保存.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=&quot;system_evolution.mp4&quot;, writer=&quot;ffmpeg&quot;, 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(&quot;Saving system_evolution.mp4&quot;)
    anim.save(filename=&quot;system_evolution.mp4&quot;, writer=&quot;ffmpeg&quot;, fps=30)
elif pillow_available():
    print(&quot;Saving system_evolution.gif&quot;)
    anim.save(filename=&quot;system_evolution.gif&quot;, writer=&quot;pillow&quot;, fps=30)
else:
    print(&quot;Please install either ffmpeg to save a mp4 or pillow to save a gif.&quot;)

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(&#39;ffmpeg&#39;)

def pillow_available():
    return animation.writers.is_available(&#39;pillow&#39;)

if ffmpeg_available():
    print(&quot;Saving system_evolution.mp4&quot;)
    anim.save(filename=&quot;system_evolution.mp4&quot;, writer=&quot;ffmpeg&quot;, fps=30)
elif pillow_available():
    print(&quot;Saving system_evolution.gif&quot;)
    anim.save(filename=&quot;system_evolution.gif&quot;, writer=&quot;pillow&quot;, fps=30)
else:
    print(&quot;Please install either ffmpeg to save a mp4 or pillow to save a gif.&quot;)

huangapple
  • 本文由 发表于 2023年6月12日 01:00:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定